# tar zxvf keepalived-1.3.5.tar.gz #解壓 # cd keepalived-1.3.5/ # ./configure # make && make install #編譯安裝
在/etc/keepalived/ 目錄下建立keepalived.confnginx
# vim /etc/keepalived/keepalived.conf #添加如下內容
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from www@example.com smtp_server mail.example.com smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" interval 2 weight -5 fall 3 rise 2 } vrrp_instance VI_1 { state MASTER interface eth0 #網卡接口名稱 virtual_router_id 51 priority 101 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.2.95 #虛擬地址1 } track_script { chk_nginx } } vrrp_instance VI_2 { state BACKUP interface eth0 #網卡接口名 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.2.96 #虛擬地址2 } track_script { chk_nginx } }
在/etc/keepalived/ 目錄下建立keepalived.confvim
vim /etc/keepalived/keepalived.conf #添加如下內容
Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from www@example.com smtp_server mail.example.com smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" interval 2 weight -5 fall 3 rise 2 } vrrp_instance VI_1 { state BACKUP interface eth0 #網卡名稱 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.2.95 #虛擬地址1 } track_script { chk_nginx } } vrrp_instance VI_2 { state MASTER interface eth0 #網卡名稱 virtual_router_id 52 priority 101 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.2.96 #虛擬地址2 } track_script { chk_nginx } }
# vim /etc/keepalived/check_nginx.sh
#!/bin/bash counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then /usr/local/nginx/sbin/nginx sleep 2 counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then service keepalived stop fi fi
# chmod +x check_nginx.sh
# vim /lib/systemd/system/keepalived.service
[Unit] Description=LVS and VRRP High Availability Monitor After=syslog.target network.target [Service] Type=simple PIDFile=/usr/local/var/run/keepalived.pid KillMode=process EnvironmentFile=-/usr/local/etc/sysconfig/keepalived ExecStart=/usr/local/sbin/keepalived --dont-fork -D ExecReload=/bin/kill -HUP $MAINPID [Install] WantedBy=multi-user.target
# systemctl enable keepalived.service # 設置開機自啓動 # systemctl start keepalived.service # systemctl stop keepalived.service
防火牆啓用狀態下執行centos
# firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 \ --in-interface enp0s8 --destination 224.0.0.18 --protocol vrrp -j ACCEPT # firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 \ --out-interface enp0s8 --destination 224.0.0.18 --protocol vrrp -j ACCEPT # firewall-cmd --reload
服務器網絡環境中,路由交換層禁用了ARP的廣播限制,形成了keepalived主備協議沒法經過廣播的方式進行通訊,形成主備兩臺服務器都強制佔用HAVIP地址,出現同時兩臺服務器都有VIP地址的狀況出現。必須經過配置來指定IP的兩臺服務器間進行通bash
須要修改配置文件服務器
priority 100 unicast_src_ip 192.168.2.192 #本機實際IP unicast_peer { 192.168.2.191 #對端實際IP }
另外一臺服務器配置類似,只是互換IP地址網絡
服務啓動後,可查看VIP是否綁定ide
# ip addr
顯示結果centos7