keepalived 双实例_nginx互为主备
一、實(shí)驗(yàn)?zāi)康?#xff1a;兩臺(tái)nginx服務(wù)器,利用keepalived實(shí)現(xiàn)雙實(shí)例互為主備,并能夠?qū)ttp服務(wù)檢測(cè)動(dòng)態(tài)切換主備,主服務(wù)器檢測(cè)失敗后,重啟對(duì)應(yīng)http服務(wù)。
二、實(shí)驗(yàn)環(huán)境
| 服務(wù)器類型 | IP | 軟件 | 主備情況 | VIP |
| CentOS 6.6(nginx1) | 172.18.100.11 | nginx+keepalived | 實(shí)例VI_1為主服務(wù)器;實(shí)例VI_2為輔服務(wù)器 | 172.18.100.21 |
| CentOS 6.6(nginx2) | 172.18.100.12 | nginx+keepalived | 實(shí)例VI_2為主服務(wù)器;實(shí)例VI_1為輔服務(wù)器 | 172.18.100.22 |
三、操作步驟
????1.安裝nginx并設(shè)置測(cè)試頁;
?? ? 2.安裝keepalived實(shí)現(xiàn)地址雙主備切換;
????3.編寫檢測(cè)服務(wù)腳本;
????4.編寫狀態(tài)腳本。
四、安裝nginx服務(wù)
五、安裝keepalived,配置
? nginx1 keepalived.conf配置
!?Configuration?File?for?keepalivedglobal_defs?{notification_email?{root@localhost}notification_email_from?root@localhostsmtp_server?127.0.0.1smtp_connect_timeout?30router_id?node1vrrp_mcast_group4?224.0.100.18 } vrrp_instance?VI_1?{state?MASTERinterface?eth0virtual_router_id?20priority?100advert_int?1authentication?{auth_type?PASSauth_pass?11112222}virtual_ipaddress?{172.18.100.21?} } vrrp_instance?VI_2?{state?BACKUPinterface?eth0virtual_router_id?22priority?98advert_int?1authentication?{auth_type?PASSauth_pass?11112222}virtual_ipaddress?{172.18.100.22} }????nginx2 keepalived.conf
!?Configuration?File?for?keepalivedglobal_defs?{notification_email?{root@localhost}notification_email_from?root@localhostsmtp_server?127.0.0.1smtp_connect_timeout?30router_id?node2vrrp_mcast_group4?224.0.100.18 } vrrp_instance?VI_1?{state?BACKUPinterface?eth0virtual_router_id?20priority?98advert_int?1authentication?{auth_type?PASSauth_pass?11112222}virtual_ipaddress?{172.18.100.21} } vrrp_instance?VI_2?{state?MASTERinterface?eth0virtual_router_id?22priority?100advert_int?1authentication?{auth_type?PASSauth_pass?11112222}virtual_ipaddress?{172.18.100.22} }?測(cè)試:停掉nginx1的keepalived服務(wù),可以看到服務(wù)地址的全都配置到了nginx2服務(wù)器網(wǎng)卡上,反之亦然。
六、配置服務(wù)檢測(cè)腳本
在每個(gè)主機(jī)的keepalived兩個(gè)實(shí)例中全局中添加
在keepalived vrrp實(shí)例上下文中添加檢測(cè)track
track_script?{chk_httpd }? 測(cè)試:查看關(guān)掉nginx服務(wù)后,服務(wù)主動(dòng)降低優(yōu)先級(jí),主節(jié)點(diǎn)切換為備份節(jié)點(diǎn),IP地址漂移到另一個(gè)服務(wù)器。這時(shí)候無論那個(gè)服務(wù)器的服務(wù)進(jìn)程宕掉(當(dāng)然可以用其他方式測(cè)試,注意效率,畢竟一秒一次測(cè)試),服務(wù)都能將地址和服務(wù)轉(zhuǎn)移到另一臺(tái)服務(wù)器。
七、添加服務(wù)狀態(tài)檢測(cè)腳本,對(duì)服務(wù)失敗的服務(wù)器重啟服務(wù)
為每個(gè)主服務(wù)器主節(jié)點(diǎn)添加檢測(cè)腳本:當(dāng)主節(jié)點(diǎn)服務(wù)不可用時(shí),重啟服務(wù)。
注意腳本需要可執(zhí)行權(quán)限,且只能有一個(gè)。
/etc/keepalived/changToBackup.sh
#!/bin/sh service?httpd?restart?&&?echo?"httpd?service?restart?success.@$(date?+'%F?%T')"為每個(gè)主服務(wù)器實(shí)例上下文中添加檢測(cè)腳本:
notify_backup?"/etc/keepalived/changToBackup.sh"? 測(cè)試:當(dāng)主動(dòng)將一臺(tái)服務(wù)的服務(wù)停掉后,服務(wù)會(huì)將服務(wù)重啟,主服務(wù)器從主服務(wù)器轉(zhuǎn)換動(dòng)輔服務(wù)器,在切換到主服務(wù)器,自動(dòng)完成!不必手動(dòng)操作!
轉(zhuǎn)載于:https://blog.51cto.com/kingslanding/1772422
總結(jié)
以上是生活随笔為你收集整理的keepalived 双实例_nginx互为主备的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 张良王者荣耀打法
- 下一篇: PopupWindow的一些使用总结