1. 程式人生 > 其它 >Linux - Keepalived + Nginx(雙主)

Linux - Keepalived + Nginx(雙主)

客戶端 cli.noise.org : 10.0.0.7 - cli.noise.org

keepalived1 + nginx - 10.0.0.34 -ka1.noise.org

keepalived2 + ngixn -10.0.0.22 -ka2.noise.org

後臺web伺服器1: 10.0.0.35 - rs1.noise.org

後臺web伺服器2: 10.0.0.36 - rs2.noise.org

==========================================================

ka1.noise.org - keepalived.conf

  
global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from [email protected]
   smtp_server 
127.0.0.1 smtp_connect_timeout 30 router_id ka1.noise.org       -》 唯一ID vrrp_mcast_group 224.0.100.100 -》 多播地址 } vrrp_script check_nginx { script "/data/check_nginx.sh"  -》 檢查指令碼,nginx服務掛掉,自動重啟 interval 1 weight -30 fall 3 rise 5 timeout 2 } vrrp_instance VI_1 {      -》 叢集VI_1, ka1為主節點 state MASTER        -》 節點狀態 interface eth0 virtual_router_id
66    -》 唯一叢集ID #nopreempt priority 100        -》 節點優先順序 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 10.0.0.20/24 dev eth0 label eth0:1  -》 叢集漂移VIP } track_interface { eth0 } track_script { check_nginx } } vrrp_instance VI_2 {  -》 叢集VI_2 state BACKUP      -> 當前節點作為子節點 interface eth0 virtual_router_id
88  -》 唯一叢集ID #nopreempt priority 80      -》 節點優先順序 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 10.0.0.30/24 dev eth0 label eth0:1  -》 叢集VIP } track_interface { eth0 } track_script { check_nginx } }

===============================================

此為ka2.noise.org的配置,部分配置與ka1.noise.org相反

! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id ka2.noise.org vrrp_mcast_group 224.0.100.100 } vrrp_script check_nginx { script "/data/check_nginx.sh" interval 1 weight -30 fall 3 rise 5 timeout 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 66 #nopreempt priority 80 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 10.0.0.20/24 dev eth0 label eth0:1 } track_interface { eth0 } track_script { check_nginx } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 88 #nopreempt priority 100 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 10.0.0.30/24 dev eth0 label eth0:1 } track_interface { eth0 } track_script { check_nginx } }

===================================

兩個ka節點nginx配置一樣,負責反向代理

http { upstream websrvs { server
10.0.0.35:80 weight=1; server 10.0.0.36:80 weight=1; } server { listen 80; location / { proxy_pass http://websrvs/; } } }

==================================

[19:07:33 root@ka1 ~]#cat /data/check_nginx.sh 
#!/bin/bash
#
#**************************************************************************************
#Author:                                   Noise Lys
#QQ:                                       578110218
#Date:                                     2021-08-29
#Filename:                                 /data/check_nginx.sh
#URL:                                      https://www.cnblogs.com/noise/
#Description:                              The test script
#Copyright (C):                            2021 All rights reserved
#**************************************************************************************
/usr/bin/killall -0 nginx || systemctl restart nginx 

=================================

開始檢查結果,訪問10.0.0.20即叢集VI_1的VIP時,主節點ka1.noise.org工作,ka2.noise.org不工作
但是訪問10.0.0.30即叢集VI_2的VIP時,主節點ka2.noise.org工作,ka1.noise.org不工作
[19:00:48 root@cli ~]#curl 10.0.0.20
rs1.noise.org
[19:00:49 root@cli ~]#curl 10.0.0.20
rs2.noise.org
[19:00:49 root@cli ~]#curl 10.0.0.20
rs1.noise.org
[19:00:49 root@cli ~]#curl 10.0.0.20
rs2.noise.org
[19:00:50 root@cli ~]#curl 10.0.0.20
rs1.noise.org
[19:00:50 root@cli ~]#curl 10.0.0.30
rs1.noise.org
[19:00:52 root@cli ~]#curl 10.0.0.30
rs2.noise.org
[19:00:53 root@cli ~]#curl 10.0.0.30
rs1.noise.org
[19:00:53 root@cli ~]#curl 10.0.0.30
rs2.noise.org