1. 程式人生 > 其它 >Linux配置Keepalived,實現nginx主備切換

Linux配置Keepalived,實現nginx主備切換

一、下載Keepalived

1、下載依賴軟體

yum install -y gcc
yum install -y openssl-devel
yum install -y libnl libnl-devel
yum install -y libnfnetlink-devel
yum install -y net-tools
yum install -y vim wget

2、下載壓縮包到/usr/local(我以keepalived-2.0.10為例),解壓安裝

  cd /use/local/src
  tar -zxvf keepalived-2.0.10.tar.gz
  mv keepalived-2.0
.10 keepalived   # 修改檔名 cd /use/local/keepalived/ ./configure         make && make install

3、將配置檔案複製到預設路徑

mkdir /etc/keepalived
cp /usr/local/keepalived/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/         # 啟動檔案,用於開機自啟
cp /usr/local/keepalived/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp
/usr/local/sbin/keepalived /usr/sbin/

4、設定開機自啟

chkconfig keepalived on
service keepalived start   #啟動服務
service keepalived stop    #停止服務
service keepalived restart #重啟服務

二、建立隨Nginx服務停止而停止的指令碼(主備兩臺都要配置)

1、在/etc/keeplived路徑下建立check_nginx.sh指令碼檔案,內容如下:

#!/bin/bash
A=`ps -C nginx --no-header | wc -l`
if [ $A -eq 0
];then service keepalived stop fi

2、修改該指令碼的許可權

chmod 777 check_nginx.sh

3、修改/etc/keepalived下的keepalived.conf如下(紅色部分)

三、其他

1、keepalived配置檔案參考:https://www.cnblogs.com/kevingrace/p/6248941.html

2、如果加入LVS,參考:https://lupengfei.blog.csdn.net/article/details/86514445