yum安裝
yum -y install keepalived
源碼包下載地址
https://src.fedoraproject.org/repo/pkgs/keepalived/
可自行選擇版本,本次使用yum安裝bash
主節點配置測試
[root@ol13 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { *@*.com #郵件報警及相關配置 } notification_email_from noreply@hxshop.com smtp_server smtp.qq.com smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_http_port { script "/etc/keepalived/check_haproxy.sh" #檢測ha是否存活的腳本 interval 2 weight 2 fall 2 rise 1 } vrrp_instance VI_1 { state MASTER #標識爲主 interface eth0 #綁定網卡 virtual_router_id 55 #主備節點應一致 priority 110 #優先級主節點應高於備節點 advert_int 1 authentication { #驗證信息主備節點應一致 auth_type PASS auth_pass 123456 } track_script { chk_http_port #測試腳本 在上文中定義 } virtual_ipaddress { 172.16.0.248/24 dev eth0 label eth0:0 #綁定的對應Ip } }
備節點配置調試
[root@ol23 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { *@*.com } notification_email_from noreply@hxshop.com smtp_server smtp.qq.com smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_http_port { script "/etc/keepalived/check_haproxy.sh" interval 2 weight 2 fall 2 rise 1 } vrrp_instance VI_1 { state BACKUP interface eth0 #對應網卡設備名 virtual_router_id 55 #與主節點相同 priority 90 #低於主節點 advert_int 1 #nopreempt authentication { auth_type PASS auth_pass 123456 } track_script { chk_http_port } virtual_ipaddress { 172.16.0.248/24 dev eth0 label eth0:0 } }
[root@ol13 ~]# cat /etc/keepalived/check_haproxy.sh #!/bin/bash A=`ps -C haproxy --no-header |wc -l` if [ $A -eq 0 ];then systemctl start haproxy # /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg echo "`date`:HaProxy start" >> /etc/keepalived/check_ha.log fi sleep 2 if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then systemctl stop keepalived # /etc/init.d/keepalived stop echo "`date`:keepalived stop" >> /etc/keepalived/check_ha.log fi
測試腳本在主備節點均存在
須要加上執行權限
[root@ol13 ~]# chmod +x /etc/keepalived/check_haproxy.sh
日誌
查看日誌
[root@ol23 ~]# journalctl -u keepalived
開啓防火牆 注意修改對應網卡設備名code
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface ens33 --destination 224.0.0.18 --protocol vrrp -j ACCEPT firewall-cmd --reload
主備同時
systemctl start keepalived
驗證
ip a
router
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:54:00:fb:5c:5f brd ff:ff:ff:ff:ff:ff inet 172.16.0.13/24 brd 172.16.0.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet 172.16.0.248/24 scope global secondary eth0:0 valid_lft forever preferred_lft forever
會出現多個ipserver