lvs+keepalive大战haproxy+keepalive实现高可用集群

devtools/2024/10/17 17:39:17/

华子目录

  • lvs+keepalive
    • 实验架构
    • 实验前的准备工作
      • 1.主机准备
      • 2.KA1和KA2上安装`lvs+keepalive`
      • 3.webserver1和webserver2上安装httpd
      • 4.制作测试效果网页内容
      • 5.所有主机关闭`firewalld`和`selinux`
      • 6.开启httpd服务
    • 实验步骤
      • 1.webserver1和webserver2上配置vip
      • 2.webserver1和webserver2上关闭arp响应
      • 3.修改keepalived.conf配置文件
      • 4.重启lvs+keepalived服务
    • 测试
      • vip测试
      • 访问websever测试
      • 高可用测试
  • haproxy+keepalived
    • 实验前的准备工作
      • 1.主机准备
      • 2.KA1和KA2上安装`haproxy+keepalive`
      • 3.webserver1和webserver2上安装httpd
      • 4.制作测试效果网页内容
      • 5.所有主机关闭`firewalld`和`selinux`
      • 6.开启httpd服务
    • 实验步骤
      • 1.`KA1`和`KA2`两个节点启用`内核参数`
      • 2.配置`haproxy.cfg`配置文件
      • 3.编写脚本,用于检测`haproxy`的状态
      • 4.修改`keepalived.conf`配置文件
      • 5.重启`haproxy+keepalived`
    • 测试
      • vip测试
      • 访问websever测试
      • 高可用测试

lvskeepalive_1">lvs+keepalive

实验架构

  • 实验双主的lvs-dr模式

在这里插入图片描述

  • 由于是双主模式,所以需要2个vipKA1为主时的vip172.25.254.100KA2为主时的vip172.25.254.200
  • KA1的真实IP172.25.254.10
  • 由于是lvs-dr模式,websever1webserver2上同样都必须有两个vip172.25.254.100 172.25.254.200
  • KA2的真实IP172.25.254.20
  • webserver1的真实IP172.25.254.110
  • webserver2的真实IP172.25.254.120

实验前的准备工作

1.主机准备

  • 这里我们准备4台主机,两台web服务器两台keepalive服务器,简称KA

在这里插入图片描述

lvskeepalive_18">2.KA1和KA2上安装lvs+keepalive

[root@KA1 ~]# yum install ipvsadm keepalived -y
[root@KA2 ~]# yum install ipvsadm keepalived -y

3.webserver1和webserver2上安装httpd

[root@webserver1 ~]# yum install httpd -y
[root@webserver2 ~]# yum install httpd -y

4.制作测试效果网页内容

[root@webserver1 ~]# echo webserver1-172.25.254.110 > /var/www/html/index.html
[root@webserver2 ~]# echo webserver2-172.25.254.120 > /var/www/html/index.html

linux_45">5.所有主机关闭firewalldselinux

[root@KA1 ~]# systemctl is-active httpd
inactive
[root@KA1 ~]# getenforce
Disabled
[root@KA2 ~]# systemctl is-active httpd
inactive
[root@KA2 ~]# getenforce
Disabled
[root@webserver1 ~]# systemctl is-active httpd
inactive
[root@webserver1 ~]# getenforce
Disabled
[root@webserver2 ~]# systemctl is-active httpd
inactive
[root@webserver2 ~]# getenforce
Disabled

6.开启httpd服务

[root@webserver1 ~]# systemctl enable --now httpd
[root@webserver2 ~]# systemctl enable --now httpd

实验步骤

1.webserver1和webserver2上配置vip

  • webserver上
[root@webserver1 ~]# ip addr add 172.25.254.100/32 dev lo
[root@webserver1 ~]# ip addr add 172.25.254.200/32 dev lo
  • webserver2上
[root@webserver2 ~]# ip addr add 172.25.254.100/32 dev lo
[root@webserver2 ~]# ip addr add 172.25.254.200/32 dev lo

2.webserver1和webserver2上关闭arp响应

  • webserver1上(临时关闭,开机后无效)
[root@webserver1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@webserver1 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@webserver1 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@webserver1 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
  • webserver2上(临时关闭,开机后无效)
[root@webserver2 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@webserver2 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@webserver2 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@webserver2 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore

3.修改keepalived.conf配置文件

  • KA1上
[root@KA1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {notification_email {3066136553@qq.com}notification_email_from keepalived@timinglee.orgsmtp_server 127.0.0.1smtp_connect_timeout 30router_id ka1.timinglee.orgvrrp_skip_check_adv_addr#vrrp_strict    #必须把这里注释掉,否则keepalived服务无法启动vrrp_garp_interval 0vrrp_gna_interval 0vrrp_mcast_group4 224.0.0.18
}vrrp_instance VI_1 {    #第一组虚拟路由state MASTER      #主interface eth0    #流量接口virtual_router_id 100  #主备两主机上的虚拟路由id必须一致,相同id的主机为同一个组priority 100  #优先级大的为主advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {   #虚拟出来的接口为eth0:1172.25.254.100/24 dev eth0 label eth0:1}unicast_src_ip 172.25.254.10   #发单播包,主,发送方unicast_peer {172.25.254.20  #备,接受方}
}
vrrp_instance VI_2 { #第二组虚拟路由state BACKUP  #备interface eth0virtual_router_id 200priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.254.200/24 dev eth0 label eth0:2}unicast_src_ip 172.25.254.10unicast_peer {172.25.254.20}
}
virtual_server 172.25.254.100 80 {  #当访问该vip时delay_loop 6lb_algo wrr   #加权轮询算法lb_kind DRprotocol TCPreal_server 172.25.254.110 80 {  #转到这里主机上weight 1HTTP_GET {url {path /status_code 200}connect_timeout 3nb_get_retry 2delay_before_retry 2}}real_server 172.25.254.120 80 {  #转到这个主机上weight 1HTTP_GET {url {path /status_code 200}connect_timeout 3nb_get_retry 2delay_before_retry 2}}
}
virtual_server 172.25.254.200 80 {  #当访问这个vip的80端口时delay_loop 6lb_algo wrr  #加权轮询算法lb_kind DRprotocol TCPreal_server 172.25.254.110 80 {  #转到这个主机上weight 1HTTP_GET {url {path /status_code 200}connect_timeout 3nb_get_retry 2delay_before_retry 2}}real_server 172.25.254.120 80 { #转到这个主机上weight 1HTTP_GET {url {path /status_code 200}connect_timeout 3nb_get_retry 2delay_before_retry 2}}
}
  • KA2上
[root@KA2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {notification_email {3066136553@qq.com}notification_email_from keepalived@timinglee.orgsmtp_server 127.0.0.1smtp_connect_timeout 30router_id ka1.timinglee.orgvrrp_skip_check_adv_addr#vrrp_strict    #必须把这里注释掉,否则keepalived服务无法启动vrrp_garp_interval 0vrrp_gna_interval 0vrrp_mcast_group4 224.0.0.18
}vrrp_instance VI_1 {state BACKUPinterface eth0virtual_router_id 100priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.254.100/24 dev eth0 label eth0:1}unicast_src_ip 172.25.254.20unicast_peer {172.25.254.10}
}vrrp_instance VI_2 {state MASTERinterface eth0virtual_router_id 200priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.254.200/24 dev eth0 label eth0:2}unicast_src_ip 172.25.254.20unicast_peer {172.25.254.10}
}virtual_server 172.25.254.100 80 {delay_loop 6lb_algo wrrlb_kind DRprotocol TCPreal_server 172.25.254.110 80 {weight 1HTTP_GET {url {path /status_code 200}connect_timeout 3nb_get_retry 2delay_before_retry 2}}real_server 172.25.254.120 80 {weight 1HTTP_GET {url {path /status_code 200}connect_timeout 3nb_get_retry 2delay_before_retry 2}}
}virtual_server 172.25.254.200 80 {delay_loop 6lb_algo wrrlb_kind DRprotocol TCPreal_server 172.25.254.110 80 {weight 1HTTP_GET {url {path /status_code 200}connect_timeout 3nb_get_retry 2delay_before_retry 2}}real_server 172.25.254.120 80 {weight 1HTTP_GET {url {path /status_code 200}connect_timeout 3nb_get_retry 2delay_before_retry 2}}
}

lvskeepalived_356">4.重启lvs+keepalived服务

[root@KA1 ~]# systemctl restart ipvsadm.service  #lvs服务必须开
[root@KA1 ~]# systemctl restart keepalived.service
[root@KA2 ~]# systemctl restart ipvsadm.service
[root@KA2 ~]# systemctl restart keepalived.service

测试

vip测试

[root@KA1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 172.25.254.10  netmask 255.255.255.0  broadcast 172.25.254.255inet6 fe80::4e21:e4b4:36e:6d14  prefixlen 64  scopeid 0x20<link>ether 00:0c:29:a7:b6:fb  txqueuelen 1000  (Ethernet)RX packets 8373  bytes 2451524 (2.3 MiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 6303  bytes 625002 (610.3 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 172.25.254.100  netmask 255.255.255.0  broadcast 0.0.0.0ether 00:0c:29:a7:b6:fb  txqueuelen 1000  (Ethernet)lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0inet6 ::1  prefixlen 128  scopeid 0x10<host>loop  txqueuelen 1000  (Local Loopback)RX packets 56  bytes 4228 (4.1 KiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 56  bytes 4228 (4.1 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@KA2 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 172.25.254.20  netmask 255.255.255.0  broadcast 172.25.254.255inet6 fe80::7baa:9520:639b:5e48  prefixlen 64  scopeid 0x20<link>ether 00:0c:29:85:04:e5  txqueuelen 1000  (Ethernet)RX packets 8714  bytes 7279852 (6.9 MiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 4561  bytes 417141 (407.3 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0eth0:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 172.25.254.200  netmask 255.255.255.0  broadcast 0.0.0.0ether 00:0c:29:85:04:e5  txqueuelen 1000  (Ethernet)lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0inet6 ::1  prefixlen 128  scopeid 0x10<host>loop  txqueuelen 1000  (Local Loopback)RX packets 96  bytes 11546 (11.2 KiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 96  bytes 11546 (11.2 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

访问websever测试

  • 访问172.25.254.100
    在这里插入图片描述
  • 访问172.25.254.200
    在这里插入图片描述

高可用测试

  • 当KA1宕机后,vip会跑到KA2上

在这里插入图片描述

  • web服务正常

在这里插入图片描述

  • 当webserver1宕机后,keepalive也可以检测到,并会让webserver2提供web服务

在这里插入图片描述

haproxy+keepalived

  • 实验双主haproxy-dr模式
    在这里插入图片描述
  • 由于是双主模式,所以需要2个vipKA1为主时的vip172.25.254.100KA2为主时的vip172.25.254.200
  • KA1的真实IP172.25.254.10
  • 由于是lvs-dr模式,websever1webserver2上同样都必须有两个vip172.25.254.100 172.25.254.200
  • KA2的真实IP172.25.254.20
  • webserver1的真实IP172.25.254.110
  • webserver2的真实IP172.25.254.120

实验前的准备工作

  • 重置上面的实验环境,搭建新的环境

1.主机准备

  • 这里我们准备4台主机,两台web服务器两台keepalive服务器,简称KA

在这里插入图片描述

2.KA1和KA2上安装haproxy+keepalive

[root@KA1 ~]# yum install haproxy -y
[root@KA1 ~]# yum install keepalived -y
[root@KA2 ~]# yum install haproxy -y
[root@KA2 ~]# yum install keepalived -y

3.webserver1和webserver2上安装httpd

[root@webserver1 ~]# yum install httpd -y
[root@webserver2 ~]# yum install httpd -y

4.制作测试效果网页内容

[root@webserver1 ~]# echo webserver1-172.25.254.110 > /var/www/html/index.html
[root@webserver2 ~]# echo webserver2-172.25.254.120 > /var/www/html/index.html

linux_479">5.所有主机关闭firewalldselinux

[root@KA1 ~]# systemctl is-active httpd
inactive
[root@KA1 ~]# getenforce
Disabled
[root@KA2 ~]# systemctl is-active httpd
inactive
[root@KA2 ~]# getenforce
Disabled
[root@webserver1 ~]# systemctl is-active httpd
inactive
[root@webserver1 ~]# getenforce
Disabled
[root@webserver2 ~]# systemctl is-active httpd
inactive
[root@webserver2 ~]# getenforce
Disabled

6.开启httpd服务

[root@webserver1 ~]# systemctl enable --now httpd
[root@webserver2 ~]# systemctl enable --now httpd

实验步骤

1.KA1KA2两个节点启用内核参数

[root@KA1 ~]# vim /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind=1[root@KA1 ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
[root@KA2 ~]# vim /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind=1[root@KA2 ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1

2.配置haproxy.cfg配置文件

  • KA1haproxy.cfg文件末尾添加以下内容
[root@KA1 ~]# vim /etc/haproxy/haproxy.cfg
listen webserverbind 172.25.254.100:80,172.25.254.200:80mode httpbalance roundrobinserver web1 172.25.254.110:80 check inter 2 fall 3 rise 5server web2 172.25.254.120:80 check inter 2 fall 3 rise 5
  • KA2haproxy.cfg文件末尾添加以下内容
[root@KA2 ~]# vim /etc/haproxy/haproxy.cfg
listen webserverbind 172.25.254.100:80,172.25.254.200:80mode httpbalance roundrobinserver web1 172.25.254.110:80 check inter 2 fall 3 rise 5server web2 172.25.254.120:80 check inter 2 fall 3 rise 5

3.编写脚本,用于检测haproxy的状态

  • KA1
[root@KA1 ~]# vim /etc/keepalived/test.sh
#!/bin/bash
killall -0 haproxy[root@KA1 ~]# chmod +x /etc/keepalived/test.sh
  • KA2
[root@KA2 ~]# vim /etc/keepalived/test.sh
#!/bin/bash
killall -0 haproxy[root@KA2 ~]# chmod +x /etc/keepalived/test.sh

4.修改keepalived.conf配置文件

  • KA1
[root@KA1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {notification_email {3066136553@qq.com}notification_email_from keepalived@timinglee.orgsmtp_server 127.0.0.1smtp_connect_timeout 30router_id ka1.timinglee.orgvrrp_skip_check_adv_addr#vrrp_strictvrrp_garp_interval 0vrrp_gna_interval 0vrrp_mcast_group4 224.0.0.18
}vrrp_script check_haproxy {     #在虚拟路由模块的前面添加这个模块script "/etc/keepalived/test.sh"   #这里写检测脚本的路径interval 1weight -30   #当检测到haproxy挂掉后,降低优先级fall 2rise 2timeout 2
}vrrp_instance VI_1 {state MASTERinterface eth0virtual_router_id 100priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.254.100/24 dev eth0 label eth0:1}unicast_src_ip 172.25.254.10unicast_peer {172.25.254.20}track_script {    #在虚拟路由模块中添加这个小模块check_haproxy   #这里的名字要和上面vrrp_script模块中的名字一致}
}
vrrp_instance VI_2 {state BACKUPinterface eth0virtual_router_id 200priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.254.200/24 dev eth0 label eth0:2}unicast_src_ip 172.25.254.10unicast_peer {172.25.254.20}track_script {   #在虚拟路由模块中添加这个小模块check_haproxy   #这里的名字要和上面vrrp_script模块中的名字一致}
}
  • 在KA2上
[root@KA2 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {notification_email {3066136553@qq.com}notification_email_from keepalived@timinglee.orgsmtp_server 127.0.0.1smtp_connect_timeout 30router_id ka1.timinglee.orgvrrp_skip_check_adv_addr#vrrp_strictvrrp_garp_interval 0vrrp_gna_interval 0vrrp_mcast_group4 224.0.0.18
}vrrp_script check_haproxy {    #在虚拟路由模块的前面添加这个模块script "/etc/keepalived/test.sh"   #这里写检测脚本的路径interval 1weight -30   #当检测到haproxy挂掉后,降低优先级fall 2rise 2timeout 2
}vrrp_instance VI_1 {state BACKUPinterface eth0virtual_router_id 100priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.254.100/24 dev eth0 label eth0:1}unicast_src_ip 172.25.254.20unicast_peer {172.25.254.10}track_script {    #在虚拟路由模块中添加这个小模块check_haproxy   #这里的名字要和上面vrrp_script模块中的名字一致}
}vrrp_instance VI_2 {state MASTERinterface eth0virtual_router_id 200priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.254.200/24 dev eth0 label eth0:2}unicast_src_ip 172.25.254.20unicast_peer {172.25.254.10}track_script {   #在虚拟路由模块中添加这个小模块check_haproxy    #这里的名字要和上面vrrp_script模块中的名字一致}
}

5.重启haproxy+keepalived

[root@KA1 ~]# systemctl restart haproxy.service
[root@KA1 ~]# systemctl restart keepalived.service
[root@KA2 ~]# systemctl restart haproxy.service
[root@KA2 ~]# systemctl restart keepalived.service

测试

vip测试

  • KA1上
    在这里插入图片描述
  • KA2上
    在这里插入图片描述

访问websever测试

  • 访问vip1172.25.254.100

在这里插入图片描述

  • 访问vip2172.25.254.200

在这里插入图片描述

高可用测试

  • 当KA1宕机时,vip就会跑到KA2上

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • webserver1宕机时,keepalived会自动检测到,并让webserver2提供服务

在这里插入图片描述
在这里插入图片描述


http://www.ppmy.cn/devtools/99315.html

相关文章

基于Java爬取微博数据(二) 正文长文本+导出数据Excel

基于Java爬取微博数据二 正文长文本导出数据Excel 长文本补全导出微博数据到Excel注意点 上一篇文章简单讲述了基于Java爬取微博数据(一)&#xff0c;那么这篇将Java爬取的微博数据导出到Excel中。下面开始具体的操作。 长文本补全 在爬取微博数据的时候&#xff0c;大家可能…

电商api接口进行数据采集获取多平台商品价格

在电商运营中&#xff0c;从品牌角度来看&#xff0c;品牌方通过接口进行数据采集&#xff0c;获取多渠道商品价格信息的这一行为&#xff0c;能为品牌方带来诸多好处&#xff1a; 及时准确&#xff1a;API接口能为品牌提供实时数据&#xff0c;这意味着企业可以即时获取最新的…

Python优化算法09——黏菌优化算法(SMA)

科研里面优化算法都用的多&#xff0c;尤其是各种动物园里面的智能仿生优化算法&#xff0c;但是目前都是MATLAB的代码多&#xff0c;python几乎没有什么包&#xff0c;这次把优化算法系列的代码都从底层手写开始。 需要看以前的文章可以参考&#xff1a;Python优化算法_阡之尘…

springboot 通过ftp 上传文件

1. 服务器安装ftp服务 1.1 Linux 安装ftp服务器 yum -y install vsftpd (离线安装 ——见 文末 ) 关闭SELINUX /// 非必要项 修改/etc/selinux/config文件&#xff0c;把SELINUX参数的值改为disabled。 SELINUX disabled 启动vsftpd服务 ftp服务器的服务名是vsftpd&…

Python、R用RFM模型、机器学习对在线教育用户行为可视化分析|附数据、代码

全文链接&#xff1a;https://tecdat.cn/?p37409 分析师&#xff1a;Chunni Wu 随着互联网的不断发展&#xff0c;各领域公司都在拓展互联网获客渠道&#xff0c;为新型互联网产品吸引新鲜活跃用户&#xff0c;刺激用户提高购买力&#xff0c;从而进一步促进企业提升综合实力和…

配置PXE预启动执行环境:Kickstart自动化无人值守安装

文章目录 实现 Kickstart 无人值守安装1. 安装Kickstart和配置应答文件&#xff08;图形化界面&#xff09;2. 配置 PXE 菜单支持 Kickstart 无人值守安装3. 验证 Kickstart 无人值守安装4. 拓展&#xff1a;命令行配置应答文件&#xff08;命令行界面&#xff09; 实现 Kickst…

数据库查询大量数据避免内存溢出的方法

原理就是分批查询。每次查询一定数量数据之后记录id&#xff0c;进行数据处理之后再继续查询继续处理&#xff0c; allFrameObject mapper.findAllFrameObjectByMaxId(minTime, beginRow, 1000); while (CollectionUtils.isNotEmpty(allFrameObject)) {beginRow allFrameObj…

如何安装一个comfyui插件?ComfyUI_MiniCPM-V-2_6-int4为例

‍‍ #### 确认插件 requirements 里的依赖包都已经安装成功 Ensure all dependencies listed in the plugin requirements are successfully installed. #### 启动 ComfyUI 时的报错信息 When starting ComfyUI via cmd, you may encounter error messages. #### 安装缺少的依…