keepalived是一个用于做双机热备(HA)的软件,常和haproxy联合起来做热备+负载均衡,达到高可用
1. 实验环境
Master:10.20.2.233
Backup:10.20.2.235
虚拟IP:10.20.2.238
2. Keepalived安装
1) 通过yum安装软件依赖包
yum install -y wget make gcc openssl-devel popt-devel
2) Keepalived源码包下载安装
下载地址:http://www.keepalived.org/software/keepalived-1.2.23.tar.gz
tar -zxf keepalived-1.2.23.tar.gz -C /usr/src cd /sru/src/keepalived-1.2.23/ ./configure --prefix=/usr/local/keepalived/ Make make install
3) 安装成功后做成服务模式
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
3. 编辑配置文件
mkdir -p /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
1) Master配置文件内容如下:
! Configuration File for keepalived global_defs { router_id HA_TEST } #监测haproxy进程状态,每2秒执行一次 vrrp_script chk_haproxy { script "/etc/keepalived/check_haproxy.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER # 从为BACKUP priority 200 # 从为100 virtual_router_id 51 #路由ID,可通过#tcpdump vrrp查看。 garp_master_delay 1 #主从切换时间,单位为秒。 interface eth0 advert_int 1 mcast_src_ip 10.20.2.233 #MASTER服务器IP,从服务器写从服务器的IP authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.20.2.238 #虚拟IP } track_script { chk_haproxy #监测haproxy进程状态 } }
2) BACKUP配置文件内容如下:
! Configuration File for keepalived global_defs { router_id HA_TEST } #监测haproxy进程状态,每2秒执行一次 vrrp_script chk_haproxy { script "/etc/keepalived/check_haproxy.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP # 主为MASTER priority 100 # 主为101 virtual_router_id 51 #路由ID,可通过#tcpdump vrrp查看。 garp_master_delay 1 #主从切换时间,单位为秒。 interface eth0 advert_int 1 mcast_src_ip 10.20.2.235 #BACKUP服务器IP,主服务器写主服务器的IP authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.20.2.238 #虚拟IP } track_script { chk_haproxy #监测haproxy进程状态 } }
3) haproxy检测脚本(check_haproxy.sh)内容如下
#!/bin/bash A=`ps -C haproxy --no-header |wc -l` if [ $A -eq 0 ];then /etc/init.d/haproxyd start sleep 3 if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then /etc/init.d/keepalived stop fi Fi chmod 755 /etc/keepalived/check_haproxy.sh #脚本编写完成后需要赋上权限
4) Keepalived的启停
service keepalived start #停止 service keepalived stop #启动 service keepalived restart #重启
4. 测试:
1) 启动MASTER和BACKUP上的keepalived
service keepalived start (keepalived启动的同时会将haproxy启动) [[email protected] keepalived]# service keepalived start Starting keepalived: [ OK ] [[email protected] keepalived]# service haproxyd status HAProxy(pid 32077) is running!
MASTER绑定了VIP,BACKUP未绑定VIP
[[email protected] keepalived]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:85:46:b2 brd ff:ff:ff:ff:ff:ff inet 10.20.2.233/24 brd 10.20.2.255 scope global eth0 inet 10.20.2.238/32 scope global eth0 inet6 fe80::250:56ff:fe85:46b2/64 scope link valid_lft forever preferred_lft forever [[email protected] keepalived]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:85:73:ce brd ff:ff:ff:ff:ff:ff inet 10.20.2.235/24 brd 10.20.2.255 scope global eth0 inet6 fe80::250:56ff:fe85:73ce/64 scope link valid_lft forever preferred_lft forever
2) 停止主上的haproxy,3秒后,keepalived会将其启动
[[email protected] keepalived]# service haproxyd stop STOP HAProxy Listen STOP HAProxy process [[email protected] keepalived]# service haproxyd status HAProxy(pid 32368) is running!
3) 停止主上的keepalived,备机马上接管服务
BACKUP上绑定了VIP
[[email protected] keepalived]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:85:73:ce brd ff:ff:ff:ff:ff:ff inet 10.20.2.235/24 brd 10.20.2.255 scope global eth0 inet 10.20.2.238/32 scope global eth0 inet6 fe80::250:56ff:fe85:73ce/64 scope link valid_lft forever preferred_lft forever
4) 启动主上的keepalived,MASTER绑定VIP,BACKUP释放了VIP
[[email protected] keepalived]# service keepalived start Starting keepalived: [ OK ] [[email protected] keepalived]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:85:73:ce brd ff:ff:ff:ff:ff:ff inet 10.20.2.235/24 brd 10.20.2.255 scope global eth0 inet 10.20.2.238/32 scope global eth0 inet6 fe80::250:56ff:fe85:73ce/64 scope link valid_lft forever preferred_lft forever
时间: 2024-10-06 13:20:59