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如下(红色部分)

Linux配置Keepalived,实现nginx主备切换

 

 

 

三、其他

1、keepalived配置文件参考:https://www.cnblogs.com/kevingrace/p/6248941.html

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

上一篇:LVS + Keepalived群集


下一篇:LVS负载均衡+DR模式+Keepalived