How to configure keepalived dual-machine hot standby nginx

王林
Release: 2023-05-14 12:34:11
forward
1211 people have browsed it

环境准备:

  2台centos: 192.168.0.105 和192.168.0.118, 虚拟ip(vip)为192.168.0.119

配置keepavlived

  分别在105和118上安装keepalived

yum install keepalived
Copy after login

  准备心跳角本

  keepalived的配置相当灵活,可以定时执行角本命令,用于心跳检查,比如,我们访问nginx发现nginx不可访问时,就关闭keepalived,从而切换到从nginx来实现不间断的服务支持。

vi /etc/keepalived/keepalived.conf

#!/bin/bash

count=0
for (( k=0; k<2; k++ ))
do
 check_code=$( curl --connect-timeout 3 -sl -w "%{http_code}\\n" http://localhost:81 -o /dev/null )
 if [ "$check_code" != "200" ]; then
  count=count +1
  continue
 else
  count = 0
  break
 fi
done
if [ "$count" != "0" ]; then
 killall keepalived
 exit 1
else
 exit 0
fi
Copy after login

  这段角本的意思就是说每次的心跳检查会执行一个for循环,访问,在for的2次循环中如果返回的状态都不是200就会关闭keepalived。这个角本在105和118两台机器上都要准备好。

  设置118机器上的keepalived为主节点,105机器上的keepalived为从节点,它们两个的配置文件差不太多

vi /etc/keepalived/keepalived.conf
Copy after login

  118机器keepalived的配置文件

! configuration file for keepalived
vrrp_script chk_nginx {
 script "/etc/keepalived/check_nginx.sh" # 心跳检测角本
 interval 2 #脚本执行间隔,每2s检测一次
 weight -5 #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5
 fall 3 #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
 rise 2 #检测1次成功就算成功。但不修改优先级
}
vrrp_instance vi_1 {
 state master #指定keepalived的角色,master表示此主机是主服务器,backup表示此主机是备用服务器
 interface eth0 #指定监测网络的接口。实例绑定的网卡,因为在配置虚拟ip的时候必须是在已有的网卡上添加的
 mcast_src_ip 192.168.0.118 ## 发送多播数据包时的源ip地址
 virtual_router_id 51 #虚拟路由标识,master和backup必须是一致的
 priority 100 #定义优先级,数字越大,优先级越高
 advert_int 2 #设定master与backup负载均衡器之间同步检查的时间间隔,单位是秒
 authentication { #设置验证类型和密码。主从必须一样
  auth_type pass #设置vrrp验证类型,主要有pass和ah两种
  auth_pass 1111 #设置vrrp验证密码,在同一个vrrp_instance下,master与backup必须使用相同的密码才能正常通信
 }
 virtual_ipaddress {
  192.168.0.119 #vrrp 虚拟地址 如果有多个vip,换行填写
 }
 track_script {
  chk_nginx # 心跳脚本,即在 vrrp_script 部分指定的名字
 }
}
Copy after login

115机器上的角本对上面的角本稍做变动即可

改变 state master -> state backup,priority 100 -> priority 90,mcast_src_ip 192.168.0.118 -> mcast_src_ip 192.168.0.105。其他的地方保持不变,是不是很简单。

! configuration file for keepalived
vrrp_script chk_nginx {
 script "/etc/keepalived/check_nginx.sh"
 interval 2
 weight -5
}
vrrp_instance vi_1 {
 state backup # 修改
 interface eth0
 mcast_src_ip 192.168.0.105 # 修改 为本机ip
 virtual_router_id 51
 priority 90 #数字变小
 advert_int 2
 authentication {
  auth_type pass
  auth_pass 1111
 }
 virtual_ipaddress {
  192.168.0.119
 }
 track_script {
  chk_nginx
 }
}
Copy after login

到这里keepalived的简单配置就完成了下面就启动keepalived

service keepalived start
Copy after login

安装nginx

查看nginx的依赖库是否完整

rpm -qa zlib
rpm -qa zlib-devel
rpm -qa openssl
rpm -qa openssl-devel
rpm -qa pcre
rpm -qa pcre-devel
rpm -qa gcc
Copy after login

How to configure keepalived dual-machine hot standby nginx

如果没有就安装

yum -y install gcc zlib zlib-devel openssl openssl-devel pcre-devel
Copy after login

下载并解压nginx

mkdir nginxsrc
cd nginxsrc/
Copy after login

How to configure keepalived dual-machine hot standby nginx

wget http://nginx.org/download/nginx-1.13.9.tar.gz
tar zxvf nginx-1.13.9.tar.gz
cd nginx-1.13.9/
Copy after login

How to configure keepalived dual-machine hot standby nginx

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module
Copy after login

安装在/usr/local/nginx/sbin/nginx 目录下

make&&make install
Copy after login

How to configure keepalived dual-machine hot standby nginx

执行成功后会显示leaving directory

How to configure keepalived dual-machine hot standby nginx

启动

/usr/local/nginx/sbin/nginx
Copy after login
Copy after login

How to configure keepalived dual-machine hot standby nginx

检查

ps aux|grep nginx
Copy after login

How to configure keepalived dual-machine hot standby nginx

开机启动

vi /etc/rc.local
Copy after login

添加一行

/usr/local/nginx/sbin/nginx
Copy after login
Copy after login

How to configure keepalived dual-machine hot standby nginx

因为我的机器上80端口被别的应用占用了,所以就修改105和118的 nginx.conf 把端口80 修改为81

vi /usr/local/nginx/conf/nginx.conf
Copy after login

How to configure keepalived dual-machine hot standby nginx

分别个性105 和119上的index.html 加上一个ip以便们们识别打开的是哪个ip上的index.html

vi /usr/local/nginx/html/index.html
Copy after login

How to configure keepalived dual-machine hot standby nginx

nginx重新加载配置

/usr/local/nginx/sbin/nginx -s reload
Copy after login

好了,我们访问一下

curl 192.168.0.119:81
Copy after login

返回的html是 118机器上的index.html

How to configure keepalived dual-machine hot standby nginx

现在我们把118的nginx停止

 /usr/local/nginx/sbin/nginx -s stop
Copy after login

这时118上的keepalived的心跳检查角本发现nginx无法访问会把keepalived关闭,然后转向从节点

我们再访问一下119

 curl 192.168.0.119:81
Copy after login

How to configure keepalived dual-machine hot standby nginx

在118上重新启动nginx和keepalived

再访问119 返回的网址是118上的index.html

The above is the detailed content of How to configure keepalived dual-machine hot standby nginx. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!