CentOS 7 下 Keepalived + Nginx 實現雙機高可用
文章目錄
- CentOS 7 下 Keepalived + Nginx 實現雙機高可用
- 服務器準備
- 服務信息
- 服務架構
- 服務安裝
- nginx
- Keepalived
- 服務配置
- nginx
- Keepalived
- 啟動服務
- nginx
- keepalived
- 服務驗證
- 查看 VIP 狀態
- CURL 命令訪問
- 瀏覽器訪問
- 高可用驗證
- 停止 web01 下 Nginx
- 恢復 web01 下 Nginx
- 參考
服務器準備
服務信息
主機名 | IP | 角色 | 其他 |
---|---|---|---|
my-web01 | 192.168.157.31 | nginx keepalived | master |
my-web02 | 192.168.157.32 | nginx keepalived | backup |
VIP | 192.168.157.30 |
服務架構
服務安裝
nginx
# 所有主機
[root@my-web01 ~]$ yum -y install nginx
Keepalived
# 所有主機
[root@my-web01 ~]$ yum -y install keepalived
服務配置
nginx
- web01
[root@my-web01 ~]$ cat /usr/share/nginx/html/index.html
<!DOCTYPE html>
<h1>my web01 ~~~</h1>
- web02
[root@my-web02 ~]$ cat /usr/share/nginx/html/index.html
<!DOCTYPE html>
<h1>my web02 ~~~</h1>
Keepalived
- web01
[root@my-web01 ~]$ cat /etc/keepalived/keepalived.conf
! Configuration File for keepalivedvrrp_script check_nginx {script "killall -0 nginx"interval 2
}vrrp_instance VI_1 {interface ens33state MASTERpriority 200virtual_router_id 33virtual_ipaddress {192.168.157.30}authentication {auth_type PASSauth_pass password}track_script {check_nginx}
}
- web02
[root@my-web02 ~]$ cat /etc/keepalived/keepalived.conf
! Configuration File for keepalivedvrrp_script check_nginx {script "killall -0 nginx"interval 2
}vrrp_instance VI_1 {interface ens33state BACKUPpriority 100virtual_router_id 33virtual_ipaddress {192.168.157.30}authentication {auth_type PASSauth_pass password}track_script {check_nginx}
}
啟動服務
nginx
# 所有節點
systemctl start nginx
keepalived
# 所有節點
systemctl start nginx
服務驗證
查看 VIP 狀態
# web01 -- 獲取 VIP
[root@my-web01 ~]$ ip addr | grep '192.168.157'inet 192.168.157.31/24 brd 192.168.157.255 scope global ens33inet 192.168.157.30/32 scope global ens33# web01 -- 未獲取 VIP
[root@my-web02 ~]$ ip addr | grep '192.168.157'inet 192.168.157.32/24 brd 192.168.157.255 scope global ens33
CURL 命令訪問
# VIP -- 訪問到 web01
[root@my-web01 ~]$ curl 192.168.157.30
<!DOCTYPE html>
<h1>my web01 ~~~</h1># web01
[root@my-web01 ~]$ curl 192.168.157.31
<!DOCTYPE html>
<h1>my web01 ~~~</h1># web02
[root@my-web01 ~]$ curl 192.168.157.32
<!DOCTYPE html>
<h1>my web02 ~~~</h1>
瀏覽器訪問
- vip && web01
- vip && web02
高可用驗證
停止 web01 下 Nginx
- 停止服務
# web01
[root@my-web01 ~]$ systemctl stop nginx
- 驗證 VIP
# web01 -- VIP 漂移
[root@my-web01 ~]$ ip addr | grep '192.168.157'inet 192.168.157.31/24 brd 192.168.157.255 scope global ens33# web02 -- 獲取 VIP
[root@my-web02 ~]$ ip addr | grep '192.168.157'inet 192.168.157.32/24 brd 192.168.157.255 scope global ens33inet 192.168.157.30/32 scope global ens33
- 瀏覽器訪問
恢復 web01 下 Nginx
- 恢復服務
[root@my-web01 ~]$ systemctl start nginx
- 驗證 VIP
# web01 -- 獲取 vip
[root@my-web01 ~]$ ip addr | grep '192.168.157'inet 192.168.157.31/24 brd 192.168.157.255 scope global ens33inet 192.168.157.30/32 scope global ens33# web02 -- vip 漂移
[root@my-web02 ~]$ ip addr | grep '192.168.157'inet 192.168.157.32/24 brd 192.168.157.255 scope global ens33
參考
- Keepalived + nginx 實現高可用