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

2021/8/21 7:06:22

本文主要是介绍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



这篇关于Linux配置Keepalived,实现nginx主备切换的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程