您好,登錄后才能下訂單哦!
下文給大家帶來基于Linux Centos7下 keepalived + nginx負載均衡的一些問題解答,希望能夠給大家在實際運用中帶來一定的幫助,負載均衡涉及的東西比較多,理論也不多,網上有很多書籍,今天我們就用億速云在行業內累計的經驗來做一個解答。
首先需要云服務器關都閉防火墻和selinux
1,準備四臺nginx云服務器,兩臺做代理,兩臺做后端真實云服務器。
2,配置兩臺真實云服務器
第一臺:
[root@vm-4 ~]# systemctl start nginx
[root@vm-4 ~]# systemctl enable nginx
[root@vm-4 conf.d]# echo "server111" > /usr/share/nginx/html/index.html
測試:
[root@vm-4 conf.d]# curl localhost
server111
第二臺:
[root@vm-5 ~]# systemctl start nginx
[root@vm-5 ~]# systemctl enable nginx
[root@vm-5 conf.d]# echo "server222" > /usr/share/nginx/html/index.html
測試:
[root@vm-5 conf.d]# curl localhost
server222
3,配置兩臺nginx代理云服務器
[root@vm-2 ~]# systemctl start nginx
[root@vm-2 ~]# systemctl enable nginx
[root@vm-2 ~]# cd /etc/nginx/conf.d/
[root@vm-2 conf.d]# cp default.conf proxy.conf
[root@vm-2 conf.d]# mv default.conf default.conf.bak
[root@vm-2 conf.d]# vi upstream.conf
upstream nginx_pool {
server 10.30.161.241:80 weight=1 max_fails=2 fail_timeout=2;
server 10.30.161.242:80 weight=1 max_fails=2 fail_timeout=2;
}
[root@vm-2 conf.d]# vi proxy.conf
location / {
proxy_pass http://nginx_pool;
proxy_redirect default;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
將nginx的配置文件拷貝到另一臺代理云服務器中:
[root@vm-2 conf.d]# scp proxy.conf upstream.conf 10.30.161.214:/etc/nginx/conf.d/
分別重啟nginx:
systemctl restart nginx
4, 兩臺代理云服務器分別安裝軟件
master:
[root@vm-2 conf.d]# yum install -y keepalived
[root@vm-2 conf.d]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@vm-2 conf.d]# vim /etc/keepalived/keepalived.conf
配置keepalived,將里面內容全部刪除,添加下列內容:
! Configuration File for keepalived
global_defs {
router_id director1 #輔助改為director2
}
vrrp_instance VI_1 {
state MASTER #定義主還是備
interface ens33 #VIP綁定接口
virtual_router_id 80 #整個集群的調度器一致
priority 100 #back改為50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.30.161.200/24 #設置VIP
}
}
backup:
[root@yum_nginx ~]# cp /etc/keepalived/keepalived.conf
[root@yum_nginx ~]# vi /etc/keepalived/keepalived.conf
配置keepalived,將里面內容全部刪除,添加下列內容:
! Configuration File for keepalived
global_defs {
router_id directory2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
nopreempt
virtual_router_id 80
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.30.161.200/24
}
}
5、兩臺代理服務器分別啟動keepalived
[root@yum_nginx ~]# systemctl enable keepalived.service
[root@yum_nginx ~]# systemctl start keepalived.service
6、檢測是否成功
在master上 ip a 可顯示VIP為10.30.161.200/24
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:29:a6:12 brd ff:ff:ff:ff:ff:ff
inet 10.30.161.51/24 brd 10.30.161.255 scope global dynamic ens33
valid_lft 168379sec preferred_lft 168379sec
inet 10.30.161.200/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::8cfe:8d87:6478:baee/64 scope link
valid_lft forever preferred_lft forever
inet6 fe80::d9c7:8228:5b45:afed/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
用curl訪問VIP,正常輪詢
[root@vm-5 conf.d]# curl 10.30.161.200
server111
[root@vm-5 conf.d]# curl 10.30.161.200
server222
[root@vm-5 conf.d]# curl 10.30.161.200
server111
[root@vm-5 conf.d]# curl 10.30.161.200
server222
測試當一臺master故障
[root@vm-2 conf.d]# systemctl stop keepalived
ip a 發現VIP偏移到backup代理服務器上
到此:
可以解決心跳故障keepalived
但不能解決Nginx服務故障
擴展對調度器Nginx健康檢查(可選)兩臺都設置
思路:
讓Keepalived以一定時間間隔執行一個外部腳本,腳本的功能是當Nginx失敗,則關閉本機的Keepalived
(1) script
[root@nginx-proxy-master ~]# vim /etc/keepalived/check_nginx_status.sh
#!/bin/bash
#+檢查nginx進程是否存在
counter=$(ps -C nginx --no-heading|wc -l) #此行有服務名
if [ "${counter}" = "0" ]; then
#嘗試啟動一次nginx,停止5秒后再次檢測
systemctl start nginx #啟動服務
sleep 5
counter=$(ps -C nginx --no-heading|wc -l) #此行有服務名
if [ "${counter}" = "0" ]; then
#如果啟動沒成功,就殺掉keepalive觸發主備切換
service keepalived stop
fi
fi
[root@nginx-proxy-master ~]# chmod a+x /etc/keepalived/check_nginx_status.sh
(2). keepalived使用script
! Configuration File for keepalived
global_defs {
router_id director1
}
vrrp_script check_nginx { #健康檢測模塊調用
script "/etc/keepalived/check_nginx_status.sh" #指定腳本
interval 5 #檢查頻率,秒
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 80
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.246.16/24
}
track_script { 引用腳本
check_nginx
}
}
注:必須先啟動nginx,再啟動keepalived
看了以上關于基于Linux Centos7下 keepalived + nginx負載均衡的一些問題解答,如果大家還有什么地方需要了解的可以在億速云行業資訊里查找自己感興趣的或者找我們的專業技術工程師解答的,億速云技術工程師在行業內擁有十幾年的經驗了。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。