1.場景描述
因為要做閘道器的高可用,用到了keepalived+nginx,來保證nginx的高可用,如下圖:
安裝了keepavlived,走了一些彎路,記錄下吧,nginx的安裝就不多說了,部落格已經介紹了好幾篇了。
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解壓也行。
2.1.2 下載並安裝keepalived
官網地址:https://www.keepalived.org/download.html
我們使用的最新版本:keepalived-2.0.18.tar.gz,993k
tar -zxvf keepalived-2.0.18.tar.gz
cd keepalived-2.0.18/
./configure --prefix=/usr/local/keepalived
make
make 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資訊。
如果你覺得文章對你有些幫助,歡迎微信搜尋「軟體老王」第一時間閱讀或交流!
2.2 配置檔案修改
2.2.1 master的conf配置檔案
vi cd /etc/keepalived/keepalived.conf
!Configuration File for keepalived
global_defs {
router_id RUANJIANLAOWANG_MASTER
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens666 #這裡必須非常注意,軟體老王剛開始沒太注意,這裡是你的網路卡名稱
virtual_router_id 81 #這個會在backup機器上用到
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
10.192.168.21
}
}
2.2.2 還有個檢查nginx是否掛掉的sh,也上傳下
vi nginx_check.sh
#!/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
/etc/init.d/keepalived stop
fi
fi
2.2.3 backup機器上conf檔案
/bin/bash: Configuration: command not found
bal_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 BACKUP
interface ens168
virtual_router_id 81
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 111track_script {
chk_nginx ## 執行 Nginx 監控的服務,軟體老王
}
virtual_ipaddress {
10.192.168.21
}
}
這裡記著也把檢查nginx的sh檔案上傳上,跟master上的一樣。
2.3 啟動
(1)配置完成後開始啟動
[root@t-ruanjianlaowang ~]# service keepalived start
(2)啟動過程中,開啟日誌看一下(剛開始可能會有問題,看下日誌啥都明白了)
[root@t-ruanjianlaowang ~]# tail -f /var/log/messages
(3)假如啟動不了,可以嘗試以下方式。
[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 ens168
ens666、ens168為網路卡名稱
假如繫結成功,軟體老王機器裡顯示的資訊裡面會有:10.192.168.21的顯示
更多知識請關注公眾號:「軟體老王」,IT技術與相關乾貨分享,回覆關鍵字獲取對應乾貨,java,送必看的10本“武功祕籍”;圖片,送100多萬張可商用高清圖片;面試,送剛畢業就能月薪“20k”的java面試題,軟考,送官方pdf書籍與通關論文,後續會不斷更新,比如“工具”,“視訊“等,已經在整理中。