openEuler欧拉配置nginx Keepalived主从和双主

​虚拟:172.62.17.110
Nginx主:172.62.17.111
Nginx从:172.62.17.112

一、系统优化

关闭防火墙(两台)

systemctl stop firewalld
systemctl disable firewalld

关闭selinux

sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0

二、安装nginx,keepalived(两台)

dnf -y install nginx
dnf -y install keepalived
dnf -y install vim

三、配置Keepalived

配置Keepalived(两台)

ip a 查下真实网卡
查到本机的真实网卡名称是 ens18

vim /etc/nginx/nginx_check.sh 创建检测脚本

#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
    /usr/sbin/nginx
    sleep 2
    counter=$(ps -C nginx --no-heading|wc -l)
    if [ "${counter}" = "0" ]; then
        systemctl stop keepalived
    fi
fi

主节点配置

vim /etc/keepalived/keepalived.conf

smtp_server 172.62.17.111  # 邮箱ip改为本机
#   vrrp_strict  把这个注销,否则最后虚拟ip不通
#添加检测脚本
vrrp_script chk_http_port {
        script "/etc/nginx/nginx_check.sh"
        interval 2
        weight 2
}
vrrp_instance VI_1 {
    state MASTER   #主机这里是MASTER 从机是BACKUP
    interface ens18  #网卡
    virtual_router_id 51  # 主、从机的virtual_router_id必须相同
    priority 100   # 主、备机取不同的优先级,主机值较大,备份机值较小
    advert_int 1  #心跳检测间隔时间
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.62.17.110   # VRRP 虚拟IP ;可换行输入多个进行绑定
    }
}

从节点配置

vim /etc/keepalived/keepalived.conf

smtp_server 172.62.17.112
#添加检测脚本
vrrp_script chk_http_port {
        script "/etc/nginx/nginx_check.sh"
        interval 2
        weight 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens18
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.62.17.110
    }
}

在默认的keepalive.conf里面还有 virtual_server,real_server 这样的配置,我们这用不到,它是为lvs准备的。

接着重启nginx,重启keepalived


systemctl restart nginx
systemctl restart keepalived


systemctl restart nginx
systemctl restart keepalived

查日志
tail -f /var/log/messages|grep Keepalived

四、主主配置

主节点加入VI_2

vim /etc/keepalived/keepalived.conf

vrrp_instance VI_2 {
    state BACKUP  #这里修改为BACKUP
    interface ens18
    virtual_router_id 52 #这里修改为52
    priority 90    #修改优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.62.17.113  #绑定另外一个VIP
    }
track_script {
   chk_http_port
 }
}

从节点加入VI_2

vrrp_instance VI_2 {
    state MASTER
    interface ens18
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.62.17.113
    }
track_script {
   chk_http_port
 }
}

主从总结:

master没挂,则master占有vip且nginx运行在master上
master挂了,则slave抢占vip且在slave上运行nginx服务
如果master上的nginx服务挂了,则nginx会自动重启,重启失败后会自动关闭keepalived,这样vip资源也会转移到slave上。
master和slave两边都开启nginx服务,无论master还是slave,当其中的一个keepalived服务停止后,vip都会漂移到keepalived服务还在的节点上。
缺点是在于,如果master一直不挂,slave有点浪费。

主主总结:

当任何一台服务器上面的keepalived服务停掉后,另外一台服务器上面的keepalived都会进入MASTER状态处理请求。
如这里停掉17.111上的keepalived服务,则17.112上的keepalived服务的VRRP_Instance(VI_1)进入MASTER状态,并对虚拟17.110,17.113进行处理。

posted @ 2024-01-08 11:02  李济宏(Amadeus)  阅读(358)  评论(0编辑  收藏  举报