配置Keepalive實現nginx高可用
阿新 • • 發佈:2018-11-07
搭建環境
- 已關閉防火牆,關閉selinux;
- 所用伺服器IP地址如下:
IP地址 | 伺服器 | 服務 |
---|---|---|
192.168.91.129 | master | keepalived、nginx |
192.168.91.130 | backup | keepalived、nginx |
192.168.91.254(VIP) | master、backup | ---------------------- |
搭建步驟
配置keepalived
- 配置192.168.91.129和192.168.91.130網路源
[[email protected] ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[[email protected] ~]# sed -i 's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo
[[email protected] ~]# yum -y install epel-release
[ [email protected] ~]# yum -y install keepalived nginx
[[email protected] ~]# scp /etc/yum.repo/CentOS-Base.repo [email protected]:/etc/yum.repo.d/
[[email protected] ~]# yum -y install epel-release
[[email protected] ~]# yum -y install keepalived nginx
- 啟動192.168.91.129和192.168.91.130的nginx服務,並檢視埠
[[email protected] ~]# systemctl start nginx
[[email protected] ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 :::80 :::*
-
在瀏覽器上進行訪問
- 192.168.91.129(master)上nginx的的測試頁面
- 192.168.91.130(backup)上nginx的測試頁面
- 192.168.91.129(master)上nginx的的測試頁面
-
配置192.168.91.129(master)的/etc/keepalived/keepalived.conf配置檔案
[[email protected] ~]# cp -a /etc/keepalived/keepalived.conf{,.bak}
[[email protected] ~]# cat > /etc/keepalived/keepalived.conf << EOF
! Configuration File for keepalived
global_defs {
router_id 001
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.91.254
}
}
virtual_server 192.168.91.254 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.91.129 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.91.130 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
EOF
- 配置192.168.91.129(backup)的/etc/keepalived/keepalived.conf配置檔案(注:route_id不同)
[[email protected] ~]# cat > /etc/keepalived/keepalived.conf << EOF
! Configuration File for keepalived
global_defs {
router_id 002
}
vrrp_instance VI_1 {
state BACKUP ##修改初始狀態為BACKUP
interface eno16777736
virtual_router_id 51
priority 90 ##低於MASTER優先順序
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.91.254
}
}
virtual_server 192.168.91.254 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.91.129 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.91.130 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
EOF
- 啟動master和backup上的keepalived服務,並設定開機自動啟動
[[email protected] ~]# systemctl start keepalived
[[email protected] ~]# systemctl enable keepalived
ln -s '/usr/lib/systemd/system/keepalived.service' '/etc/systemd/system/multi-user.target.wants/keepalived.service'
- 檢視虛擬IP地址在哪個伺服器上
## master上的IP地址 ##
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:47:34:cd brd ff:ff:ff:ff:ff:ff
inet 192.168.91.129/24 brd 192.168.91.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet 192.168.91.254/32 scope global eno16777736
valid_lft forever preferred_lft forever
## backup上的IP地址 ##
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:39:25:f5 brd ff:ff:ff:ff:ff:ff
inet 192.168.91.130/24 brd 192.168.91.255 scope global eno16777736
valid_lft forever preferred_lft forever
指令碼監控nginx
讓keepalived監控nginx負載均衡機
- 在192.168.91.129伺服器(master)建立指令碼存放位置,並編寫指令碼
[[email protected] ~]# mkdir /etc/keepalived/scripts
[[email protected] ~]# vim /etc/keepalived/scripts/check_n.sh
#!/bin/bash
nginx_status=$(ps aux | grep -Ev "grep|$0" | grep '\bnginx\b' | wc -l)
if [ $nginx_status -lt 1 ];then
systemctl stop keepalived
fi
[[email protected] ~]# chmod +x /etc/keepalived/scripts/check_n.sh
[[email protected] ~]# ll /etc/keepalived/scripts/check_n.sh
-rwxr-xr-x. 1 root root 146 Nov 1 02:45 /etc/keepalived/scripts/check_n.sh
[[email protected] ~]# vim /etc/keepalived/scripts/notify.sh
#!/bin/bash
VIP=$2
function sendmail () {
subject="${VIP}'s server keepalived state is change."
connect="$(date +'%F %T'): $(hostname)'s state change to master"
echo $connect | mail -s "$subject" <收件人>
}
case "$1" in
master)
nginx_status=$(ps aux | grep -Ev "grep|$0" | grep '\bnginx\b' | wc -l)
if [ $nginx_status -lt 1 ];then
systemctl start nginx
fi
sendmail
;;
backup)
nginx_status=$(ps aux | grep -Ev "grep|$0" | grep '\bnginx\b' | wc -l)
if [ $nginx_status -gt 0 ];then
systemctl stop nginx
fi
sendmail
;;
*)
echo "Usage:$0 master|backup VIP"
;;
esac
[[email protected] ~]# chmod +x /etc/keepalived/scripts/notify.sh
[[email protected] ~]# ll /etc/keepalived/scripts/notify.sh
-rwxr-xr-x. 1 root root 596 Nov 1 02:55 /etc/keepalived/scripts/notify.sh8
- 在192.168.91.130伺服器(backup)建立指令碼存放位置,並編寫指令碼
[[email protected] ~]# mkdir /etc/keepalived/scripts
[[email protected] ~]# vim /etc/keepalived/scripts/notify.sh
#!/bin/bash
VIP=$2
function sendmail () {
subject="${VIP}'s server keepalived state is translate."
content="$(date +'%F %T'): $(hostname)'s state change to master."
echo $connect | mail -s "$subjecj" [email protected]
}
case "$1" in
master)
nginx_status=$(ps aux | grep -Ev "grep|$0" | grep '\bnginx\b' | wc -l)
if [ $nginx_status -lt 1 ];then
systemctl start nginx
fi
sendmail
;;
backup)
nginx_status=$(ps aux | grep -Ev "grep|$0" | grep '\bnginx\b' | wc -l)
if [ $nginx_status -gt 0 ];then
systemctl stop nginx
fi
sendmail
;;
*)
echo "Usage:$0 master|backup VIP"
;;
[[email protected] ~]# chmod +x /etc/keepalived/scripts/notify.sh
[[email protected] ~]# ll /etc/keepalived/scripts/notify.sh
-rwxr-xr-x. 1 root root 604 Nov 1 04:01 /etc/keepalived/scripts/notify.sh
配置keepalived加入監控指令碼的設定
- 配置192.168.91.129(master)的/etc/keepalived/keepalived.conf配置檔案
global_defs {
router_id 002
}
vrrp_script nginx_check {
script "/etc/keepalived/scripts/check_n.sh" ##執行監控指令碼
interval 1 ##1秒鐘執行一次
weight -20
}
......
virtual_ipaddress {
192.168.91.254
}
track_script {
nginx_check
}
## 執行指令碼,傳送郵件報警。當狀態從BACKUP改變為MASTER時傳送郵件 ##
notify_master "/etc/keepalived/scripts/notify.sh master 192.168.91.254"
## 執行指令碼,傳送郵件報警。當狀態從MASTER改變為BACKUP時傳送郵件 ##
notify_backup "/etc/keepalived/scripts/notify.sh backup 192.168.91.254"
......
- 配置192.168.91.130(backup)的/etc/keepalived/keepalived.conf配置檔案
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
......
virtual_ipaddress {
192.168.91.254
}
notify_master "/etc/keepalived/scripts/notify.sh master 192.168.91.254"
notify_backup "/etc/keepalived/scripts/notify.sh backup 192.168.91.254"
......
- 當master這臺伺服器中nginx服務掛掉後,keepalived服務監控到後會將自己停止掉
[[email protected] ~]# ip a
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:47:34:cd brd ff:ff:ff:ff:ff:ff
inet 192.168.91.129/24 brd 192.168.91.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet 192.168.91.254/32 scope global eno16777736
valid_lft forever preferred_lft forever
[[email protected] ~]# ps aux|grep -E "keepalived|nginx"
root 60367 0.0 0.2 121148 2092 ? Ss 12:03 0:00 nginx: master process /usr/sbin/nginx
nginx 60368 0.0 0.3 121612 3312 ? S 12:03 0:00 nginx: worker process
nginx 60369 0.0 0.3 121612 3312 ? S 12:03 0:00 nginx: worker process
root 60376 0.0 0.1 119144 1376 ? Ss 12:03 0:00 /usr/sbin/keepalived -D
root 60377 0.0 0.2 119264 2588 ? S 12:03 0:00 /usr/sbin/keepalived -D
root 60378 0.0 0.2 121240 2464 ? S 12:03 0:00 /usr/sbin/keepalived -D
[[email protected] ~]# systemctl stop nginx
[[email protected] ~]# ps aux|grep -E "keepalived|nginx"
root 65627 0.0 0.0 112640 992 pts/1 S+ 12:16 0:00 grep --color=auto -E keepalived|nginx
- 當master伺服器出現問題後,VIP會跳到BACKUP上,併發送郵件
[[email protected] ~]# ip a
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:39:25:f5 brd ff:ff:ff:ff:ff:ff
inet 192.168.91.130/24 brd 192.168.91.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet 192.168.91.254/32 scope global eno16777736
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe39:25f5/64 scope link
valid_lft forever preferred_lft forever