目錄
- Keepalived實現(xiàn)Nginx高可用
- Master上的keepalived.conf
- Backup上的keepalived.conf
- Nginx檢測腳本check_nginx.sh
- Keepalived+Nginx高可用集群
Keepalived實現(xiàn)Nginx高可用
Keepalived安裝可參考Mysql+Keepalived實現(xiàn)雙主熱備
Master上的keepalived.conf
global_defs { ?? ?router_id LVS_LEVEL1?? ?#主服務(wù)器名稱 } ? vrrp_script check_run { ? ?script "/usr/local/src/check_nginx.sh" ? ?interval 5?? ??? ??? ??? ?#5秒執(zhí)行一次腳本 } ? vrrp_instance VI_1 { ? ? state MASTER?? ??? ??? ?#主服務(wù)器 ? ? interface eth0?? ??? ??? ?#承載VIP地址到物理接口 ? ? virtual_router_id 51?? ?#虛擬路由器ID號,每個熱播組保持一致 ? ? priority 100?? ??? ??? ?#優(yōu)先級,數(shù)值越大優(yōu)先級越高 ? ? advert_int 1?? ??? ??? ?#檢查間隔,默認為1s ? ? authentication {?? ??? ?#認證信息,每個熱播組保持一致 ? ? ? ? auth_type PASS ? ? ?#認證類型 ? ? ? ? auth_pass 1111?? ??? ?#密碼字串 ? ? } ? ? virtual_ipaddress { ? ? ? ? 192.168.0.200?? ??? ?#VIP地址(內(nèi)網(wǎng)地址) ? ? } ? ? track_script { ? ? ? ? check_run ? ? } }
Backup上的keepalived.conf
global_defs { ?? ?router_id LVS_LEVEL2?? ?#備份服務(wù)器名稱 } vrrp_script check_run { ?? ?script "/usr/local/src/check_nginx.sh" ?? ?interval 5?? ??? ??? ??? ?#5秒執(zhí)行一次腳本 } vrrp_instance VI_1 { ? ? state BACKUP?? ??? ??? ?#備份服務(wù)器 ? ? interface eth0?? ??? ??? ?#承載VIP地址到物理接口 ? ? virtual_router_id 51?? ?#虛擬路由器ID號,每個熱播組保持一致 ? ? priority 50?? ??? ??? ??? ?#優(yōu)先級,數(shù)值越大優(yōu)先級越高 ? ? advert_int 1?? ??? ??? ?#檢查間隔,默認為1s ? ? authentication {?? ??? ?#認證信息,每個熱播組保持一致 ? ? ? ? auth_type PASS ? ? ?#認證類型 ? ? ? ? auth_pass 1111?? ??? ?#密碼字串 ? ? } ? ? virtual_ipaddress { ? ? ? ? 192.168.0.200 ? ? ? #VIP地址(和主服務(wù)器設(shè)置一樣) ? ? } ? ? 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高可用集群
實驗環(huán)境
準備2臺設(shè)備
設(shè)備1 192.168.217.11 nginx +keepalived
設(shè)備2 192.168.217.12 nginx +keepalived
虛擬ip 192.168.217.3
設(shè)備1、2 安裝nginx keepalived
(此處設(shè)備已安裝nginx)
我們在此基礎(chǔ)上直接利用yum安裝keepalived
更新網(wǎng)絡(luò)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 ? ? ? ? ? ?#主調(diào)度器的初始角色(本實驗主備MASTER 從BACKUP) ? ? interface ens33 ? ? ? ? ? ?#修改網(wǎng)卡名稱 ? ? virtual_router_id 52 ? ? ?#主id 與從id ?不要重復(fù) ? ?? ? ? priority 90 ? ? ? ? ? ? ? ? #主調(diào)度器的選舉優(yōu)先級 ? (本實驗 ?主備100 ?從90 ?數(shù)據(jù)越大 優(yōu)先級越高) ? ? advert_int 1 ? ? ? ? ? ? ? ? authentication { ? ? ? ? auth_type PASS ? ? ? ? auth_pass 1111 ? ? } ? ? virtual_ipaddress { ? ? ? ? 192.168.217.3 ? ? ? ? ? ? ? ? ? ? ?#虛擬ip ? (本實驗需設(shè)置 ?同網(wǎng)段 ?主從一樣) ? ? } }
查看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
關(guān)閉防火墻 內(nèi)核
[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'
實驗環(huán)境
準備2臺設(shè)備 雙主keepalived
設(shè)備1 192.168.217.11 nginx +keepalived
設(shè)備2 192.168.217.12 nginx +keepalived
虛擬ip 192.168.217.3
虛擬ip 192.168.217.6
在以上實驗基礎(chǔ)上
設(shè)備1
[root@localhost ~]# vim /etc/keepalived/keepalived.conf vrrp_instance VI_1 { #修改模塊名字 state MASTER #主調(diào)度器的初始角色(本實驗主備MASTER 從BACKUP) interface ens33 #修改網(wǎng)卡名稱 virtual_router_id 51 #主id 與從id 不要重復(fù) priority 100 #主調(diào)度器的選舉優(yōu)先級 (本實驗 主備100 從90 數(shù)據(jù)越大 優(yōu)先級越高) 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}
設(shè)備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
設(shè)備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
設(shè)備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
設(shè)備1、設(shè)備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; 連接超時
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持。