1. 程式人生 > >2018-7-3

2018-7-3

%d color root tex 執行 from 加ip cat inter

18.1 集群介紹/18.2 keepalived介紹

18.3/18.4/18.5 用keepalived配置高可用集群



18.1 集群介紹/18.2 keepalived介紹

技術分享圖片


集群聽起來好像就是一個很高端很的技術,其實不是的,那麽集群其實就是一堆計算機的集合,給用戶提供同一個服務的一組計算機,就稱之為集群,對於用戶而言好像就是一臺計算機提供的服務


技術分享圖片



18.3/18.4/18.5 用keepalived配置高可用集群

準備兩臺機器

技術分享圖片

131作為master

技術分享圖片

133作為backp



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

技術分享圖片


兩臺機器都安裝nginx

131已經安裝過

技術分享圖片

yum install -y nginx


更改配置文件

技術分享圖片

技術分享圖片


清空配置文件

技術分享圖片

配置模板

global_defs {

notification_email {

[email protected]

}

notification_email_from [email protected]

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 MASTER

interface ens33

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass aminglinux>com

}

virtual_ipaddress {

192.168.188.100

}


track_script {

chk_nginx

}


}


編輯監控腳本vim

技術分享圖片

模板:

#!/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


給腳本755權限

啟動服務

技術分享圖片

主上配置完畢,註意selinux和firewalld


131編輯配置文件,模板:

global_defs {

notification_email {

[email protected]

}

notification_email_from [email protected]

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.188.100

}


track_script {

chk_nginx

}


}


編輯監控腳本:

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

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



測試:

先確定好兩臺機器上nginx差異,比如可以通過curl -I 來查看nginx版本

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

測試2:在master上增加iptabls規則

iptables -I OUTPUT -p vrrp -j DROP

測試3:關閉master上的keepalived服務

測試4:啟動master上的keepalived服務


2018-7-3