LVS + KeepAlived实现高可用负载均衡

上文已经介绍了lvs和keepalived的基本概念和用法,下面直接做lvs和keepalived来实现高可用负载均衡

配置:

主机名 ip 系统 用途
client 172.16.147.1 mac 客户端
lvs-keepalived-master 172.16.147.154 centos7.5 分发器
lvs-keepalived-slave 172.16.147.155 centos7.5 分发器备
test-nginx1 172.16.147.153 centos7.5 web1
test-nginx2 172.16.147.156 centos7.5 web2
vip
  1. 主备调度器安装软件

    [root@lvs-keepalived-master ~]# yum -y install ipvsadm keepalived
    [root@lvs-keepalived-slave ~]# yum -y install ipvsadm keepalived
    2. Keepalived
    lvs-master
    [root@lvs-keepalived-master ~]# vim /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived

    global_defs {
    router_id lvs-keepalived-master #辅助改为lvs-backup
    }

    vrrp_instance VI_1 {
    state MASTER
    interface ens33 #VIP绑定接口
    virtual_router_id 80 #VRID 同一组集群,主备一致
    priority 100 #本节点优先级,辅助改为50
    advert_int 1 #检查间隔,默认为1s
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {
    172.16.147.101/24 # 可以写多个vip
    }
    }

    virtual_server 172.16.147.101 80 { #LVS配置
    delay_loop 3
    lb_algo rr #LVS调度算法
    lb_kind DR #LVS集群模式(路由模式)
    net_mask 255.255.255.0
    protocol TCP #健康检查使用的协议
    real_server 172.16.147.153 80 {
    weight 1
    inhibit_on_failure #当该节点失败时,把权重设置为0,而不是从IPVS中删除
    TCP_CHECK { #健康检查
    connect_port 80 #检查的端口
    connect_timeout 3 #连接超时的时间
    }
    }
    real_server 172.16.147.156 80 {
    weight 1
    inhibit_on_failure
    TCP_CHECK {
    connect_timeout 3
    connect_port 80
    }
    }
    }

  2. 备用机安装和配置

    [root@lvs-keepalived-slave ~]# vim /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived

    global_defs {
    router_id lvs-keepalived-slave
    }

    vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    nopreempt #不抢占资源
    virtual_router_id 80
    priority 50
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {
    172.16.147.101/24
    }
    }
    virtual_server 172.16.147.101 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    net_mask 255.255.255.0
    protocol TCP
    real_server 172.16.147.153 80 {
    weight 1
    inhibit_on_failure
    TCP_CHECK {
    connect_port 80
    connect_timeout 3
    }
    }
    real_server 172.16.147.156 80 {
    weight 1
    inhibit_on_failure
    TCP_CHECK {
    connect_timeout 3
    connect_port 80
    }
    }
    }

  3. 启动KeepAlived

    [root@lvs-keepalived-master ~]# systemctl start keepalived
    [root@lvs-keepalived-master ~]# systemctl enable keepalived

    [root@lvs-keepalived-master ~]# ipvsadm -Ln
    IP Virtual Server version 1.2.1 (size=4096)
    Prot LocalAddress:Port Scheduler Flags
    -> RemoteAddress:Port Forward Weight ActiveConn InActConn
    TCP 172.16.147.101:80 rr persistent 20
    -> 172.16.147.153:80 Route 1 0 0
    -> 172.16.147.156:80 Route 0 0 0

  4. 所有RS配置(nginx1,nginx2)

    [root@test-nginx1 ~]# yum install -y nginx
    [root@test-nginx2 ~]# yum install -y nginx
    [root@test-nginx1 ~]# echo "ip addr add dev lo 172.16.147.101/32" >> /etc/rc.local
    [root@test-nginx1 ~]# echo "net.ipv4.conf.all.arp_ignore = 1" >> /etc/sysctl.conf
    [root@test-nginx1 ~]# echo "net.ipv4.conf.all.arp_announce = 2" >> /etc/sysctl.conf
    [root@test-nginx1 ~]# sysctl -p
    [root@test-nginx1 ~]# echo "web1..." >> /usr/share/nginx/html/index.html
    [root@test-nginx1 ~]# systemctl start nginx
    [root@test-nginx1 ~]# chmod +x /etc/rc.local

上述步骤实现了两台机器的高可用并进行172.16.147.156和172.16.147.153的负载均衡

相关推荐
只是橘色仍温柔1 小时前
xshell可以ssh连接,但vscode不行
运维·vscode·ssh
IT里的交易员1 小时前
【系统】换硬盘不换系统,使用WIN PE Ghost镜像给电脑无损扩容换硬盘
运维·电脑
共享家95271 小时前
深入剖析Linux常用命令,助力高效操作
linux·运维·服务器
大刘讲IT1 小时前
制造业数字化转型:流程改造先行还是系统固化数据?基于以MTO和MTS的投资回报分析
运维·经验分享·生活·产品经理·数据可视化
吃旺旺雪饼的小男孩2 小时前
Ubuntu 22.04 安装和运行 EDK2 超详细教程
linux·运维·ubuntu
阿政一号2 小时前
Linux进程间通信:【目的】【管道】【匿名管道】【命名管道】【System V 共享内存】
linux·运维·服务器·进程间通信
方渐鸿2 小时前
【2025】快速部署安装docker以及项目搭建所需要的基础环境(mysql、redis、nginx、nacos)
java·运维·docker·持续部署·dockercompse
小哈里3 小时前
【运维】云计算的发展历程,云原生时代的运维理念&工具技术栈,高可用系统的云运维 —— 以K8S集群调度算法与命令为例
运维·云原生·kubernetes·云计算·架构设计
A charmer3 小时前
【Linux】文件系统知识梳理:从磁盘硬件到文件管理
linux·运维·服务器
Cynthia的梦3 小时前
Linux学习-Linux进程间通信(IPC)聊天程序实践指南
linux·运维·学习