keepalived+nginx实现高可用

搭建说明

  1. 服务器:192.168.117.97、192.168.117.98、192.168.117.99
  2. VIP:192.168.117.100

搭建Nginx环境

  1. 下载安装包:https://pan.baidu.com/s/1eAJHe0c_jvcwvjhRzgdlsw,密码:x4jo
  2. 解压安装包:tar -xf /opt/module/nginx-1.19.4.tar.gz && rm -rf /opt/module/nginx-1.19.4.tar.gz
  3. 检查openssl-devel:yum list installed openssl-devel,如果没有需要先安装:yum -y install openssl openssl-devel
  4. 编译安装:cd /opt/module/nginx-1.19.4 && ./configure && make && make install
  5. 创建软连接:ln -s /usr/local/nginx /opt/module/nginx
  6. 启动Nginx:/opt/module/nginx/sbin/nginx
  7. 查看是否启动成功:ps -ef | grep nginx

Keepalived实现高可用

  1. keepalived安装:yum install keepalived -y

  2. killall命令:用于杀死一个进程,包括其子进程,如果命令未找到,则需要下载:yum install psmisc

  3. 添加Nginx检查脚本:vi /etc/keepalived/check_nginx.sh,查看nginx是否启动,如果没启动则先杀掉nginx所有进程,然后启动,如果启动不起来,停掉keepalived服务,此时心跳断掉,VIP进行转移。

    #!/bin/bash
    counter=$(ps -C nginx --no-heading|wc -l)
    if [ "${counter}" != "2" ]; then
        killall nginx
        sleep 0.5
        /opt/module/nginx/sbin/nginx
        sleep 2
        counter=$(ps -C nginx --no-heading|wc -l)
        if [ "${counter}" != "2" ]; then
            service keepalived stop
        fi
    fi
    
  4. 给脚本添加执行权限:chmod +x /etc/keepalived/check_nginx.sh

  5. 配置keepalived:vi /etc/keepalived/keepalived.conf

    ! Configuration File for keepalived
    global_defs {
       router_id LVS_NGINX
    }
    
    vrrp_script check_nginx {
      script "/etc/keepalived/check_nginx.sh"          #表示将一个脚本信息赋值给变量check_web
      interval 5                                     #执行监控脚本的间隔时间
      weight 2                                       #利用权重值和优先级进行运算,从而降低主服务优先级使之变为备服务器(建议先忽略)
    }
    
    vrrp_instance VI_1 {
        state MASTER         # 备机选BACKUP
        #nopreempt            # 配置不竞争,旧主来了,VIP不会离开飘走
        interface eth0      # 表示keepalived家族表示信息
        virtual_router_id 51
        priority 100        # 后面备机需要依次往下减
        advert_int 1          # 主服务器组播包发送间隔时间
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.117.100/24 dev eth0 label eth0:2
        }
        track_script {
           check_nginx        #调用执行脚本
        }
    }
    
  6. 启动keepalived:service keepalived start

  7. 查看keepalived状态:service keepalived status

  8. 查看VIP挂载情况:ip addr

  9. 如果nginx想要使用VIP,因为备机是没有VIP的,所有Nginx无法监听,此时则需要修改内核文件

    • 实现监听本地不存在的ip地址:echo 'net.ipv4.ip_nonlocal_bind = 1' >>/etc/sysctl.conf
    • sysctl -p
    • echo "1" >/proc/sys/net/ipv4/ip_nonlocal_bind

实验测试

  1. 调整nginx首页,便于观察VIP的移动路径,vi /opt/module/nginx/html/index.html

    <h1>Welcome to nginx!(192.168.117.94)h1>
    
  2. 网页访问VIP:http://192.168.117.100/

  3. 主节点关闭nginx:killall nginx,然后刷新网页,预期VIP不漂移,因为keepalived重启nginx成功了

  4. 主节点重启keepalived:service keepalived restart,预期VIP会漂移

  5. 主节点模拟nginx损坏,启动不成功,预计会漂移

    • 模拟nginx损坏:mv /opt/module/nginx/conf/nginx.conf /opt/module/nginx/conf/nginx.conf-back
    • 关闭nginx:killall nginx
    • 刷新网页,查看VIP是否漂移
    • 恢复nginx:mv /opt/module/nginx/conf/nginx.conf-back /opt/module/nginx/conf/nginx.conf
    • 重启keepalived:service keepalived restart
    • 查看nginx是否自动启动:ps -ef | grep nginx

你可能感兴趣的:(nginx,centos)