Keepalived实现Nginx的高可用集群案例

服务器规划:
    
                    serverb(nginx2):192.168.233.144
                    serverc(客户端):192.168.233.140
                    serverd(nginx1):192.168.233.141

结构图:

serverd(nginx1):

# 安装nginx
yum install nginx -y

# 进入nginx配置目录
cd /etc/nginx/conf.d/

# 编辑www.conf文件
vim www.conf
server {
    listen 80; 
    server_name localhost;

    location / { 
        root /data/web;
        index index.html;
    }   
}
# 创建web目录
mkdir -p /data/web

# 创建测试页面index.html
echo "web test page, ip is `hostname -I`." >/data/web/index.html

# 启动nginx服务
systemctl start nginx

# 安装keepalived
yum install keepalived -y

# 进入keepalived配置目录
cd /etc/keepalived/

# 备份keepalived.conf文件
cp keepalived.conf{,.bak}

# 编辑keepalived.conf文件
vim keepalived.conf
! Configuration File for keepalived

global_defs {
    router_id LVS_141  # 设置路由器的ID为LVS_141
}

vrrp_instance nginx {
    state MASTER  # 将该实例设置为MASTER状态,备份节点应设置为BACKUP
    interface ens160  # 指定VRRP实例使用的网络接口
    virtual_router_id 51  # 虚拟路由器的唯一ID,同一组中MASTER和BACKUP的virtual_router_id必须相同
    priority 100  # 优先级设置为100,MASTER的优先级通常比BACKUP的优先级高
    advert_int 1  # 发送VRRP通告的时间间隔,单位为秒

    authentication {
        auth_type PASS  # 使用简单密码认证方式
        auth_pass 1111  # 认证密码为1111
    }

    virtual_ipaddress {
        192.168.233.50  # 配置虚拟IP地址,客户端将访问该IP以访问服务
    }
}

# 启动keepalived服务
systemctl start keepalived

serverb(nginx2):

# 安装 nginx
yum install nginx -y

# 进入 nginx 配置目录
cd /etc/nginx/conf.d/

# 编辑 nginx 配置文件 www.conf
vim www.conf

# 在配置文件中写入以下内容
server {
    listen 80; 
    server_name localhost;

    location / { 
        root /data/web;
        index index.html;
    }   
}

# 创建 web 根目录
mkdir -p /data/web

# 在 web 根目录下创建测试页面 index.html
echo "web test page, ip is `hostname -I`." >/data/web/index.html

# 启动 nginx
systemctl start nginx

# 安装 keepalived
yum install keepalived -y

# 进入 keepalived 配置目录
cd /etc/keepalived/

# 备份 keepalived 配置文件
cp keepalived.conf{,.bak}

# 编辑 keepalived 配置文件
vim keepalived.conf

# 在 keepalived 配置文件中写入以下内容
! Configuration File for keepalived

global_defs {
   router_id LVS_144  # 设置路由器标识为LVS_144
}

vrrp_instance nginx {  # 创建一个名为nginx的VRRP实例
    state BACKUP  # 将此节点设置为备用状态,主节点应该设置为MASTER
    interface ens160  # 指定VRRP实例要使用的网络接口
    virtual_router_id 51  # VRRP实例的虚拟路由器ID,与同一VLAN内其他节点的ID相同
    priority 80  # 优先级,数字越大表示优先级越高,主节点通常设置为100
    advert_int 1  # 发送VRRP通告的间隔时间,单位为秒
    authentication {  # 配置认证信息
        auth_type PASS  # 使用密码认证方式
        auth_pass 1111  # 认证密码
    }
    virtual_ipaddress {  # 设置虚拟IP地址列表
        192.168.233.50  # 在VRRP实例中使用的虚拟IP地址
    }
}

# 启动 keepalived
systemctl start keepalived

serverc(客户端):

vip访问:
curl 192.168.233.50

日志查看:

tail -f /var/log/messages

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值