18.1 集羣介紹nginx
1. 根據功能劃分爲兩大類:高可用和負載均衡vim
2. 高可用集羣一般爲兩臺服務器,一臺工做,另一臺做爲冗餘,當提供服務的機器宕機,冗餘將接替繼續提供服務後端
3. 實現高可用的開源軟件有:heartbeat、keepalivedcentos
負載均衡集羣,須要有一臺服務器做爲分發器,它負責把用戶的請求分發給後端的服務器處理,在這個集羣裏,除了分發器外,就是給用戶提供服務的服務器了,這些服務器數量至少爲2bash
4. 實現負載均衡的開源軟件有LVS、keepalived、haproxy、nginx,商業的有F5、Netscaler 服務器
18.2 keepalived介紹負載均衡
1. 在這裏咱們使用keepalived來實現高可用集羣,由於heartbeat在centos6上有一些問題,影響實驗效果tcp
2. keepalived經過VRRP(Virtual Router Redundancy Protocl)來實現高可用。ide
3. 在這個協議裏會將多臺功能相同的路由器組成一個小組,這個小組裏會有1個master角色和N(N>=1)個backup角色。測試
4. master會經過組播的形式向各個backup發送VRRP協議的數據包,當backup收不到master發來的VRRP數據包時,就會認爲master宕機了。此時就須要根據各個backup的優先級來決定誰成爲新的mater。
5. Keepalived要有三個模塊,分別是core、check和vrrp。其中core模塊爲keepalived的核心,負責主進程的啓動、維護以及全局配置文件的加載和解析,check模塊負責健康檢查,vrrp模塊是來實現VRRP協議的。
18.3-18.5用keepalived配置高可用集羣(上中下)
1. 準備兩臺機器hao1和hao2 :
hao1做爲master,hao2做爲backup
2. 兩臺機器都安裝 :yum install -y keepalived
[root@hao-01 ~]# yum install -y keepalived
[root@hao-02 ~]# yum install -y keepalived
3. 兩臺機器都安裝nginx,能夠編譯安裝nginx :
也能夠用yum安裝nginx : yum install -y nginx
安裝epel倉庫(源碼包,默認centos不帶nginx包) :
yum install -y epel-release
yum install -y nginx
yum安裝nginx啓動方式:service nginx start
編譯安裝nginx啓動方式:/etc/init.d/nginx start
4. 搜索nginx是否啓動 ?
[root@hao-01 ~]# ps aux |grep nginx
[root@hao-02 ~]# ps aux |grep nginx
hao1機器上操做
(這裏hao1是編譯安裝的nginx)
5. 清空keepalived.conf文件中的內容(hao1機器) :
[root@hao-01 ~]# > /etc/keepalived/keepalived.conf
6. 從新配置keepalived.conf文件(hao1機器) :
[root@hao-01 ~]# vim /etc/keepalived/keepalived.conf
插入內容:
global_defs {
bal_defs {
notification_email {
1071599947@qq.com
}
notification_email_from root@hao.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 MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass haomima>com
}
virtual_ipaddress {
192.168.130.100
}
track_script {
chk_nginx
}
}
7. 建立check_ng.sh腳本,並添加內容 :
[root@hao-01 ~]# 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
#yum安裝的nginx用:systemctl start nginx 編譯安裝的用:/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
8. 設定check_ng.sh腳本755權限 :
[root@hao-01 ~]# chmod 755 /usr/local/sbin/check_ng.sh
9. 啓動keepalived :
[root@hao-01 ~]# systemctl start keepalived
10. 搜索keepalived是否啓動 ?
[root@hao-02 ~]# ps aux |grep keepalived
11. 關閉Nginx,再搜索nginx是否啓動 ?
[root@hao-01 ~]# /etc/init.d/nginx stop
[root@hao-01 ~]# ps aux |grep nginx
11. 臨時關閉getenforce防火牆 :
[root@hao-01 ~]# setenforce 0
關閉firewalld防火牆:
[root@hao-01 ~]# systemctl stop firewalld
hao2機器上操做
(這裏hao2是yum安裝的nginx)
12. 臨時關閉getenforce防火牆 :
[root@hao-02 ~]# setenforce 0
關閉firewalld防火牆:
[root@hao-02 ~]# systemctl stop firewalld
13. 清空keepalived.conf文件中的內容(hao2機器) :
[root@hao-02 ~]# > /etc/keepalived/keepalived.conf
14. 從新配置keepalived.conf文件(hao2機器) :
[root@hao-02 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
1071599947@qq.com
}
notification_email_from root@hao.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 haomima>com
}
virtual_ipaddress {
192.168.130.100
}
track_script {
chk_nginx
}
}
15. 建立check_ng.sh腳本,並添加內容 :
[root@hao-02 ~]# 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
#yum安裝的nginx用:systemctl start nginx 編譯安裝的用:/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
16. 設定check_ng.sh腳本755權限 :
[root@hao-02 ~]# chmod 755 /usr/local/sbin/check_ng.sh
17. 啓動keepalived :
[root@hao-02 ~]# systemctl start keepalived
18. 搜索keepalived是否啓動 ?
[root@hao-02 ~]# ps aux |grep keepalived
hoa1 hao2機器都打開80端口:
打開80端口 :
[root@hao-01 ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@hao-02 ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
遊覽器訪問hao1 hao2 ip :
測試高可用
1. 關閉Nginx,再搜索nginx是否啓動 ?
[root@hao-01 ~]# systemctl stop nginx
[root@hao-01 ~]# ps aux |grep nginx
2. ip add查看.100是否是在hao1機器上(master) :
[root@hao-01 ~]# ip add
模仿hao1機器掛了,關閉了 keepalived
[root@hao-01 ~]# systemctl stop keepalived
[root@hao-01 ~]# ip add
3. hao1機器(master)掛了,hao2機器(backup)上,ip add查看.100自動掛載到了hao2上!
4. hao1機器(master)再啓動keepalived .100又掛在hao1機器 :
[root@hao-01 ~]# systemctl start keepalived