keepalived主从模式监测nginx
一、環(huán)境說明
VirtualBox6.1
CenOS7
Nginx
keepalived-1.1.20.tar.gz
二、IP配置
主Keepalived服務(wù)器IP地址:10.0.2.4
備Keepalived服務(wù)器IP地址:10.0.2.5
虛擬Keepalived服務(wù)器IP地址:10.0.2.6
三、下載地址
http://www.keepalived.org/software/ 可以選擇自己需要的版本。
四、安裝步驟
1、下載好安裝包,可使用xshell軟件連接虛擬機,將壓縮包上傳至/home目錄。或者直接在虛擬機里執(zhí)行命令下載:wget http://www.keepalived.org/software/keepalived-1.1.20.tar.gz(如果提示沒有wget命令,安裝即可:yum install wget)。
2、解壓keepalived(兩個虛擬機都需要做,步驟一致):
執(zhí)行完./configure后會顯示:
3、編譯安裝:
4、修改配置文件路徑:
[root@localhost keepalived-1.1.20]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ [root@localhost keepalived-1.1.20]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ [root@localhost keepalived-1.1.20]# mkdir /etc/keepalived [root@localhost keepalived-1.1.20]# cp /usr/local/etc/keepalived/keepalived.conf/etc/keepalived/ [root@localhost keepalived-1.1.20]# cp /usr/local/sbin/keepalived /usr/sbin/5、設(shè)置服務(wù)開機啟動:
[root@localhost keepalived-1.1.20]# vi /etc/rc.local在最后一行加上: /etc/init.d/keepalived start
五、配置流程
1、配置 主:
[root@localhost keepalived-1.1.20]# vi /etc/keepalived/keepalived.conf
2、配置 備:
3、啟動服務(wù):
六、驗證測試
1、創(chuàng)建監(jiān)控雙機nginx狀態(tài)的腳本:vi /opt/check_nginx.sh
寫入以下代碼:
2、安裝psmisc,因為腳本用到了killall命令,最小化安裝的centos沒有,所以要安裝一下:yum -y install psmisc。
3、再次編輯keepalived配置文件:
插入第一段:
vrrp_script check_nginx {script "/root/check_nginx.sh"interval 2weight 2 }再插入第二段:
track_script { check_nginx }它們的位置如下:
4、備用機與此步驟一樣。
5、啟動keepalived服務(wù),并開機自啟。
6、測試:
修改一下主機nginx的歡迎界面,以和備用機區(qū)分:添加單詞Master和Backup:
7、在瀏覽器輸入虛擬機ip:81,停掉主機服務(wù)器的nginx,systemctl stop nginx.service。刷新頁面會發(fā)現(xiàn)沒有變化,這是因為keepalived檢測到nginx掛了,會通過check_nginx.sh腳本自動啟動nginx。
8、當(dāng)手動停止主機的keepalived:systemctl stop keepalived.service
會發(fā)現(xiàn)頁面標(biāo)題的Master沒有了,就變成了Backup。
此時才算是配置成功了。
總結(jié)
以上是生活随笔為你收集整理的keepalived主从模式监测nginx的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 添加chorme插件时出现“程序包无效”
- 下一篇: Nexus安装及配置