zi663227 发表于 2018-12-29 13:52:07

linux web + keepalived 应用

  Linux中的Keepalived功能
  一、Keepalived 最初设计,就是为ipvs提供高可用功能
              ipvs --> HA
              ipvs: --> VIP
      1、VRRP: Virtual Routing Redundent Protocol
      虚拟路由冗余协议,主要实现地址漂移
备份组中:有一个主,master,多个从, slave
      master:
       选举协议:
      一主多从:
      备份组:
                  master, slave
      2、VRRP优点:
      冗余:可以使用多个路由器设备作为LAN客户端的默认网关,大大降低了默认网关成为单点故障的可能性;
      负载共享:允许来自LAN客户端的流量由多个路由器设备所共享;
      多VRRP组:在一个路由器物理接口上可配置多达255个VRRP组;
      多IP地址:基于接口别名在同一个物理接口上配置多个IP地址,从而支持在同一个物理接口上接入多个子网;
      抢占:在master故障时允许优先级更高的backup成为master;
      通告协议:使用IANA所指定的组播地址224.0.0.18进行VRRP通告;
      VRRP追踪:基于接口状态来改变其VRRP优先级来确定最佳的VRRP路由器成为master;
      3、VRRP监视功能:
    监视指定接口功能
    监视Trank项功能
  二、Keepalived的工作原理:
IP:VRRP漂移
Lvs:转移lvs规则
Health check:健康检查
  三、Keepalived的配置与安装
1、时间同步:
hwclock -s跟主机同步
ntpdate 172.16.0.1跟服务器同步http://blog.运维网.com/attachment/201305/191512395.jpg
2、安装keepalived服务
启用
--enable-vrrp
禁用
--enable-snmp
3、下载rpm包并安装
先安装yum源
wget ftp://172.16.0.1/pub/gls/server.repo -O /etc/yum.repos.d/server.repo
然后下载并安装keepalived
yum --nogpgcheck localinstall keepalived-1.2.7-5.el5.i386.rpmhttp://blog.运维网.com/attachment/201305/191652468.jpg
查看生成文件
rpm -ql keepalivedhttp://blog.运维网.com/attachment/201305/191716926.jpg
4、复制
复制安装包
scp keepalived-1.2.7-5.el5.i386.rpm node2:/root/http://blog.运维网.com/attachment/201305/191825811.jpg
安装
yum --nogpgcheck localinstall keepalived-1.2.7-5.el5.i386.rpm
5、查看配置文件
ls /etc/keepalived/
vrrp_instance: 定义虚拟路由
virtual_ipaddress:定义虚拟地址
virtual_server 192.168.200.100 443:定义集群服务
6、再次准备两台主机,并配置IP在同一网段http://blog.运维网.com/attachment/201305/191937624.jpg
7、验证HTTPD服务,并开启
Netstat -tnlphttp://blog.运维网.com/attachment/201305/192028721.jpg
Service httpd starthttp://blog.运维网.com/attachment/201305/192045839.jpg
验证网页http://blog.运维网.com/attachment/201305/192102971.jpg
8、编写RealServer脚本
#!/bin/bash
#
# Script to start LVS DR real server.
# description: LVS DR real server
#
./etc/rc.d/init.d/functions
VIP=192.168.0.219
host=`/bin/hostname`
case"$1"in
start)
# Start LVS-DR real server on thismachine.
/sbin/ifconfig lo down
/sbin/ifconfig lo up
echo 1> /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2> /proc/sys/net/ipv4/conf/lo/arp_announce
echo 1> /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2> /proc/sys/net/ipv4/conf/all/arp_announce
/sbin/ifconfig lo:0$VIP broadcast $VIP netmask 255.255.255.255up
/sbin/route add -host $VIP dev lo:0
;;
stop)
# Stop LVS-DR real server loopback device(s).
/sbin/ifconfig lo:0down
echo 0> /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0> /proc/sys/net/ipv4/conf/lo/arp_announce
echo 0> /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0> /proc/sys/net/ipv4/conf/all/arp_announce
;;
status)
# Status of LVS-DR real server.
islothere=`/sbin/ifconfig lo:0| grep $VIP`
isrothere=`netstat -rn | grep "lo:0"| grep $VIP`
if[ ! "$islothere"-o ! "isrothere"];then
# Either the route or the lo:0device
# not found.
echo "LVS-DR real server Stopped."
else
echo "LVS-DR real server Running."
fi
;;
*)
# Invalid entry.
echo "$0: Usage: $0 {start|status|stop}"
exit 1
;;
esac

执行一次
./startrs.sh start
验证
http://blog.运维网.com/attachment/201305/192957887.jpg


把脚本复制给另一台主机
scp startrs.sh 172.16.33.12:/root/
执行一次
./startrs.sh start
9、配置keepalived
复制一份做备份
cp keepalived.conf keepalived.conf.back
编辑 keepalived.conf
http://blog.运维网.com/attachment/201305/193157411.jpg
复制一份给node2
    scp keepalived.confnode2:/etc/keepalived/

编辑 keepalived.conf
10、启动keepalived
Service keepalived starthttp://blog.运维网.com/attachment/201305/193519724.jpg
查看地址
Ip addr show
11、安装ipvsadm
Yum -y install ipvsadmhttp://blog.运维网.com/attachment/201305/193802196.jpg
重启keepalived服务器
service keepalived restarthttp://blog.运维网.com/attachment/201305/193744995.jpg
验证成功
curl -I 172.16.33.12http://blog.运维网.com/attachment/201305/193823288.jpg
四、使keepalived 支持web 服务
关闭keepalivedhttp://blog.运维网.com/attachment/201305/193844417.jpg
安装web服务器http://blog.运维网.com/attachment/201305/193858255.jpg
为web提供页面
http://blog.运维网.com/attachment/201305/193935688.jpg
启动web服务
http://blog.运维网.com/attachment/201305/193949350.jpg
复制keepalived.conf.haproxy_example为keepalived.conf
    cp keepalived.conf.haproxy_examplekeepalived.conf
编辑keepalived.conf
复制到node2
scp keepalived.conf notify.shnode2:/etc/keepalived/
修改keepalived.conf
把优先级改小,主改从
启动服务
http://blog.运维网.com/attachment/201305/194114803.jpg
Ifconfig
http://blog.运维网.com/attachment/201305/194128347.jpg
  

  




页: [1]
查看完整版本: linux web + keepalived 应用