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 跟服务器同步
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.rpm
查看生成文件
rpm -ql keepalived
4、复制
复制安装包
scp keepalived-1.2.7-5.el5.i386.rpm node2:/root/
安装
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在同一网段
7、验证HTTPD服务,并开启
Netstat -tnlp
Service httpd start
验证网页
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
验证
把脚本复制给另一台主机
scp startrs.sh 172.16.33.12:/root/
执行一次
./startrs.sh start
9、配置keepalived
复制一份做备份
cp keepalived.conf keepalived.conf.back
编辑 keepalived.conf
复制一份给node2
scp keepalived.conf node2:/etc/keepalived/
编辑 keepalived.conf
10、启动keepalived
Service keepalived start
查看地址
Ip addr show
11、安装ipvsadm
Yum -y install ipvsadm
重启keepalived服务器
service keepalived restart
验证成功
curl -I 172.16.33.12
四、使keepalived 支持web 服务
关闭keepalived
安装web服务器
为web提供页面
启动web服务
复制keepalived.conf.haproxy_example为keepalived.conf
cp keepalived.conf.haproxy_example keepalived.conf
编辑keepalived.conf
复制到node2
scp keepalived.conf notify.sh node2:/etc/keepalived/
修改keepalived.conf
把优先级改小,主改从
启动服务
Ifconfig