Keepalived+Nginx(主备模式) 高可用集群搭建演示

本文详细介绍了如何使用Keepalived配合Nginx实现服务器的高可用性,通过主备模式配置,确保在Nginx宕机时,流量能切换到备份服务器,包括添加epel源、配置Nginx和Keepalived,以及测试和部署过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为什么要使用keepalived

Nginx作为反向代理服务器实现服务器分配资源负载均衡,但是如果有一天Nginx服务器宕机了,那用户的请求如何发送到服务器来呢?于是,keepalived作为检查服务器状态的软件来了!

主备模式搭建

整体架构图
在这里插入图片描述
172.16.10.185 模拟web服务器1
172.16.10.189 模拟web服务器2
172.16.10.186 keepalived + nginx 负载均衡服务器1
172.16.10.184 keepalived + nginx 负载均衡服务器2
172.16.10.190 模拟客户端测试

添加epel源:

curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

四台机器关闭防火墙安装nginx:

systemctl stop firewalld         //关闭防火墙
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux        //关闭selinux,重启生效
setenforce 0                //关闭selinux,临时生效
ntpdate 0.centos.pool.ntp.org    //时间同步
yum install nginx -y           //安装nginx

两台模拟web修改

# vim /etc/nginx/nginx.conf        //编辑配置文件
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80;
        server_name  www.zhubing.com;
        location / {
            root         /usr/share/nginx/html;
        }
    access_log    /var/log/nginx/access.log main;
    }
}

两台LB服务器

# vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    upstream backend {
    server 172.16.10.185:80 weight=1 max_fails=3 fail_timeout=20s;
    server 172.16.10.189:80 weight=1 max_fails=3 fail_timeout=20s;
    }
    server {
        listen       80;
        server_name  www.mtian.org;
        location / {
        proxy_pass http://backend;
        proxy_set_header Host $host:$proxy_port;
        proxy_set_header X-Forwarded-For $remote_addr;
        }
    }
}

检测nginx配置文件语法是否正确

nginx -t

在这里插入图片描述
测试机器 配置域名解析
vim /etc/hosts
在这里插入图片描述
测试,看到均衡负载,证明nginx搭建成功
在这里插入图片描述

搭建keepalived,两台LB服务器

yum install -y keepalived

主:vim /etc/keepalived/keepalived.conf
注意 interface 属性 需要设置成自己的网卡名
在这里插入图片描述
从:vim /etc/keepalived/keepalived.conf

在这里插入图片描述
启动 keepalived
systemctl start keepalived
查看状态
systemctl status keepalived

我的查看状态发现dead状态
在这里插入图片描述
查看日志重启,需要开两个终端

tail -f /var/log/message 
systemctl restart keepalived 

在这里插入图片描述
发现interface 配置错了,
小插曲:如果发现启动不了,不要慌,tail -f /var/log/message 查看日志

启动之后 查看ip(主)

[root@k8s-slave2 nginx] ip a

在这里插入图片描述

我们在测试机上访问 vip
在这里插入图片描述
从机

因为从设置的是backup模式,所以不会出现vip,只有当主keepalived 挂掉,就会漂移到从机

systemctl stop keepalived.service

在从服务器中出现vip
在这里插入图片描述
访问vip依旧是通的

至此Keepalived+Nginx高可用主备模式搭建成功!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值