keepalived(四)Keepalived+Nginx
實驗
架構圖
內容
Keepalived+Nginx實現Nginx負載均衡器的高可用,Nginx反向代理後端的web服務。但是Keepalived對Nginx的健康檢查需要自定義vrrp指令碼。
vrrp指令碼的定義
vrrp_script <SCRIPT_NAME> {
script <STRING>|<QUOTED-STRING> #要執行的命令或指令碼路徑
interval <INTEGER> #指令碼呼叫時間間隔,預設1秒
timeout <INTEGER> #指令碼執行等待超時時長,超過該時長,表示指令碼執行失敗
weight <INTEGER:-254..254> #根據該權重值調整vrrp例項優先順序值,預設值為0
rise <INTEGER> #需要成功多少次,vrrp才進行角色狀態切換
fall <INTEGER> #需要失敗多少次,vrrp才進行角色狀態切換
}
vrrp監控
#在vrrp_instance內設定
#監控介面,當某介面down掉時,切換為fault狀態
track_interface {
ens33
}
#監控指令碼,即執行vrrp_script定義的指令碼
track_script {
<SCRIPT_NAME>
}
環境
ip | 系統 | 軟體 | 角色 |
---|---|---|---|
192.168.253.128 | CentOS7 | Keepalived,Nginx | MASTER節點,web伺服器 |
192.168.253.158 | CentOS7 | Keepalived,Nginx | BACKUP節點,web伺服器 |
第一步:192.168.253.128和192.168.253.158上準備好nginx的web服務以及負載均衡
192.168.253.128和192.168.253.158上都執行下列操作
systemctl stop firewalld
setenforce 0
#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo如果已經有epel源不需要執行
yum install nginx -y #nginx需要epel源才可以yum安裝
修改配置檔案/etc/nginx/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream backend {
server 192.168.253.128:8080 weight=1;
server 192.168.253.158:8080 weight=3;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
server {
listen 8080;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
第二步:設定各自的首頁,方便區分
192.168.253.128主機上
echo "192.168.253.128:8080" > /usr/share/nginx/html/index.html
192.168.253.158主機上
echo "192.168.253.158:8080" > /usr/share/nginx/html/index.html
第三步:測試兩臺負載均衡器是否正常
啟動nginx
systemctl start nginx
192.168.253.128這臺負載均衡器沒問題,權重也是1:3
192.168.253.158這臺負載均衡器沒問題,權重也是1:3
第三步:192.168.253.128上安裝keepalived和修改配置檔案
yum install keepalived psmisc -y
修改配置檔案 /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id 192.168.253.128
}
vrrp_script chk_nginx {
script "killall -0 nginx && exit 0 || exit 1" #如果存在nginx程式就正常退出,否則返回1(只有返回0才是成功)
interval 1 #指令碼呼叫時間間隔,1s
weight -10 #權重降低10
fall 2 #需要失敗2次,vrrp才進行角色狀態切換
rise 2 #需要成功2次,vrrp才進行角色狀態切換
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 15
priority 95
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
192.168.253.190/32 dev ens33
}
track_script {
chk_nginx ##監控chk_nginx
}
notify_master "systemctl start nginx" #狀態變為MASTER時,觸發的指令碼
notify_backup "systemctl start nginx" #狀態變為MASTER時,觸發的指令碼
notify_fault "systemctl start nginx" #狀態變為fault,觸發的指令碼
}
第四步:在192.168.253.158上安裝keepalived和修改配置檔案
yum install keepalived psmisc -y
修改配置檔案/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id 192.168.253.158
}
vrrp_script chk_nginx {
script "killall -0 nginx && exit 0 || exit 1"
interval 1
weight -10
fall 2
rise 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 15
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
192.168.253.190/32 dev ens33
}
track_script {
chk_nginx
}
notify_master "systemctl start nginx"
notify_backup "systemctl start nginx"
notify_fault "systemctl start nginx"
}
第五步:測試keepalived的健康檢查
兩臺主機執行keepalived
systemctl start keepalived
抓包分析
tcpdump -i ens33 -nn -p vrrp
這裡可以看到192.168.253.128是MASTER節點
這時候我們down掉nginx,看192.168.253.128是否會優先順序降低和重啟nginx
這裡可以看到192.168.253.128的優先順序已經下降了。而且192.168.253.158也成為了MASTER節點
而且nginx也重新啟動成功了
修復成功之後,192.168.253.128的優先順序就上升了於是主節點從192.168.253.158變成192.168.253.128
第六步:測試nginx的負載均衡
for i in {1..15} ; do curl 192.168.253.190 ; done
這時候我們down掉192.168.253.128的keepalived和nginx的負載均衡功能
systemctl stop keepalived
iptables -A INPUT -p tcp --dport 80 -j REJECT #訪問80埠的tcp協議全部拒絕,也就是阻斷了192.168.253.128的負載均衡功能
然後再訪問vip看是否正常
可以看到我們down了一臺nginx負載均衡器和一臺keepalived,但是後端的web服務沒有受到影響。
相關文章
- Keepalived+Nginx高可用叢集搭建筆記Nginx筆記
- Keepalived+Nginx高可用案例(搶佔式與非搶佔式)Nginx
- LVS#MySQL+Keepalived四層負載均衡MySql負載
- keepalived
- mysql with keepalivedMySql
- Keepalived - Keepalived 實現 tomcat雙機熱備Tomcat
- Keepalived 高可用
- keepalived安裝
- keepalived(二)vip漂移
- Keepalived 使用指南
- openGauss+KeepAlived
- keepalived 安裝部署
- Keepalived 原理與實戰
- nginx+keepalived高可用Nginx
- RabbitMQ+haProxy+keepalived使用MQ
- Keepalived服務詳解
- Keepalived 高可用詳解
- 負載均衡之keepalived負載
- Nginx教程以及配置keepalivedNginx
- mysql + keepalived 安裝配置MySql
- mysql高可用之keepalivedMySql
- keepalived之 Keepalived 原理(定義、VRRP 協議、VRRP 工作機制)VR協議
- KeepAlived+LVS+NginxNginx
- Keepalived部署與配置詳解
- mysql高可用衡搭建(Keepalived)MySql
- keepalived + nginx 實現高可用Nginx
- nginx實現keepalived高可用Nginx
- keepalived VRRP同步組配置VR
- Keepalived 架構高可用 Mysql架構MySql
- nginx keepalived 主從切換Nginx
- keepalived高可用負載均衡負載
- keepalived之 ipvsadm-1.26-4(lvs)+ keepalived-1.2.24 安裝
- keepalived+MySQL實現高可用MySql
- Nginx&Keepalived 實現高可用Nginx
- Keepalived實現服務高可用
- WEB服務-Nginx之十-keepalivedWebNginx
- keepalived配置redis主從切換Redis
- MySQL主主模式+Keepalived高可用MySql模式