KEEPALIVED 全csdn最详细----理论+实验(干货扎实,包教会的)

news2024/11/17 1:35:39

环境准备


主机名IP虚拟IP(VIP)功能
ka1172.25.254.10172.25.254.100keepalived服务
ka2172.25.254.20172.25.254.100keepalived服务
realserver1172.25.254.110web服务
realserver2172.25.254.120web服务

注意一定要关闭selinux,和防火墙,不然在后面配置vrrp时不起作用

#在realserver1上配置
[root@realserver1 ~]# yum install httpd -y
[root@realserver1 ~]# echo realserver1 172.25.254.110 > /var/www/html/index.html
[root@realserver1 ~]# systemctl enable --now httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.


#在realserver2上配置
[root@realserver2 ~]# yum install httpd -y
[root@realserver2 ~]# echo realserver2 172.25.254.120 > /var/www/html/index.html
[root@realserver2 ~]# systemctl enable --now httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.





#在ka上面测试能否访问
[root@ka1 ~]# curl 172.25.254.110
realserver1 172.25.254.110
[root@ka1 ~]# curl 172.25.254.120
realserver2 172.25.254.120

[root@ka2 ~]# curl 172.25.254.110
realserver1 172.25.254.110
[root@ka2 ~]# curl 172.25.254.120
realserver2 172.25.254.120

在ka1,ka2上面配置keepalived:

[root@ka1 ~]#   yum install keepalived -y
[root@ka1 ~]#   systemctl enable --now keepalived


[root@ka2 ~]#   yum install keepalived -y
[root@ka2 ~]#   systemctl enable --now keepalived

keepalived基本配置

keepalived的主配置文件为/etc/keepalived/keepalived.conf,我们主要是在这里面进行配置

 在ka1上面的配置

首先打开配置文件,找到global_defs(全局配置),vrrp_instance VI_1 (配置虚拟子接口)进行如下配置:

[root@ka1 ~]# vim /etc/keepalived/keepalived.conf 


! Configuration File for keepalived

global_defs {
   notification_email {
        2787013485@qq.com
   }
   notification_email_from keepalived@xie.com
   smtp_server 172.0.0.1
   smtp_connect_timeout 30
   router_id ka1.xie.com
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_mcast_group4 224.0.0.18
   vrrp_iptables
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 100
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:1
    }
}

 其优先级为100,其作为主服务器。

 然后我们要重启keepalived服务,进行测试,其就出现VIP来供我们使用。

[root@ka1 ~]# systemctl restart keepalived
[root@ka1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.10  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::20c:29ff:fe67:d0cc  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:67:d0:cc  txqueuelen 1000  (Ethernet)
        RX packets 5119  bytes 363815 (355.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 9498  bytes 713779 (697.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.100  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:67:d0:cc  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 8  bytes 600 (600.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 600 (600.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@ka1 ~]# 

 如下图所示

修改ka2

[root@ka2 ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
        2787013485@qq.com
   }
   notification_email_from keepalived@xie.com
   smtp_server 172.0.0.1
   smtp_connect_timeout 30
   router_id ka1.xie.com
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_mcast_group4 224.0.0.18
   vrrp_iptables
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 100
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:1
    }
}

 重启测试,然后就发现ka2上是没有VIP的。

抓包测试

 [root@ka1 ~]# tcpdump -i eth0 -nn host 224.0.0.18

首先是172.25.254.10为主在发送数据。

 然后关闭ka1的keepalived服务再查看该走向,其VIP就会到ka2上面

 然后再重启ka1的keepalived服务,再查看,发现又出现了。

 使得ka1,ka2能够ping vip

最开始ping VIP172.25.254.100是ping不通的,想要ping通有以下两种方法

1.加上vrrp_iptables

在配置文件里面加上vrrp_iptables,就可以实现了。-----注意ka1,ka2都要设置,这样才能都实现

 测试界面如下:

 2.注释掉   vrrp_strict  
[root@ka1 ~]# vim /etc/keepalived/keepalived.conf


! Configuration File for keepalived

global_defs {
   notification_email {
        2787013485@qq.com
   }
   notification_email_from keepalived@xie.com
   smtp_server 172.0.0.1
   smtp_connect_timeout 30
   router_id ka1.xie.com
   vrrp_skip_check_adv_addr
  # vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_mcast_group4 224.0.0.18

}


[root@ka2 ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
        2787013485@qq.com
   }
   notification_email_from keepalived@xie.com
   smtp_server 172.0.0.1
   smtp_connect_timeout 30
   router_id ka1.xie.com
   vrrp_skip_check_adv_addr
  # vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_mcast_group4 224.0.0.18

}

#测试注释掉  # vrrp_strict  ping 172.25.254.100 能否ping通
[root@ka1 ~]# systemctl restart keepalived
[root@ka1 ~]# ping 172.25.254.100
PING 172.25.254.100 (172.25.254.100) 56(84) bytes of data.
64 bytes from 172.25.254.100: icmp_seq=1 ttl=64 time=0.011 ms
64 bytes from 172.25.254.100: icmp_seq=2 ttl=64 time=0.059 ms
64 bytes from 172.25.254.100: icmp_seq=3 ttl=64 time=0.054 ms
64 bytes from 172.25.254.100: icmp_seq=4 ttl=64 time=0.037 ms



#测试注释掉  # vrrp_strict  ping 172.25.254.100 能否ping通
[root@ka2 ~]# systemctl restart keepalived
[root@ka2 ~]# ping 172.25.254.100
PING 172.25.254.100 (172.25.254.100) 56(84) bytes of data.
64 bytes from 172.25.254.100: icmp_seq=1 ttl=64 time=0.309 ms
64 bytes from 172.25.254.100: icmp_seq=2 ttl=64 time=0.750 ms
64 bytes from 172.25.254.100: icmp_seq=3 ttl=64 time=0.613 ms
^C
--- 172.25.254.100 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.309/0.557/0.750/0.185 ms
[root@ka2 ~]#
 启用keepalived日志

[root@ka1 ~]# vim /etc/sysconfig/keepalived


# Options for keepalived. See `keepalived --help' output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp               -P    Only run with VRRP subsystem.
# --check              -C    Only run with Health-checker subsystem.
# --dont-release-vrrp  -V    Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs  -I    Dont remove IPVS topology on daemon stop.
# --dump-conf          -d    Dump the configuration data.
# --log-detail         -D    Detailed log messages.
# --log-facility       -S    0-7 Set local syslog facility (default=LOG_DAEMON)
#

KEEPALIVED_OPTIONS="-D -S 6"





[root@ka1 ~]# vim /etc/rsyslog.conf
#-----------------------------------------省略---------------------------------------------
# Save boot messages also to boot.log
local7.*                                                /var/log/boot.log
local6.*                                                /var/log/keepalived
#-----------------------------------------省略-------------------------------------------


[root@ka1 ~]#  systemctl restart rsyslog.service
[root@ka1 ~]#  systemctl restart keepalived.service


[root@ka1 ~]# ll /var/log/keepalived.log
-rw------- 1 root root 4005 Aug 17 16:07 /var/log/keepalived.log


[root@ka1 ~]# tail -f  /var/log/keepalived.log
Aug 17 16:07:38 ka1 Keepalived_healthcheckers[55872]: Timeout connecting server [192.168.201.100]:443.
Aug 17 16:07:38 ka1 Keepalived_healthcheckers[55872]: Check on service [192.168.201.100]:443 failed after 3 retry.
Aug 17 16:07:38 ka1 Keepalived_healthcheckers[55872]: Removing service [192.168.201.100]:443 from VS [192.168.200.100]:443
Aug 17 16:07:38 ka1 Keepalived_healthcheckers[55872]: Lost quorum 1-0=1 > 0 for VS [192.168.200.100]:443
Aug 17 16:07:38 ka1 Keepalived_healthcheckers[55872]: Remote SMTP server [172.0.0.1]:25 connected.
Aug 17 16:07:53 ka1 Keepalived_healthcheckers[55872]: Error reading data from remote SMTP server [172.0.0.1]:25.


实现独立子配置文件

 将原来的主配置文件的虚拟路由部分注释掉,添 include"/etc/keepalived/conf.d/*.conf",重新写一个子配置文件

[root@ka1 ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
        2787013485@qq.com
   }
   notification_email_from keepalived@xie.com
   smtp_server 172.0.0.1
   smtp_connect_timeout 30
   router_id ka1.xie.com
   vrrp_skip_check_adv_addr
  # vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_mcast_group4 224.0.0.18
  # vrrp_iptables
}

#vrrp_instance VI_1 {
#    state MASTER
#    interface eth0
#    virtual_router_id 100
#    priority 100
#    advert_int 1
#    authentication {
#        auth_type PASS
#        auth_pass 1111
#    }
#    virtual_ipaddress {
#       172.25.254.100/24 dev eth0 label eth0:1
#    }
#}
include "/etc/keepalived/conf.d/*.conf"




[root@ka1 ~]# mkdir -p /etc/keepalived/conf.d
[root@ka1 ~]#  vim /etc/keepalived/conf.d/172.25.254.100.conf

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 100
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       172.25.254.100/24 dev eth0 label eth0:1
    }
}

[root@ka1 ~]#  systemctl restart keepalived.service
#测试
[root@ka1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.10  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::20c:29ff:fe67:d0cc  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:67:d0:cc  txqueuelen 1000  (Ethernet)
        RX packets 35127  bytes 2447462 (2.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 71612  bytes 7215886 (6.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.100  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:67:d0:cc  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 35  bytes 2896 (2.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 35  bytes 2896 (2.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 keepalived企业应用实例

抢占模式和非抢占模式
非抢占模式
nopreempt

默认为抢占模式preempt,即当高优先级的主机恢复在线后,会抢占低先级的主机的master角色,这样会使vip在KA主机中来回漂移,造成网络抖动,建议设置为非抢占模式 nopreempt ,即高优先级主机恢复后,并不会抢占低优先级主机的master角色。

非抢占模块下,如果原主机down机, VIP迁移至的新主机, 后续也发生down时,仍会将VIP迁移回原主机。

 

注意:要关闭 VIP抢占,必须将各 keepalived 服务器state配置为BACKUP

ka1上主配置文件的配置:添加nopreempt,并修改BACKUP。

[root@ka1 ~]# vim /etc/keepalived/keepalived.conf


vrrp_instance VI_1 {
    state BACKUP     #改为BACKUP
    interface eth0
    virtual_router_id 100
    priority 100
    nopreempt        #添加非抢占模式
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       172.25.254.100/24 dev eth0 label eth0:1
    }
}
[root@ka1 ~]#  systemctl restart keepalived.service



[root@ka2 ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 100
    priority 80
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:1
    }
}

[root@ka2 ~]# systemctl restart keepalived

抢占延迟模式 preempt_delay
抢占延迟模式,即优先级高的主机恢复后,不会立即抢回 VIP ,而是延迟一段时间(默认 300s)再抢回 VIP
preempt_delay # # 指定抢占延迟时间为 #s ,默认延迟 300s
注意:需要各 keepalived 服务器 state 为 BACKUP, 并且不要启用 vrrp_strict



[root@ka1 ~]# vim /etc/keepalived/keepalived.conf

# KA1主机配置抢占延迟模式

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 100
    priority 100
    advert_int 1
    preempt_delay 5s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:1
   }
}

[root@ka2 ~]# vim /etc/keepalived/keepalived.conf

# KA2上的非抢占模式与KA1相同,加上preempt_delay 5s参数即可
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 100
    priority 100
    advert_int 1
    preempt_delay 5s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:1
   }
}

 然后我们可以 通过stop再进行restart   keepalived

 查看VIP,间隔时间为抢占延时时间。做完这个实验之后建议删掉或者注释·此配置。

4.3 VIP单播配置

默认 keepalived 主机之间利用多播相互通告消息,会造成网络拥塞,可以替换成单播,减少网络流量。

注意:启用 vrrp_strict 时,不能启用单播

# KA1配置组播变单播

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 100
    priority 100
    advert_int 1
    #nopreempt
    #preempt_delay 5s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:1
   }
   unicast_src_ip 172.25.254.10
   unicast_peer {
        172.25.254.20
   }
}

# KA2配置组播变单播

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 100
    priority 80
    advert_int 1
    #nopreempt
    #preempt_delay 5s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:1
    }
    unicast_src_ip 172.25.254.20
    unicast_peer {
        172.25.254.10
    }

}

此时VIP在KA1上,利用抓包工具抓到从KA1传给KA2的流量

现在关掉KA1再做测试

 

 此时VIP跑到KA2上,重新开启KA1后,VIP又会重新回到KA1上

keepalived通知脚本配置

邮件配置

安装邮件发送工具mailx,KA1和KA2都需要安装

[root@ka1 ~]# yum install mailx -y

QQ邮箱配置

[root@ka1 ~]# vim /etc/mail.rc

# For Linux and BSD, this should be set.
set bsdcompat
set from=2787013485@qq.com  #自己的QQ邮箱
set smtp=smtp.qq.com
set smtp-auth-user=2787013485@qq.com
set smtp-auth-password=umyqzzecpjhqddbc  #在网页QQ邮箱中申请自己的授权码
set smtp-auth=login
set ssl-verify=ignore

[root@ka1 ~]# vim /etc/mail.rc
[root@ka1 ~]#  echo hello world | mail -s test 2787013485@qq.com

测试发送邮箱:


[root@ka1 ~]# vim /etc/keepalived/mail.sh
#!/bin/bash
mail_dest='2787013485@qq.com'
send_message()
{
  mail_subj="$HOSTNAME to be $1 vip move"
  mail_mess="`date +%F\ %T`: vrrp move $HOSTNAME change $1"
  echo "$mail_mess" | mail -s "$mail_sub" $mail_dest
}

case $1 in
  master)
  send_message master
  ;;
  backup)
  send_message backup
  ;;
  fault)
  send_message fault
  ;;
  *)
  ;;
esac

# 给脚本赋予权限
[root@ka1 ~]# chmod +x /etc/keepalived/mail.sh
# 执行脚本观察QQ邮箱
[root@ka1 ~]# /etc/keepalived/mail.sh master


# 编辑配置文件实行脚本的调用
[root@ka1 ~]# vim /etc/keepalived/keepalived.conf
=================省略==================
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 100
    priority 100
    advert_int 1
    #nopreempt
    #preempt_delay 5s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:1
   }
   unicast_src_ip 172.25.254.10
   unicast_peer {
        172.25.254.20
   }
   notify_master "/etc/keepalived/mail.sh master"
   notify_backup "/etc/keepalived/mail.sh backup"
   notify_fault "/etc/keepalived/mail.sh fault"
}
=================省略==================

# 重启服务
[root@ka1 ~]# systemctl restart keepalived.service

#KA2也需要跟KA1做同样的操作,如果不想写可以直接复制
[root@ka2 keepalived]# scp -v 172.25.254.10:/etc/keepalived/mail.sh /etc/keepalived/

# 最后停掉KA1的keepalived,然后在自己的QQ邮箱中查看是否发了邮件

 

实现master/master的Keepalived双主架构
双主架构用途:

master/slave的单主架构,同一时间只有一个Keepalived对外提供服务,此主机繁忙,而另一台主机却很空闲,利用率低下,可以使用master/master的双主架构,解决此问题。

即将两个或以上VIP分别运行在不同的keepalived服务器,以实现服务器并行提供web访问的目的,提高服务器资源利用率。

# KA1主机
[root@ka1 ~]# vim /etc/keepalived/keepalived.conf
=================省略==================
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 100
    priority 100
    advert_int 1
    #nopreempt
    #preempt_delay 5s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.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 MASTER
    interface eth0
    virtual_router_id 200
    priority 80
    advert_int 1
    #nopreempt
    #preempt_delay 5s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.200/24 dev eth0 label eth0:2
   }
   unicast_src_ip 172.25.254.10
   unicast_peer {
        172.25.254.20
   }
}
=================省略==================

#KA2上
[root@ka2 ~]# vim /etc/keepalived/keepalived.conf
=================省略==================
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 100
    priority 80
    advert_int 1
    #nopreempt
    #preempt_delay 5s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:1
    }
    unicast_src_ip 172.25.254.20
    unicast_peer {
        172.25.254.10
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 200
    priority 100
    #advert_int 1
    #nopreempt
    preempt_delay 5s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.200/24 dev eth0 label eth0:2
   }
   unicast_src_ip 172.25.254.20
   unicast_peer {
        172.25.254.10
   }
}
=================省略==================

 

 此时两台Keepalived主机上都分别有一个VIP

# 在两台后端RealServer上安装httpd
[root@realserver1 ~]# yum install httpd -y
[root@realserver1 ~]# echo realserver1 - 172.25.254.110 > /var/www/html/index.html
[root@realserver1 ~]# systemctl enable --now httpd.service 

# 配置环回IP地址
[root@realserver1 ~]# ip a a 172.25.254.100/32 dev lo

# 禁止ARP响应
[root@realserver1 ~]# vim /etc/sysctl.d/arp.conf
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
net.ipv4.conf.lo.arp_ignore=1
net.ipv4.conf.lo.arp_announce=2

[root@realserver2 ~]# sysctl --p
[root@realserver2 ~]# sysctl --system

# RealServer2也做同样的配置
[root@realserver2 ~]# yum install httpd -y
[root@realserver2 ~]# echo realserver2 - 172.25.254.120 > /var/www/html/index.html
[root@realserver2 ~]# systemctl enable --now httpd.service 

# 配置环回IP地址
[root@realserver2 ~]# ip a a 172.25.254.100/32 dev lo

# 禁止ARP响应
[root@realserver2 ~]# vim /etc/sysctl.d/arp.conf
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
net.ipv4.conf.lo.arp_ignore=1
net.ipv4.conf.lo.arp_announce=2

[root@realserver2 ~]# sysctl --p
[root@realserver2 ~]# sysctl --system
# 在两台Keepalived主机上安装ipvsadm
#KA1
[root@ka1 ~]# yum install ipvsadm -y

# 在Keepalived配置文件中配置Keepalived
[root@ka1 ~]# vim /etc/keepalived/keepalived.conf

=================省略==================
virtual_server 172.25.254.100 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    #persistence_timeout 50
    protocol TCP

    real_server 172.25.254.110 80 {
        weight 1
        HTTP_GET {
            url {
              path /
             status_code 200
            }
            connect_timeout 3
            nb_get_retry 2
            delay_before_retry 2
        }
    }
    real_server 172.25.254.120 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 2
            delay_before_retry 2
        }
    }
}
------------省略--------------

# KA2
[root@ka2 ~]# vim /etc/keepalived/keepalived.conf 
=================省略==================
virtual_server 172.25.254.100 80 {
    delay_loop 6
    lb_algo wrr
    lb_kind DR
    #persistence_timeout 50
    protocol TCP

    real_server 172.25.254.110 80 {
        weight 1
        HTTP_GET {
            url {
              path /
             status_code 200
            }
            connect_timeout 3
            nb_get_retry 2
            delay_before_retry 2
        }
    }
    real_server 172.25.254.120 80 {
        weight 1
        HTTP_GET {
            url {
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 2
            delay_before_retry 2
        }
    }
}
=================省略==================
# 重启服务,在测试端进行访问测试结果
# 检查一下lvs策略
[root@ka1 ~]# systemctl restart keepalived.service 
[root@ka1 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.25.254.100:80 wrr
  -> 172.25.254.110:80            Route   1      0          0         
  -> 172.25.254.120:80            Route   1      0          0        
  
  
[root@test ~]# while true; do curl 172.25.254.100; sleep 0.5; done
realserver1 - 172.25.254.110
realserver2 - 172.25.254.120
realserver1 - 172.25.254.110
realserver2 - 172.25.254.120
realserver1 - 172.25.254.110
realserver2 - 172.25.254.120
realserver1 - 172.25.254.110
realserver2 - 172.25.254.120

# 模拟故障
#第一台RS1故障,自动切换至RS2
[root@realserver1 ~]# systemctl stop httpd #当RS1故障
[root@realserver1 ~]# while true; do curl 172.25.254.100; sleep 0.5; done
#全部流浪被定向到RS2中
realserver2 - 172.25.254.120
realserver2 - 172.25.254.120
realserver2 - 172.25.254.120
realserver2 - 172.25.254.120
realserver2 - 172.25.254.120
realserver2 - 172.25.254.120

#RS1策略被踢出保留RS2的lvs策略
[root@ka1 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.25.254.100:80 wrr   
  -> 172.25.254.120:80            Route   1      0          0        

# 在两台后端RealServer上删掉环回地址
[root@realserver1 ~]# ip a d 172.25.254.100/32 dev lo

# 开启ARP响应
[root@realserver1 ~]# vim /etc/sysctl.d/arp.conf
net.ipv4.conf.all.arp_ignore=0
net.ipv4.conf.all.arp_announce=0
net.ipv4.conf.lo.arp_ignore=0
net.ipv4.conf.lo.arp_announce=0

# 注释掉在Keepalived主机上的LVS策略部分

# 两台keepalived主机上安装HAProxy
[root@ka1 ~]# yum install haproxy -y

#在两个ka1和ka2两个节点启用内核参数
[root@ka1 ~]# vim /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
[root@ka1 ~]# sysctl -p

# 在两个ka1和ka2实现haproxy的配置
[root@ka1 ~]# vim /etc/haproxy/haproxy.cfg

=================省略==================
isten webcluster
    bind 172.25.254.100:80
    mode http
    balance roundrobin
    server web1 172.25.254.110:80 check inter 3 fall 2 rise 5
    server web2 172.25.254.120:80 check inter 3 fall 2 rise 5


# 编写脚本监测HAProxy状态
[root@ka1 ~]# vim /etc/keepalived/test.sh
#!/bin/bash
killall -0 haproxy

# 给脚本执行权限
[root@ka1 ~]# chmod +x /etc/keepalived/test.sh

# 在ka1中配置keepalived
[root@ka1 ~]# vim /etc/keepalived/keepalived.conf 

=================省略==================
vrrp_script check_haproxy {
        script "/etc/keepalived/test.sh"
        interval 1
        weight -30
        fall 2
        rise 2
        timeout 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 100
    priority 100
    advert_int 1
    #nopreempt
    #preempt_delay 5s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:1
   }
   unicast_src_ip 172.25.254.10
   unicast_peer {
        172.25.254.20
   }
   track_script {
      check_haproxy
   }

}

vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 200
    priority 80
    advert_int 1
    #nopreempt
    #preempt_delay 5s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.200/24 dev eth0 label eth0:2
   }
   unicast_src_ip 172.25.254.10
   unicast_peer {
        172.25.254.20
   }
}

=================省略==================

# 在KA2上配置Keepalived
[root@ka2 ~]# vim /etc/keepalived/keepalived.conf

=================省略==================
vrrp_script check_haproxy {
        script "/etc/keepalived/test.sh"
        interval 1
        weight -30
        fall 2
        rise 2
        timeout 2
}


vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 100
    priority 80
    advert_int 1
    #nopreempt
    #preempt_delay 5s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.100/24 dev eth0 label eth0:1
    }
    unicast_src_ip 172.25.254.20
    unicast_peer {
        172.25.254.10
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 200
    priority 100
    #advert_int 1
    #nopreempt
    preempt_delay 5s
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.200/24 dev eth0 label eth0:2
   }
   unicast_src_ip 172.25.254.20
   unicast_peer {
        172.25.254.10
   }
   track_script {
      check_haproxy
   }

}
=================省略==================

# 重启服务进行测试

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2049617.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

zabbix7.0 设置中文语言( Debian GNU/Linux 12)

本例为安装zabbix7.0 zabbix_server (Zabbix) 6.4.17 Revision c12261f00b4 15 July 2024, compilation time: Jul 15 2024 11:05:06 系统版本信息为 lsb_release -a No LSB modules are available. Distributor ID: Debian Description: Debian GNU/Linux 12 (bookworm) Rele…

防疫物资管理信息系统pf

TOC springboot379防疫物资管理信息系统pf 第1章 绪论 1.1选题动因 当前的网络技术&#xff0c;软件技术等都具备成熟的理论基础&#xff0c;市场上也出现各种技术开发的软件&#xff0c;这些软件都被用于各个领域&#xff0c;包括生活和工作的领域。随着电脑和笔记本的广泛…

【Django开发】前后端分离django美多商城项目第2篇:展示用户注册页面,1. 创建用户模块子应用【附代码文档】

全套笔记资料代码移步&#xff1a; 前往gitee仓库查看 感兴趣的小伙伴可以自取哦~ 本教程的知识点为&#xff1a; 项目准备 项目准备 配置 1. 修改settings/dev.py 文件中的路径信息 2. INSTALLED_APPS 3. 数据库 用户部分 图片 1. 后端接口设计&#xff1a; 视图原型 2. 具体…

【网络安全】SSO登录过程实现账户接管

未经许可,不得转载。 文章目录 正文正文 登录页面展示了“使用 SSO 登录”功能: 经分析,单点登录(SSO)系统的身份验证过程如下: 1、启动SSO流程:当用户点击按钮时,浏览器会发送一个GET请求到指定的URL: /idp/auth/mid-oidc?req=[UNIQUE_ID]&redirect_uri=[REDI…

在 Mac 上更改 24小时制时间显示

使用“日期与时间”设置设定或更改 Mac 上的日期和时间。如果日期和时间正确&#xff0c;那么电子邮件、信息和文件上的时间戳也是准确的。了解如何设定日期和时间。 若要更改这些设置&#xff0c;请选取苹果菜单 >“系统设置”&#xff0c;点按边栏中的“通用” &#x…

[星瞳科技]OpenMV使用时有哪些常见错误和解决办法?

常见代码错误 ImportError:no module named xxx 这个错误是Import错误&#xff0c;没有stepper这个模块。 原因&#xff1a; 你没有把stepper.py这个文件拖到你的板子里。见&#xff1a;模块的使用 拖过去之后&#xff0c;需要重启&#xff0c;使模块生效 MemoryError:FB …

Class字节码文件结构

class字节码文件结构 类型名称说明长度数量u4magic魔数,识别Class文件格式4个字节1u2minor_version副版本号(小版本)2个字节1u2major_version主版本号(大版本)2个字节1u2constant_pool_count常量池计数器2个字节1cp_infoconstant_pool常量池表n个字节constant_pool_count-1u2a…

马头拧紧驱动器维修 拧紧控制器故障

马头拧紧控制器作为工业自动化领域不可或缺的重要设备&#xff0c;其稳定运行对于生产线的效率与安全性至关重要。然而&#xff0c;在实际应用中&#xff0c;难免会遇到各种Desoutter拧紧工具控制器故障&#xff0c;影响生产进度和设备性能。 拧紧轴控制器维修 拧紧装置 马头…

ubuntu设置共享文件夹,非虚拟机,服务器版

在Ubuntu中共享文件夹通常可以通过几种不同的方式来实现&#xff0c;比如使用Samba服务、NFS&#xff08;Network File System&#xff09;或者通过虚拟机软件如VirtualBox或VMware的内置共享文件夹功能。这里我假设您是在询问如何在Ubuntu主机上设置一个简单的文件共享服务&am…

MongoDB Redis 快速上手:NoSQL数据库操作精要

先言之 ☘️随着大数据时代的到来&#xff0c;非关系型数据库因其灵活性和扩展性逐渐受到开发者的青睐。MongoDB 和 Redis 作为两种非常流行的 NoSQL 数据库&#xff0c;各自拥有独特的特性和应用场景。MongoDB 是一款面向文档的数据库&#xff0c;适用于需要存储复杂数据结构…

ESP32-C3在MQTT访问时出现“transport_base: Poll timeout or error”问题的分析(2)

接前一篇文章:ESP32-C3在MQTT访问时出现“transport_base: Poll timeout or error”问题的分析(1) 前一篇文章在分析定位笔者所遇MQTT(MQTTs)传输时问题的时候,定位到了问题是出自于components\components\tcp_transport\transport_ssl.c的ssl_write函数。本回开始,就围…

【四】阿伟开始学Kafka

阿伟开始学Kafka 概述 人生若只如初见&#xff0c;阿伟心里回想起了第一次和Kafka见面的场景&#xff0c;记忆虽然已经有些模糊&#xff0c;但是感觉初次见面是美好的。积累了一些实战经验之后&#xff0c;阿伟感觉不能再是面对百度开发了&#xff0c;于是决心系统的学习一下Ka…

liblzma库Android平台编译

1.下载源码: git clone https://github.com/tukaani-project/xz.git --recursive 2.配置交叉编译环境: 生成Android平台makefile export ANDROID_API=25 export ANDROID_NDK=/opt/aarch64-darwin-android export ANDROID_NDK_REVISION=r25b export AR=/opt/aarch64-darwin-a…

李宏毅 机器学习与深度学习【2022版】 01

文章目录 一、基本概念二、深度学习内容总览三、预测YouTube播放量的模型1、假设一个含有未知参数的函数式2、根据Training Data定义一个 Loss3、最优化Optimization4、测试集验证模型性能5、线性模型特征维度提升6、非线性模型7、ReLU 四、深度学习概述1、Fully Connect Feedf…

基于改进YOLOv8的景区行人检测算法

贵向泉, 刘世清, 李立, 秦庆松, 李唐艳. 基于改进YOLOv8的景区行人检测算法[J]. 计算机工程, 2024, 50(7): 342-351. DOI: 10.19678/j.issn.10 原文链接如下&#xff1a;基于改进YOLOv8的景区行人检测算法https://www.ecice06.com/CN/rich_html/10.19678/j.issn.1000-3428.006…

DOM Clobbring个人理解

目录 toString One Level Two Level Three Level More Dom Clobbering&#xff1a;就是⼀种将 HTML 代码注⼊⻚⾯中以操纵 DOM 并最终更改⻚⾯上 JavaScript ⾏为的技术 DOM Clobbering中的操作也是根据JavaScript行为的层级来分为一层、两层、三层和更多 toString 我们…

集合及数据结构第一节————初识集合框架和数据结构

系列文章目录 集合及数据结构第一节————初识集合框架和数据结构 初始集合框架和数据结构 什么是集合框架&#xff1f;集合框架的重要性背后所涉及的数据结构以及算法数据结构的基本概念和术语逻辑结构和物理结构数据类型 文章目录 系列文章目录集合及数据结构第一节——…

机器学习/自主系统与亚当·斯密

人工智能中的机器学习和自主系统是当前科技领域的热门话题&#xff0c;它们与亚当斯密的经济学理论之间可能存在一些潜在的联系和启示。亚当斯密的经济学理论主要关注市场经济的运行和资源分配。他的核心观点是&#xff0c;通过市场机制的作用&#xff0c;个体追求自身利益的行…

算法日记day 41(动归之最长序列问题)

一、最长递增子序列 题目&#xff1a; 给你一个整数数组 nums &#xff0c;找到其中最长严格递增子序列的长度。 子序列 是由数组派生而来的序列&#xff0c;删除&#xff08;或不删除&#xff09;数组中的元素而不改变其余元素的顺序。例如&#xff0c;[3,6,2,7] 是数组 [0…

一款免费的开源支付网关系统,支持X宝、某信、云闪付等多种支付方式,提供收单、退款、聚合支付、对账、分账等功能(附源码)

前言 在数字化浪潮中&#xff0c;电子-商务和移动支付迅速崛起&#xff0c;支付系统成为企业运营的核心组件。然而&#xff0c;现有支付处理方案常面临成本高、集成复杂、系统耦合度高等挑战。 这些问题不仅增加了企业负担&#xff0c;也制约了业务的快速迭代和创新。市场迫切…