虛擬:172.62.17.110 Nginx主:172.62.17.111 Nginx從:172.62.17.112 一、系統(tǒng)優(yōu)化 #關(guān)閉防火墻(兩臺(tái)) systemctl stop firewalld systemctl disable firewalld #關(guān)閉selinux sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config setenforce 0 二,、安裝nginx,,keepalived(兩臺(tái)) dnf -y install nginx dnf -y install keepalived dnf -y install vim 三、配置Keepalived 主機(jī)配置Keepalived ip a 查下真實(shí)網(wǎng)卡 vim /etc/nginx/nginx_check.sh 創(chuàng)建檢測(cè)腳本 #!/bin/bash counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then /usr/sbin/nginx sleep 2 counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then systemctl stop keepalived fi fi 主節(jié)點(diǎn)配置 vim /etc/keepalived/keepalived.conf smtp_server 172.62.17.111 # 郵箱ip改為本機(jī) # vrrp_strict 把這個(gè)注銷,否則最后虛擬ip不通 #添加檢測(cè)腳本 vrrp_script chk_http_port { script "/etc/nginx/nginx_check.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER #主機(jī)這里是MASTER 從機(jī)是BACKUP interface ens18 #網(wǎng)卡 virtual_router_id 51 # 主,、從機(jī)的virtual_router_id必須相同 priority 100 # 主、備機(jī)取不同的優(yōu)先級(jí),,主機(jī)值較大,,備份機(jī)值較小 advert_int 1 #心跳檢測(cè)間隔時(shí)間 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.62.17.110 # VRRP 虛擬IP ;可換行輸入多個(gè)進(jìn)行綁定 } } 從節(jié)點(diǎn)配置 vim /etc/keepalived/keepalived.conf smtp_server 172.62.17.112 #添加檢測(cè)腳本 vrrp_script chk_http_port { script "/etc/nginx/nginx_check.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface ens18 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.62.17.110 } } 在默認(rèn)的keepalive.conf里面還有 virtual_server,real_server 這樣的配置,,我們這用不到,,它是為lvs準(zhǔn)備的。 接著重啟nginx,,重啟keepalived 主 systemctl restart nginx systemctl restart keepalived 從 systemctl restart nginx systemctl restart keepalived 查日志 tail -f /var/log/messages|grep Keepalived 四,、主主配置 主節(jié)點(diǎn)加入VI_2 vim /etc/keepalived/keepalived.conf vrrp_instance VI_2 { state BACKUP #這里修改為BACKUP interface ens18 virtual_router_id 52 #這里修改為52 priority 90 #修改優(yōu)先級(jí) advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.62.17.113 #綁定另外一個(gè)VIP } track_script { chk_http_port } } 從節(jié)點(diǎn)加入VI_2 vrrp_instance VI_2 { state MASTER interface ens18 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.62.17.113 } track_script { chk_http_port } } 主從總結(jié): master沒掛,則master占有vip且nginx運(yùn)行在master上 master掛了,,則slave搶占vip且在slave上運(yùn)行nginx服務(wù) 如果master上的nginx服務(wù)掛了,,則nginx會(huì)自動(dòng)重啟,重啟失敗后會(huì)自動(dòng)關(guān)閉keepalived,,這樣vip資源也會(huì)轉(zhuǎn)移到slave上,。 master和slave兩邊都開啟nginx服務(wù),無論master還是slave,,當(dāng)其中的一個(gè)keepalived服務(wù)停止后,,vip都會(huì)漂移到keepalived服務(wù)還在的節(jié)點(diǎn)上,。 缺點(diǎn)是在于,如果master一直不掛,,slave有點(diǎn)浪費(fèi),。
主主總結(jié): 當(dāng)任何一臺(tái)服務(wù)器上面的keepalived服務(wù)停掉后,另外一臺(tái)服務(wù)器上面的keepalived都會(huì)進(jìn)入MASTER狀態(tài)處理請(qǐng)求,。 如這里停掉17.111上的keepalived服務(wù),,則17.112上的keepalived服務(wù)的VRRP_Instance(VI_1)進(jìn)入MASTER狀態(tài),并對(duì)虛擬17.110,17.113進(jìn)行處理,。 |
|