侧边栏壁纸
博主头像
lai博主等级

  • 累计撰写 53 篇文章
  • 累计创建 19 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

Keepalived实现nginx高可用

lai
lai
2022-12-26 / 0 评论 / 0 点赞 / 877 阅读 / 1,094 字
温馨提示:
本文最后更新于 2022-12-26,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

环境


Red Hat 8.6
查询指令 cat /etc/issue
root用户

下载


https://www.keepalived.org/download.html
这里使用最新版本keepalived-2.2.7.tar.gz

安装


解压文件

cd /app/server/keepalived
tar -zxvf keepalived-2.2.7.tar.gz
cd keepalived-2.2.7

编译安装

# 支持IPVS
yum install -y libnl* ipvsadm

# 配置安装目录
./configure --prefix=/app/server/keepalived-service
# 确保下述内容正确
Use VRRP Framework       : Yes
Use VRRP VMAC            : Yes
Use VRRP authentication  : Yes

# 安装
make && make install

配置keepalived服务

参考配置文件

less /app/server/keepalived-service/etc/keepalived/keepalived.conf.sample

默认加载的配置文件路径为/etc/ keepalived/ keepalived. conf,目前该文件并不存在

mkdir /etc/keepalived
vi /etc/keepalived/keepalived.conf

主服务

#全局配置  
global_defs {                
    router_id nginx_master       #表示运行Keepalived服务器的一个标识,唯一的  
    script_user root
}  
#检测脚本  
vrrp_script chk_http_port 
{  
    script "/app/server/keepalived-service/check_nginx_pid.sh" #心跳执行的脚本,检测nginx是否启动  
    interval 2                          #(检测脚本执行的间隔,单位是秒)  
    weight 2                            #权重  
}  
#vrrp 实例定义部分  
vrrp_instance VI_1 {          
    state MASTER            # 指定keepalived的角色,MASTER为主,BACKUP为备  
    interface ens18         # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) 用ifconfig查看你具体的网卡  
    virtual_router_id 66    # 虚拟路由编号,主从要一致  
    priority 100            # 优先级,数值越大,获取处理请求的优先级越高  
    advert_int 1            # 检查间隔,默认为1s(vrrp组播周期秒数)  
    #授权访问  
    authentication {  
        auth_type PASS #设置验证类型和密码,MASTER和BACKUP必须使用相同的密码才能正常通信  
        auth_pass xeqevk8prbl%emY  
    }  
    track_script {  
        chk_http_port            #(调用检测脚本)  
    }  
    virtual_ipaddress {  
        168.100.100.101            # 定义虚拟ip(VIP),可多设,每行一个  
    }  
}

从服务

#全局配置  
global_defs {      
	notification_email {      
		root@localhost
	} 
	notification_email_from keepalived@localhost  
	smtp_server 127.0.0.1  
	smtp_connect_timeout 10  
    router_id nginx_backup       #表示运行Keepalived服务器的一个标识,唯一的  
    script_user root
}  
#检测脚本  
vrrp_script chk_http_port 
{  
    script "/app/server/keepalived-service/check_nginx_pid.sh" #心跳执行的脚本,检测nginx是否启动  
    interval 2                          #(检测脚本执行的间隔,单位是秒)  
    weight 2                            #权重  
}  
#vrrp 实例定义部分  
vrrp_instance VI_1 {          
    state BACKUP            # 指定keepalived的角色,MASTER为主,BACKUP为备  
    interface ens18         # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) 用ifconfig查看你具体的网卡  
    virtual_router_id 66    # 虚拟路由编号,主从要一致  
    priority 90            # 优先级,数值越大,获取处理请求的优先级越高,低于主服务  
    advert_int 1            # 检查间隔,默认为1s(vrrp组播周期秒数)  
    #授权访问  
    authentication {  
        auth_type PASS #设置验证类型和密码,MASTER和BACKUP必须使用相同的密码才能正常通信  
        auth_pass xeqevk8prbl%emY  
    }  
    track_script {  
        chk_http_port            #(调用检测脚本)  
    }  
    virtual_ipaddress {  
        168.100.100.101            # 定义虚拟ip(VIP),可多设,每行一个  
    }  
}

设置nginx检查脚本

vi /app/server/keepalived-service/check_nginx_pid.sh
#!/bin/bash  
#检测nginx是否启动了  
A=`ps -C nginx --no-header |wc -l`          
if [ $A -eq 0 ];then    #如果nginx没有启动就启动nginx                          
      /app/server/nginx/nginx-all/sbin/nginx                #重启nginx  
      if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then    #nginx重启失败,则停掉keepalived服务,进行VIP转移  
              killall keepalived                      
      fi  
fi
chmod +x /app/server/keepalived-service/check_nginx_pid.sh

开机启动

vim /lib/systemd/system/keepalived.service
[Unit]
Description=LVS and VRRP High Availability Monitor
After=network-online.target syslog.target
Wants=network-online.target
Documentation=man:keepalived(8)
Documentation=man:keepalived.conf(5)
Documentation=man:genhash(1)
Documentation=https://keepalived.org

[Service]
Type=forking
PIDFile=/run/keepalived.pid
KillMode=process
EnvironmentFile=-/app/server/keepalived-service/etc/sysconfig/keepalived
ExecStart=/app/server/keepalived-service/sbin/keepalived  $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID

[Install]

配置systemctl

1:systemctl daemon-reload  重新加载
2:systemctl enable keepalived.service  设置开机自动启动
3:systemctl disable keepalived.service 取消开机自动启动
4:systemctl start keepalived.service 启动
5:systemctl stop keepalived.service停止
6:systemctl restart keepalived.service 重启

日志配置

vi /app/server/keepalived-service/etc/sysconfig/keepalived
KEEPALIVED_OPTIONS="-D -S 6"
#配置keepalived日志路径
vi /etc/rsyslog.conf
local6.* /app/server/keepalived-service/log/keepalived.log
#重启
systemctl restart keepalived.service rsyslog.service

查看虚拟ip

ip a | grep 168.100.100.101

开通网络权限

需要开通主从对112端口进行检测

确保VIP与RIP在同一个网段,跨网会存在路由问题;

0

评论区