Keepalived+nginx 高可用配置 - 双主模式(双机互为主备)

rusli
Site Admin
文章: 212
註冊時間: 週三 7月 07, 2010 9:49 pm

Keepalived+nginx 高可用配置 - 双主模式(双机互为主备)

文章rusli » 週二 9月 05, 2017 11:30 am

keepalived+nginx.png
keepalived+nginx.png (85.36 KiB) 已瀏覽 187 次



安裝
yum -y install keepalived
兩台要做的設定
cd /etc/keepalived/
vim nginx.sh

# 监控nginx进程,若nginx主进程不存在则启动nginx
# 若5s后nginx进程还是不存在的话kill掉keepalived进程,防止nginx没运行该主机的keepalived还接管虚拟IP
#!/bin/bash
status=$(ss -lnp | grep -c 'nginx')
if [ ${status} == 0 ]; then
systemctl nginx restart
sleep 2
status=$(ss -lnp | grep -c 'nginx')
if [ ${status} == 0 ]; then
systemctl stop keepalived
fi
fi

chmod +x nginx.sh

cp keepalived.conf keepalived.conf.bak
vi keepalived.conf

vrrp_script nginx {
script "/etc/keepalived/nginx.sh"
interval 2 # 检测间隔2s
weight -5 # 若检测失败权重减低5
fall 3 # 检测失败3次就定义为down状态
rise 2 # 检测失败后,检测成功超过2次就定义为up状态
}

172.16.20.137

代碼: 選擇全部

vrrp_instance VI_1 {
    state MASTER # master_server
    interface ens160
    virtual_router_id 51
    priority 100 # 权重值,值大的优先级高
    advert_int 2 # 检测时间间隔2s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.20.111 # VIP
    }
    track_script {
       nginx # 检测脚本
    }
}

vrrp_instance VI_2 {
    state BACKUP # backup_server
    interface ens160
    virtual_router_id 52
    priority 100 # 权重值,值大的优先级高
    advert_int 2 # 检测时间间隔2s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.20.110 # VIP
    }
    track_script {
       nginx # 检测脚本
    }
}
 


172.16.20.138

代碼: 選擇全部

vrrp_script nginx {
    script "/etc/keepalived/nginx.sh"
    interval 2 # 检测间隔2s
    weight -5 # 若检测失败权重减低5
    fall 3 # 检测失败3次就定义为down状态
    rise 2 # 检测失败后,检测成功超过2次就定义为up状态
}

vrrp_instance VI_1 {
    state BACKUP # backup_server
    interface ens160
    virtual_router_id 51
    priority 100 # 权重值,值大的优先级高
    advert_int 2 # 检测时间间隔2s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.20.111 # VIP
    }
    track_script {
       nginx # 检测脚本
    }
}

vrrp_instance VI_2 {
    state MASTER # master_server
    interface ens160
    virtual_router_id 52
    priority 100 # 权重值,值大的优先级高
    advert_int 2 # 检测时间间隔2s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.20.110 # VIP
    }
    track_script {
       nginx # 检测脚本
    }
}


172.16.20.135

代碼: 選擇全部

 vrrp_script nginx {
    script "/etc/keepalived/nginx.sh"
    interval 2 # 检测间隔2s
    weight -5 # 若检测失败权重减低5
    fall 3 # 检测失败3次就定义为down状态
    rise 2 # 检测失败后,检测成功超过2次就定义为up状态
}


vrrp_instance VI_1 {
    state MASTER # master_server
    interface ens160
    virtual_router_id 50
    priority 100 # 权重值,值大的优先级高
    advert_int 2 # 检测时间间隔2s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.20.222 # VIP
    }
    track_script {
       nginx # 检测脚本
    }
}

vrrp_instance VI_2 {
    state BACKUP # backup_server
    interface ens160
    virtual_router_id 49
    priority 100 # 权重值,值大的优先级高
    advert_int 2 # 检测时间间隔2s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.20.220 # VIP
    }
    track_script {
       nginx # 检测脚本
    }
}


172.16.20.136

代碼: 選擇全部

 vrrp_script nginx {
    script "/etc/keepalived/nginx.sh"
    interval 2 # 检测间隔2s
    weight -5 # 若检测失败权重减低5
    fall 3 # 检测失败3次就定义为down状态
    rise 2 # 检测失败后,检测成功超过2次就定义为up状态
}

vrrp_instance VI_1 {
    state BACKUP # backup_server
    interface ens160
    virtual_router_id 50
    priority 100 # 权重值,值大的优先级高
    advert_int 2 # 检测时间间隔2s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.20.222 # VIP
    }
    track_script {
       nginx # 检测脚本
    }
}

vrrp_instance VI_2 {
    state MASTER # master_server
    interface ens160
    virtual_router_id 49
    priority 100 # 权重值,值大的优先级高
    advert_int 2 # 检测时间间隔2s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.20.220 # VIP
    }
    track_script {
       nginx # 检测脚本
    }
}


firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface ens160 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=112/tcp --permanent
firewall-cmd --reload

setenforce 0 ##设置SELinux 成为permissive模式
##setenforce 1 设置SELinux 成为enforcing模式

systemctl start keepalived
systemctl restart keepalived
虛擬IP查詢
ip addr # 查看虚拟IP

[驗證測試]
打開瀏覽器: http://172.16.20.111 http://172.16.20.110 http://172.16.20.222 http://172.16.20.220 可以看到畫面
到  其中一台主機 執行: systemctl stop keepalived
再打開瀏覽器: http://172.16.20.111 http://172.16.20.110 http://172.16.20.222 http://172.16.20.220 一樣可以看到畫面

查看log : tail -f /var/log/messages

回到「Nginx」

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 0 位訪客