五、nginx指令碼,新建nginx_check.sh指令碼放到/etc/keepalived下
指令碼內容:
#!/bin/bash
a=ps -c nginx –no-header |wc -l
if [ $a -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ps -c nginx --no-header |wc -l
-eq 0 ];then
killall keepalived
fifi
注:腳步放到linux伺服器後執行檢驗一下,如果報錯:
-bash: ./nginx_check.sh: /bin/bash^m: bad interpreter: no such file or directory
說明指令碼檔案格式有誤,需將dos改位unix,具體操作:
<1>在linux系統上檢視nginx_check.sh的格式
vi nginx_check.sh
<2>在vi的命令列,輸入
:set ff
會輸出 該檔案的檔案格式 ,例如:fileformat=dos
<3>如果要將其格式轉換成 unix,可以輸入命令
:set ff=unix
<4>然後輸入命令儲存退出,這個時候檔案格式已經轉換成了 unix
:wq六、修改keepalived的conf檔案
<1> nginx主機修改/etc/keedalived/keedalived.conf檔案
! configuration file for keepalived
vrrp_script chk_nginx
vrrp_instance vi_1
track_script
virtual_ipaddress
}
<2> nginx從機修改/etc/keedalived/keedalived.conf檔案
! configuration file for keepalived
vrrp_script chk_nginx
vrrp_instance vi_1
track_script
virtual_ipaddress
}
七、執行效果
直接訪問虛ip 10.20.60.132,會到主128上,如果128宕機,會自動切到129上,128再重新啟動後會自動切到128上。
128宕機後,訪問自動切到129
可參考:
keepalived nginx集群部署
192.168.132.130 伺服器1 192.168.132.132 伺服器2 虛擬ip 192.168.132.131 yum install gcc c gcc是gnu的c語言編譯器,至於c 編譯器,它的名字叫做g yum install y pcre pcre devel 在nginx編譯...
keepalived nginx負載安裝
環境 centos7.6 keepalived工具可以生成虛擬浮動ip,繫結到網絡卡上。172.21.210.19 master 172.21.210.20 backup 172.21.210.30 vip 浮動ip 1 兩個節點安裝keepalived yum y install keepaliv...
keepalived nginx離線安裝
keepalived nginx安裝 把keepalived 1.2.23.tar.gz傳送到 opt下 cd opt tar zxvf keepalived 1.2.23.tar.gz cd keepalived 1.2.23 configure prefix usr local keepaliv...