Linux集羣概述linux
用keepalived配置高可用nginx
#目錄路徑 [root@taoyuan ~]# ls /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf [root@taoyuan ~]# > !$ > /etc/keepalived/keepalived.conf [root@taoyuan ~]# vim /etc/keepalived/keepalived.conf #內容以下 global_defs { notification_email { pingbuqingyun@taoyuan.com } #出現問題時發送郵件 notification_email_from root@taoyuan.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" interval 3 } #檢測腳本,檢測時間3s一次 vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass taoyuan>com } virtual_ipaddress { 192.168.0.100 } #定義VIP,也就是公有ip track_script { chk_nginx } #加載chk_nginx }
#路徑 [root@taoyuan ~]# 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 /etc/init.d/nginx start 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
[root@taoyuan ~]# chmod 755 /usr/local/sbin/check_ng.sh
[root@taoyuan ~]# systemctl start keepalived
[root@taoyuan ~]# ps aux |grep keep
root 2084 0.0 0.0 120740 1404 ? Ss 10:01 0:00 /usr/sbin/keepalived -D
root 2085 0.0 0.1 122812 3108 ? S 10:01 0:00 /usr/sbin/keepalived -D
root 2086 0.0 0.1 127244 2940 ? S 10:01 0:00 /usr/sbin/keepalived -D
root 2213 0.0 0.0 112676 980 pts/0 R+ 10:02 0:00 grep --color=auto keepvim
* 日誌
[root@taoyuan ~]# less /var/log/messages後端
* 132上編輯配置文件
#內容
global_defs {
notification_email {br/>pingbuqingyun@taoyuan.com
}
notification_email_from root@taoyuan.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"
interval 3
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass aminglinux>com
}
virtual_ipaddress {
192.168.0.100
}
track_script {
chk_nginx
}
}centos
* 132上編輯監控腳本
#內容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權限 * 132上也啓動服務 systemctl start keepalived **測試高可用** * 先肯定好兩臺機器上nginx差別,好比能夠經過curl -I 來查看nginx版本 * 測試1:關閉master上的nginx服務 * 測試2:在master上增長iptabls規則 * iptables -I OUTPUT -P vrrp -j DROP * 測試3:關閉master上keepalived服務 * 測試4:啓動master上的keepalived服務