首頁 > 軟體

Keepalived如何實現Nginx高可用

2022-11-01 14:03:52

Keepalived實現Nginx高可用

Keepalived安裝可參考Mysql+Keepalived實現雙主熱備

Master上的keepalived.conf

global_defs {
    router_id LVS_LEVEL1    #主伺服器名稱
}
 
vrrp_script check_run {
   script "/usr/local/src/check_nginx.sh"
   interval 5                #5秒執行一次指令碼
}
 
vrrp_instance VI_1 {
    state MASTER            #主伺服器
    interface eth0            #承載VIP地址到物理介面
    virtual_router_id 51    #虛擬路由器ID號,每個熱播組保持一致
    priority 100            #優先順序,數值越大優先順序越高
    advert_int 1            #檢查間隔,預設為1s
    authentication {        #認證資訊,每個熱播組保持一致
        auth_type PASS      #認證型別
        auth_pass 1111        #密碼字串
    }
    virtual_ipaddress {
        192.168.0.200        #VIP地址(內網地址)
    }
    track_script {
        check_run
    }
}

Backup上的keepalived.conf

global_defs {
    router_id LVS_LEVEL2    #備份伺服器名稱
}
vrrp_script check_run {
    script "/usr/local/src/check_nginx.sh"
    interval 5                #5秒執行一次指令碼
}
vrrp_instance VI_1 {
    state BACKUP            #備份伺服器
    interface eth0            #承載VIP地址到物理介面
    virtual_router_id 51    #虛擬路由器ID號,每個熱播組保持一致
    priority 50                #優先順序,數值越大優先順序越高
    advert_int 1            #檢查間隔,預設為1s
    authentication {        #認證資訊,每個熱播組保持一致
        auth_type PASS      #認證型別
        auth_pass 1111        #密碼字串
    }
    virtual_ipaddress {
        192.168.0.200       #VIP地址(和主伺服器設定一樣)
    }
    track_script {
        check_run
    }
}

Nginx檢測指令碼check_nginx.sh

#!/bin/bash
 
A=`ps -C nginx --no-header |wc -l`
#判斷nginx是否宕機,如果宕機,嘗試重啟
if [ $A -eq 0 ];then
    /usr/local/nginx/sbin/nginx
    #等待一會再次檢查nginx,如果沒有啟動成功,則停止keepalived,使其啟動備用機
    sleep 5
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
    fi
fi
chmod +x /etc/keepalived/nginx_check.sh

Keepalived+Nginx高可用叢集

實驗環境

準備2臺裝置

裝置1 192.168.217.11 nginx +keepalived

裝置2 192.168.217.12 nginx +keepalived

虛擬ip 192.168.217.3

裝置1、2 安裝nginx keepalived

(此處裝置已安裝nginx)

我們在此基礎上直接利用yum安裝keepalived

更新網路yum源

[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
--2022-06-11 17:56:31--  http://mirrors.aliyun.com/repo/epel-7.repo

安裝keepalived

[root@localhost ~]# yum -y install keepalived.x86_64 

啟動nginx

[root@localhost ~]# cd /usr/src/nginx-1.12.2/
[root@localhost nginx-1.12.2]# killall -9 nginx
[root@localhost nginx-1.12.2]# nginx

修改keepalived組態檔

[root@localhost nginx-1.12.2]# vim /etc/keepalived/keepalived.conf 
vrrp_instance VI_1 {   
    state BACKUP            #主排程器的初始角色(本實驗主備MASTER 從BACKUP)
    interface ens33            #修改網路卡名稱
    virtual_router_id 52      #主id 與從id  不要重複     
    priority 90                 #主排程器的選舉優先順序   (本實驗  主備100  從90  資料越大 優先順序越高)
    advert_int 1            
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.217.3                      #虛擬ip   (本實驗需設定  同網段  主從一樣)
    }
}

檢視ip

[root@localhost ~]# ip a
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:c2:15:cf brd ff:ff:ff:ff:ff:ff
    inet 192.168.217.11/24 brd 192.168.217.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.217.3/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::1e6f:d3ee:5554:1f34/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::ac8:77ad:9154:7983/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

重啟keepalived

[root@localhost ~]# systemctl start keepalived.service
[root@localhost ~]# systemctl restart keepalived.service

關閉防火牆 核心

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

驗證

[root@localhost ~]# curl 192.168.217.11
‘nginx1'
[root@localhost ~]# curl 192.168.217.12
‘nginx2'
[root@localhost ~]# curl 192.168.217.3
‘nginx1'
[root@localhost ~]# curl 192.168.217.3
‘nginx1'

實驗環境 

準備2臺裝置 雙主keepalived

裝置1 192.168.217.11 nginx +keepalived

裝置2 192.168.217.12 nginx +keepalived

虛擬ip 192.168.217.3

虛擬ip 192.168.217.6

在以上實驗基礎上

裝置1

[root@localhost ~]# vim /etc/keepalived/keepalived.conf 
vrrp_instance VI_1 {     #修改模組名字
    state MASTER     #主排程器的初始角色(本實驗主備MASTER 從BACKUP)
    interface ens33      #修改網路卡名稱
    virtual_router_id 51       #主id 與從id  不要重複     
    priority 100           #主排程器的選舉優先順序   (本實驗  主備100  從90  資料越大 優先順序越高)
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.217.3            #虛擬ip   
    }
}


vrrp_instance VI_2 {
    state BACKUP
    interface ens33
    virtual_router_id 53
    priority 90	
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.217.6              
    }
I}

裝置2

[root@localhost ~]# vim /etc/keepalived/keepalived.conf 
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.217.3
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface ens33
    virtual_router_id 53
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.217.6
    }
}

xshell同時開啟 命令模式 重啟keepalived

[root@localhost ~]# systemctl start keepalived.service
[root@localhost ~]# systemctl restart keepalived.service

檢視ip

裝置1 飄逸Ip正常

[root@localhost ~]# ip a
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:c2:15:cf brd ff:ff:ff:ff:ff:ff
    inet 192.168.217.11/24 brd 192.168.217.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.217.3/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::1e6f:d3ee:5554:1f34/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::ac8:77ad:9154:7983/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

裝置2

[root@localhost ~]# ip a
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:49:b3:a1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.217.12/24 brd 192.168.217.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.217.6/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::1e6f:d3ee:5554:1f34/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

裝置1、裝置2驗證 (此問題暫未解決)

[root@localhost ~]# curl 192.168.217.11
curl: (7) Failed connect to 192.168.217.11:80; 拒絕連線
[root@localhost ~]# curl 192.168.217.12
‘nginx2'
[root@localhost ~]# curl 192.168.217.3
curl: (7) Failed connect to 192.168.217.3:80; 連線超時
[root@localhost ~]# curl 192.168.217.6
curl: (7) Failed connect to 192.168.217.6:80; 連線超時

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com