nginx檢測腳本html
#!/bin/bash run=`ps -C nginx --no-header | wc -l` if [ $run -eq 0 ] then /usr/local/nginx/sbin/nginx -s stop /usr/local/nginx/sbin/nginx sleep 3 if [ `ps -C nginx --no-header | wc -l` ] then killall keepalived fi fi
安裝keepalivednginx
yum -y install gcc pcre-devel zlib-devel openssl-devel keepalived
配置/etc/keeplived/keepalived.conf
bash
global_defs { #notification_email { # 15456@qq.com #} #notification_email_from Alexandre.Cassen@firewall.loc #smtp_server 192.168.200.1 #smtp_connect_timeout 30 router_id LVS_DEVEL } # 必定要有的空格 vrrp_script chk_http_port {# 必定是寫在vrrp_instance前面 script "/usr/local/src/check_nginx.sh" interval 2 # 檢測腳本執行的間隔2秒 weight 2 } # 必定要有的空格 vrrp_instance VI_1 { state MASTER # 備份服務器上將 MASTER 改成 BACKUP interface eth0 # 網卡 virtual_router_id 51 # 主、備機的virtual_router_id必須相同 priority 100 # 主、備機取不一樣的優先級,主機值較大,備份機值較小 advert_int 1 authentication { auth_type PASS auth_pass 1234 } virtual_ipaddress { 192.168.96.138 # VRRP H虛擬地址 } track_script { # 追蹤trace_script要定在virtual_ipaddress的後面 chk_http_port } }
啓動keepalived服務服務器
service keepalived start
查看是否生效 oop
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 08:00:27:18:9f:0b brd ff:ff:ff:ff:ff:ff
inet 192.168.15.253/24 brd 192.168.15.255 scope global eth0
inet 192.168.15.252/32 scope global eth0
inet6 fe80::a00:27ff:fe18:9f0b/64 scope link
valid_lft forever preferred_lft foreverspa
若是keepalived服務啓動後出現主備機都有虛ip的狀況,請查看網卡是否開啓混雜模式code
ip link set eth1 promisc on
參考文檔:https://www.cnblogs.com/kevingrace/p/6138185.htmlrouter