1.場景描述
由於要作網關的高可用,用到了keepalived+nginx,來保證nginx的高可用,以下圖:html
安裝了keepavlived,走了一些彎路,記錄下吧,nginx的安裝就很少說了,本身博客已經介紹了好幾篇了。java
2. 解決方案
2.1 安裝keepalived
2.1.1 安裝相關依賴包
[root@t-ruanjianlaowang ~]# yum install -y libnl*[root@t-ruanjianlaowang ~]# yum install -y libnfnetlink-devel zlib zlib-devel gcc gcc-c++ openssl openssl-devel openssh
假如連不了,網上也有這些包的下載,下載後tar解壓也行。nginx
2.1.2 下載並安裝keepalived
官網地址:https://www.keepalived.org/download.htmlgit
咱們使用的版本:keepalived-2.0.18.tar.gz,993kgithub
tar -zxvf keepalived-2.0.18.tar.gzcd keepalived-2.0.18/./configure --prefix=/usr/local/keepalivedmakemake install
2.1.3 添加keepavlived爲系統服務
[root@t-ruanjianlaowang ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived [root@t-ruanjianlaowang ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived[root@t-ruanjianlaowang ~]# cp /root/tools/keepalived-2.0.18/keepalived/etc/init.d/keepalived /etc/init.d/keepalived#/etc/keepalived下好像有了keepalived文件夾,有了就不用mkdir了,軟件老王[root@t-ruanjianlaowang ~]# mkdir /etc/keepalived [root@t-ruanjianlaowang ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
2.1.4 設置開機啓動
# 設置開機啓動[root@t-ruanjianlaowang ~]# chkconfig keepalived on[root@t-ruanjianlaowang ~]# service keepalived start|stop|restart
到這裏僅僅是keepalived安裝完成,接下來還要根據項目項目配置keepalived.conf信息。web
2.2 配置文件修改
2.2.1 master的conf配置文件
vi cd /etc/keepalived/keepalived.conf
!Configuration File for keepalivedglobal_defs { router_id RUANJIANLAOWANG_MASTER}vrrp_script chk_nginx {script "/etc/keepalived/nginx_check.sh"interval 2weight -20}vrrp_instance VI_1 {state MASTERinterface ens666 #這裏必須很是注意,軟件老王剛開始沒太注意,這裏是你的網卡名稱virtual_router_id 81 #這個會在backup機器上用到priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {chk_nginx}virtual_ipaddress {10.192.168.21}}
2.2.2 還有個檢查nginx是否掛掉的sh,也上傳下
vi nginx_check.sh
#!/bin/bashcounter=$(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 /etc/init.d/keepalived stop fifi
2.2.3 backup機器上conf文件
/bin/bash: Configuration: command not foundbal_defs { router_id RUANJIANLAOWANG2}vrrp_script chk_nginx {script "/etc/keepalived/nginx_check.sh" ## 檢測 nginx 狀態的腳本路徑interval 2 ## 檢測時間間隔weight -20 ## 若是條件成立,權重-20}vrrp_instance VI_1 {state BACKUPinterface ens168virtual_router_id 81priority 90advert_int 1authentication {auth_type PASSauth_pass 111track_script { chk_nginx ## 執行 Nginx 監控的服務,軟件老王 }virtual_ipaddress {10.192.168.21 }}
這裏記着也把檢查nginx的sh文件上傳上,跟master上的同樣。面試
2.3 啓動
(1)配置完成後開始啓動spring
[root@t-ruanjianlaowang ~]# service keepalived start
(2)啓動過程當中,打開日誌看一下(剛開始可能會有問題,看下日誌啥都明白了)springboot
[root@t-ruanjianlaowang ~]# tail -f /var/log/messages
(3)假如啓動不了,能夠嘗試如下方式。bash
[root@t-ruanjianlaowang ~]# systemctl daemon-reload#上面試事後,不行再從新註冊服務[root@t-ruanjianlaowang ~]#chkconfig --del keepalived[root@t-ruanjianlaowang ~]# cd /usr/lib/systemd/system/[root@t-ruanjianlaowang ~]#rm -rf keepalived.service[root@t-ruanjianlaowang ~]# chkconfig --add keepalived
2.4 驗證
[root@t-ruanjianlaowang ~]#ip addr show ens666[root@t-ruanjianlaowang ~]#ip addr show ens168ens666、ens168爲網卡名稱假如綁定成功,軟件老王機器裏顯示的信息裏面會有:10.192.168.21的顯示
1.微服務實戰系列
4.中間件等
原創不易,若是以爲有點用的話,請絕不留情地素質三連吧,分享、點贊、在看,我不挑,由於這將是我寫做更多優質文章的最強動力。
本文分享自微信公衆號 - 軟件老王(ruanjianlaowang_pub)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。