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

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-02-21 01:08:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-21 01:08:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-21 01:08:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-21 01:08:01       20 阅读

热门阅读

  1. antd dependencies使用

    2024-02-21 01:08:01       31 阅读
  2. day3 2/20

    day3 2/20

    2024-02-21 01:08:01      26 阅读
  3. git----->git pull和git fetch区别

    2024-02-21 01:08:01       27 阅读
  4. vue3+ts实现表格的增删改查(一)

    2024-02-21 01:08:01       31 阅读
  5. 我的创作纪念日

    2024-02-21 01:08:01       34 阅读
  6. 前端开发框架推荐总结二

    2024-02-21 01:08:01       27 阅读
  7. Nginx是什么?怎么用?

    2024-02-21 01:08:01       39 阅读
  8. rust-learn

    2024-02-21 01:08:01       29 阅读
  9. 数据结构——时间复杂度

    2024-02-21 01:08:01       30 阅读
  10. 【前端】Vue中引入excel模板并下载以及XLSX使用

    2024-02-21 01:08:01       28 阅读
  11. KeepAlived搭建高可用的HAproxy负载均衡集群系统

    2024-02-21 01:08:01       29 阅读