用keepalived配置高可用,監控NGINX服務

用keepalived配置高可用集羣-1html

背景需求:用keepalived配置高可用,監控NGINX服務,如今企業用NGINX作負載均衡。linux

準備工做:nginx

•設置hostname ,分別爲 master 和backupweb

主上設置 hostnamevim

#hostnamectl set-hostname mastercentos

從上設置 hostname瀏覽器

#hostnamectl set-hostname backupbash

•兩臺機器, 都是 centos7.3,設置網卡 ens33 以下:負載均衡

master 192.168.71.100curl

backup 192.168.71.166

•關閉防火牆

主和從上都操做

# iptables -F 清除規則

# service iptables save 保存規則

主和從都關閉 selinux

# setenforce 0 臨時關閉

# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config 重啓後永久生效

兩臺機器都安裝keepalived,執行yum install -y keepalived

• 兩臺機器都安裝nginx, 執行yum install -y nginx

•  編輯master上keepalived配置文件

• 設定vip爲192.168.71.110

# > /etc/keepalived/keepalived.conf //清空原配置文件內容

# vim /etc/keepalived/keepalived.conf  //加入以下:

global_defs {

notification_email {

aming@aminglinux.com

}

notification_email_from root@aminglinux.com

smtp_server 127.0.0.1

router_id LVS_DEVEL

}

vrrp_script chk_nginx {

script "/usr/local/sbin/check_ng.sh"

interval 3

}

vrrp_instance VI_1 {

state MASTER

interface ens33

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass aminglinux>com

}

virtual_ipaddress {

192.168.71.110

}

track_script {

chk_nginx

}

}

 • 編輯master監控腳本

# vim /usr/local/sbin/check_ng.sh //加入以下:

#!/bin/bash

#時間變量,用於記錄日誌

d=`date --date today +%Y%m%d_%H:%M:%S`

#計算nginx進程數量

n=`ps -C nginx --no-heading|wc -l`

#若是進程爲0,則啓動nginx,而且再次檢測nginx進程數量,

#若是還爲0,說明nginx沒法啓動,此時須要關閉keepalived

if [ $n -eq "0" ]; then

systemctl start nginx

n2=`ps -C nginx --no-heading|wc -l`

if [ $n2 -eq "0" ]; then

echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log

systemctl stop keepalived

fi

fi

• 給腳本755權限

#chmod 755 /usr/local/sbin/check_ng.sh

啓動master服務

#systemctl start  keepalived

[root@wjh-01 selinux]# ps aux|grep keepalived //keepalived服務已啓動

root 4307 0.0 0.1 126340 1856 pts/0 S+ 14:39 0:00 vi /etc/keepalived/keepalived.conf

root 25588 1.7 0.1 120212 1464 ? Ss 15:19 0:29 /usr/sbin/keepalived -D

root 25589 0.0 0.3 126824 3300 ? S 15:19 0:00 /usr/sbin/keepalived -D

root 50572 0.1 0.3 131148 3008 ? S 15:43 0:00 /usr/sbin/keepalived -D

root 51002 0.0 0.0 112664 972 pts/1 R+ 15:47 0:00 grep --color=auto kee

[root@wjh-01 selinux]# ps aux|grep nginx //nginx服務已啓動

root 48392 0.0 0.2 122284 2072 ? Ss 15:41 0:00 nginx: master process nginx

nginx 48394 0.0 0.3 122712 3104 ? S 15:41 0:00 nginx: worker process

root 51064 0.0 0.0 112664 972 pts/1 R+ 15:47 0:00 grep --color=auto nginx

• 查看定義的VIP也啓動

用keepalived配置高可用集羣-2

•  編輯backup上keepalived配置文件

# > /etc/keepalived/keepalived.conf //清空原配置文件內容

# vim /etc/keepalived/keepalived.conf  //加入以下:

global_defs {

notification_email {

aming@aminglinux.com

}

notification_email_from root@aminglinux.com

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

#全局配置,定義出現故障,給那個郵箱發郵件

vrrp_script chk_nginx {

script "/usr/local/sbin/check_ng.sh"

#定義檢測nginx的啓動腳本

interval 3

#3秒檢查一次服務

}

#keepalived的CHK模塊,檢測NGINX服務是否啓用

vrrp_instance VI_1 {

state BACKUP

#定義角色是主,仍是從

interface ens33

#定義那塊網卡,發送VRRP協議

virtual_router_id 51

#定義路由ID,主從保持一致

priority 90

#定義權重

advert_int 1

authentication {

auth_type PASS

auth_pass aminglinux>com

}

#定義認證機制是PASS

virtual_ipaddress {

192.168.71.110

}

#定義VIP(對外提供服務的IP)

track_script {

chk_nginx

}

}

• 編輯backup監控腳本

# vim /usr/local/sbin/check_ng.sh //加入以下:

#!/bin/bash

#時間變量,用於記錄日誌

d=`date --date today +%Y%m%d_%H:%M:%S`

#計算nginx進程數量

n=`ps -C nginx --no-heading|wc -l`

#若是進程爲0,則啓動nginx,而且再次檢測nginx進程數量,

#若是還爲0,說明nginx沒法啓動,此時須要關閉keepalived

if [ $n -eq "0" ]; then

systemctl start nginx

n2=`ps -C nginx --no-heading|wc -l`

if [ $n2 -eq "0" ]; then

echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log

systemctl stop keepalived

fi

fi

• 給腳本755權限

#chmod 755 /usr/local/sbin/check_ng.sh

• 啓動backup服務

#systemctl start  keepalived

用keepalived配置高可用集羣-3

測試

經過瀏覽器測試兩臺機器上的 web 內容 http://192.168.71.110。爲了區分開,咱們能夠

把 nginx 的默認頁修改一下:

在master 上執行

# echo "master" >/usr/share/nginx/html/index.html

在 backup上執行

# echo "backup" >/usr/share/nginx/html/index.html

• 先肯定好兩臺機器上nginx差別,好比能夠經過curl -I 來查看nginx版本

• 測試1:關閉master上的nginx服務

• 測試2:關閉master上的keepalived服務

[root@wjh-01 ~]# curl 192.168.71.110

master

[root@wjh-01 ~]# systemctl stop keepalived

[root@wjh-01 ~]# curl 192.168.71.110

backup

• 測試3:啓動master上的keepalived服務

[root@wjh-01 ~]# systemctl start keepalived

[root@wjh-01 ~]# curl 192.168.71.110

master

相關文章
相關標籤/搜索