利用keepalived實現高可用nginx(修改正)
阿新 • • 發佈:2018-11-29
實驗拓撲圖
(1)本次基於VMware Workstation搭建一個四臺Linux(CentOS 7.5)系統所構成的一個伺服器叢集,其中兩臺nginx做前端排程伺服器(一臺為主機,另一臺為備機),另外兩臺作為真實的Web伺服器
(2)本次實驗設定了一個VIP(Virtual IP)為192.168.190.130,使用者只需要訪問這個IP地址即可獲得網頁服務。其中,nginx主機為192.168.190.133,備機為192.168.190.134。
一,配置nginx反向代理伺服器,在兩臺nginx上都做一遍下面的操作
1,在http語句塊中定義排程規則 vim /etc/nginx/nginx.conf http { ..... upstream webser { server 172.18.38.200:80; server 172.18.38.201:80; } ..... } 2,而後在server中呼叫 vim /etc/nginx/conf.d/vhost.conf server { listen 172.18.38.99:80; server_name www.a.com; location / { proxy_pass http://webser; } }
2,重啟nginx,使配置生效
systemctl restart nginx
二,配置keepalived+nginx_master伺服器**
1,安裝keepalived
yum install keepalived
2,修改keepalived配置檔案
! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from
[email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id Lvs133 vrrp_mcast_group4 224.1.1.1 } vrrp_script chk_nginx { script "killall -0 nginx && exit 0 || exit 1" interval 1 weight -30 fall 2 rise 2 } vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.190.130 } track_script { chk_nginx } }
關鍵配置:
1,定義nginx健康檢查指令碼
vrrp_script chk_nginx {
script "killall -0 nginx && exit 0 || exit 1"
interval 1 #以秒觸發一次
weight -30 #nginx宕機就立馬減去有限級30
fall 2 #檢查兩次如果都是宕機就表示nginx掛掉了
rise 2 #宕機之後兩次檢查nginx是活著的就重新+30優先順序
}
2,呼叫
track_script {
chk_nginx
3,啟動keepalived
systemctl start keepalived
systemctl enable keepalived
三,配置keepalived+nginx_slave伺服器
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id Lvs134
vrrp_mcast_group4 224.1.1.1
}
vrrp_script chk_nginx {
script "killall -0 nginx && exit 0 || exit 1"
interval 1
weight -30
fall 2
rise 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.190.130
}
track_script {
chk_nginx
}
}
7,啟動keepalived
systemctl start keepalived
systemctl enable keepalived
d
在113機子執行
systemctl stop keepalived