環(huán)境準(zhǔn)備
IP | VIP | 環(huán)境 | |
客戶端 | 192.168.134.174 | ||
Master | 192.168.134.170 | 192.168.134.100 | 需要配置nginx負(fù)載均衡 |
Backup | 192.168.134.172 | 192.168.134.100 | 需要配置nginx負(fù)載均衡 |
web1服務(wù)器 | 192.168.134.171 | ||
web2服務(wù)器 |
192.168.134.173 |
1、首先安裝nginx服務(wù)器(此處采用yum安裝)
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
yum install nginx -y
2、修改nginx的配置文件(配置負(fù)載均衡)
cd /etc/nginx/conf.d/
vim web.conf
upstream webPools {
server 192.168.134.171;
server 192.168.134.173;
}
server {
location / {
proxy_pass http://webPools;
}
}
3、啟動(dòng)nginx
systemctl start nginx
4、修改keepalived配置文件
主節(jié)點(diǎn):
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL1
}
vrrp_instance VI_1 {
state MASTER
# nopreempt
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.134.100
}
}
備節(jié)點(diǎn)
修改:
state MASTER
priority 80
此時(shí)可以實(shí)現(xiàn)keepalived的故障切換和nginx負(fù)載均衡,但是如果nginx的主服務(wù)器出現(xiàn)故障,那么此時(shí)無(wú)法實(shí)現(xiàn)客戶端的正常訪問(wèn),即需要新的配置來(lái)實(shí)現(xiàn)高可用,因此,利用·vrrp-script(利用VIP漂移實(shí)現(xiàn)服務(wù)的可用)去監(jiān)控集群資源。
5、重新修改keepalived配置文件,添加vrrp-script,在實(shí)例中還要調(diào)用。
主備節(jié)點(diǎn)做一樣的修改
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL1
}
vrrp_script check_nginx { # 自定義資源監(jiān)控腳本
script "killall -0 nginx"
interval 2
}
vrrp_instance VI_1 {
state MASTER
# nopreempt
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check__nginx
}
virtual_ipaddress {
192.168.134.100
}
}
6、還可以用腳本檢測(cè)服務(wù)的方法
cat check_#!/bin/bash
total=$(ps -C nginx --no-header | wc -l)
if [ $total -eq 0 ]
then
nginx_status=1
else
nginx_status=0
fi
exit $nginx_status
在keepalived文件中也進(jìn)行修改
vrrp_script check_nginx {
#script "killall -0 nginx"
script "/etc/keepalived/check_nginx.sh"
interval 2
}
7、還有一種使用notify的·方法文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-646255.html
寫(xiě)notify.sh腳本
#!/bin/bash
case "$1" in
master)
nmap localhost -p 80 | grep "80/tcp open"
if [ $? -ne 0 ];then
systemctl start nginx
fi
;;
backup)
nginx_psr=`ps -C nginx --no-header | wc -l`
if [ $nginx_psr -ne 0 ];then
systemctl stop nginx
fi
;;
*)
echo "Usage:$0 master|backup"
;;
esac
chmod +x notify.sh # 給腳本增加執(zhí)行權(quán)限
然后在keepalived文件中修改就行,修改下面幾個(gè)地方
vrrp_script check_nginx {
#script "killall -0 nginx"
script "/etc/keepalived/check_nginx.sh"
interval 2
weight -30
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.134.100
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
}
8、測(cè)試如下:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-646255.html
在停掉nginx后,仍舊可以正常訪問(wèn)
systemctl stop nginx
結(jié)果如下:
web test page ,ip is 192.168.134.173 192.168.122.1
web test page ,ip is 192.168.134.171 192.168.122.1
web test page ,ip is 192.168.134.173 192.168.122.1
web test page ,ip is 192.168.134.173 192.168.122.1
web test page ,ip is 192.168.134.171 192.168.122.1
web test page ,ip is 192.168.134.171 192.168.122.1
到了這里,關(guān)于nginx+keepalived實(shí)現(xiàn)負(fù)載均衡和高可用的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!