LVS+Keepalived高可用群集配置案例

以下是一个 LVS+Keepalived 高可用群集配置案例:

1、环境准备

  • LVS 主调度器(lvs1):IP 地址为 192.168.8.101,心跳 IP 为 192.168.4.101
  • LVS 备调度器(lvs2):IP 地址为 192.168.8.102,心跳 IP 为 192.168.4.102
  • 虚拟 IP(vip):192.168.8.100
  • Web 服务器 1(nginx1):IP 地址为 192.168.8.103
  • Web 服务器 2(nginx2):IP 地址为 192.168.8.104
  • 客户端:IP 地址为 192.168.8.1

2、配置步骤

2.1、为lvs1和lvs2配置ip地址

lvs1:配置IP地址

复制代码
nmcli connection modify ens33 ipv4.method manual ipv4.addresses 192.168.8.101/24
nmcli connection modify ens36 ipv4.method manual ipv4.addresses 192.168.4.101/24
nmcli connection up ens33
nmcli connection up ens36

lvs2:配置IP地址

复制代码
nmcli connection modify ens33 ipv4.method manual ipv4.addresses 192.168.8.102/24
nmcli connection modify ens36 ipv4.method manual ipv4.addresses 192.168.4.102/24
nmcli connection up ens33
nmcli connection up ens36

2.2、为lvs1和lvs2安装keepalived和ipvsadm并修改配置文件

  • 安装:

    yum install -y keepalived ipvsadm

Ivs1的 keepalived 配置文件修改

复制代码
!configuration file for keepalived
global_defs{
    router_id cluster1
}
vrrp_instance web{
    state master
    interface ens33
    virtual_router_id 51
    priority 255
    advert_int 1
    authentication{
        auth_type pass
        auth_pass 1111
    }
    virtual_ipaddress{
        192.168.8.100/24
    }
}
virtual_server 192.168.8.100 80{
    delay_loop 6
    lb_algo rr
    lb_kind dr
    persistence_timeout 50
    protocol tcp
    real_server 192.168.8.103 80{
        weight 1
        tcp_check{
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.8.104 80{
        weight 2
        tcp_check{
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
}

Ivs2的 keepalived 配置文件修改

bash 复制代码
! configuration file for keepalived
global_defs{
    router_id cluster2
}
vrrp_instance web{
    state backup
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication{
        auth_type pass
        auth_pass 1111
    }
    virtual_ipaddress{
        192.168.8.100/24
    }
}
virtual_server 192.168.8.100 80{
    delay_loop 6
    lb_algo rr
    lb_kind dr
    persistence_timeout 50
    protocol tcp
    real_server 192.168.8.103 80{
        weight 1
        tcp_check{
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.8.104 80{
        weight 2
        tcp_check{
            connect_timeout 3
            retry 3
            delay_before_retry 3
        }
    }
}

重启服务:在两台lvs上输入

bash 复制代码
systemctl restart keepalived

3、为后端服务器安装 httpd

web1

bash 复制代码
安装:yum install -y httpd
写入信息:echo "hello,192.168.8.103" > /usr/share/nginx/html/index.html
启动服务:systemctl enable httpd.service --now

web2

bash 复制代码
安装:yum install -y httpd
写入信息:echo "hello,192.168.8.104" > /usr/share/nginx/html/index.html
启动服务:systemctl enable httpd.service --now

4、为后端服务器配置IP地址

web1

bash 复制代码
nmcli con mod ens33 ipv4.address 192.168.8.103/24 ipv4.gateway 192.168.8.2
nmcli con up ens33
增加虚拟接口并配置 vip:nmcli connection add type dummy ifname dummy2 ipv4.method manual ipv4.addresses 192.168.8.100/32
修改内核 arp 配置:cat >>/etc/sysctl.conf<< eof net.ipv4.conf.all.arp_ignore=1 net.ipv4.conf.all.arp_announce=2 eof
刷新配置:sysctl -p

web2

bash 复制代码
nmcli con mod ens33 ipv4.address 192.168.8.104/24 ipv4.gateway 192.168.8.2
nmcli con up ens33
增加虚拟接口并配置 vip:nmcli connection add type dummy ifname dummy2 ipv4.method manual ipv4.addresses 192.168.8.100/32
修改内核 arp 配置:cat >>/etc/sysctl.conf<< eof net.ipv4.conf.all.arp_ignore=1 net.ipv4.conf.all.arp_announce=2 eof
刷新配置:sysctl -p
相关推荐
高冷的肌肉码喽28 分钟前
Linux-进程间的通信
linux·运维·服务器
乖乖是干饭王32 分钟前
Linux系统编程中的_GNU_SOURCE宏
linux·运维·c语言·学习·gnu
jekc86839 分钟前
禅道18.2集成LDAP
linux·运维·服务器
weixin_307779131 小时前
Linux下GCC和C++实现统计Clickhouse数据仓库指定表中各字段的空值、空字符串或零值比例
linux·运维·c++·数据仓库·clickhouse
Tender_光2 小时前
iptables实验
运维·服务器
szxinmai主板定制专家3 小时前
【飞腾AI加固服务器】全国产化飞腾+昇腾310+PCIe Switch的AI大模型服务器解决方案
运维·服务器·arm开发·人工智能·fpga开发
深科文库3 小时前
构建 MCP 服务器:第 3 部分 — 添加提示
服务器·python·chatgpt·langchain·prompt·aigc·agi
点击查询3 小时前
怎么把自己电脑设置成服务器?
运维·服务器
阿里云大数据AI技术3 小时前
ES Serverless 8.17王牌发布:向量检索「火力全开」,智能扩缩「秒级响应」!
大数据·运维·serverless
wanhengidc3 小时前
服务器中日志分析的作用都有哪些
运维·服务器