欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 培训 > Kubernetes学习指南:保姆级实操手册05——配置集群HA负载均衡

Kubernetes学习指南:保姆级实操手册05——配置集群HA负载均衡

2024/10/24 21:23:25 来源:https://blog.csdn.net/fq3758/article/details/141899506  浏览:    关键词:Kubernetes学习指南:保姆级实操手册05——配置集群HA负载均衡

五、Kubernetes学习指南:保姆级实操手册05——配置集群HA负载均衡

简介: Keepalived 提供 VRRP 实现,并允许您配置 Linux 机器使负载均衡,预防单点故障。HAProxy 提供可靠、高性能的负载均衡,能与 Keepalived 完美配合

1、配置Keepalive

官方文档提供了两种运行方式(此案例使用选项1):

  • 选项1:在操作系统上运行服务
  • 选项2:将服务作为静态pod运行

参考文档:[https://github.com/kubernetes/kubeadm/blob/main/docs/ha-considerations.md#options-for-software-load-balancing]

1.1、安装keepalived组件

:三台master节点上安装

yum install -y keepalived
1.2、配置keepalived
### 在k8s-master01上设置:
[root@k8s-m01 ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.back
[root@k8s-master01 keepalived]# cat > /etc/keepalived/keepalived.conf << EOF
! Configuration File for keepalived
global_defs {router_id k8s-master01
}
vrrp_script check_apiserver {script "/etc/keepalived/check_apiserver.sh"interval 3weight -2fall 10rise 2
}vrrp_instance VI_1 {state MASTERinterface ens192virtual_router_id 51priority 100authentication {auth_type PASSauth_pass 123456}virtual_ipaddress {10.255.210.99}track_script {check_apiserver}
}EOF### 在k8s-master02上设置:
$ mkdir /etc/keepalived [root@k8s-master02 keepalived]# cat > /etc/keepalived/keepalived.conf << EOF
! Configuration File for keepalived
global_defs {router_id k8s-master02
}
vrrp_script check_apiserver {script "/etc/keepalived/check_apiserver.sh"interval 3weight -2fall 10rise 2
}vrrp_instance VI_1 {state BACKUPinterface ens192virtual_router_id 51priority 99authentication {auth_type PASSauth_pass 123456}virtual_ipaddress {10.255.210.99}track_script {check_apiserver}
}EOF### 在k8s-master03上设置:
$ mkdir /etc/keepalived[root@k8s-master03 keepalived]# cat > /etc/keepalived/keepalived.conf << EOF
! Configuration File for keepalived
global_defs {router_id k8s-master03
}
vrrp_script check_apiserver {script "/etc/keepalived/check_apiserver.sh"interval 3weight -2fall 10rise 2
}vrrp_instance VI_1 {state BACKUPinterface ens192virtual_router_id 51priority 98authentication {auth_type PASSauth_pass 123456}virtual_ipaddress {10.255.210.99}track_script {check_apiserver}
}EOF

扩展:参数说明

参数说明:router_id:节点ip,master每个节点配置自己的IPmcast_src_ip:节点IP,master每个节点配置自己的IPvirtual_ipaddress:虚拟IP,即VIP。interface:指定接口的名称。virtual_router_id:有效值为0-255,可以理解为一个组ID,只有相同的ID才被确认为一个组。如果每个keepalived实例修改的ID不一致,则会出现各自有一个VIP的现象。    
1.3、编写健康检查脚本
[root@k8s-master01 keepalived]# cat > /etc/keepalived/check_apiserver.sh   <<EOF
#!/bin/sh  errorExit() {  echo "*** $*" 1>&2  exit 1  
}  curl --silent --max-time 2 --insecure https://localhost:16443/ -o /dev/null || errorExit "Error GET https://localhost:16443/"  
if ip addr | grep -q 10.255.210.99; then  
curl --silent --max-time 2 --insecure https://10.255.210.99:16443/ -o /dev/null || errorExit "Error GET https://10.255.210.99:16443/"  
fi  EOFchmod +x  /etc/keepalived/check_apiserver.shscp /etc/keepalived/check_apiserver.sh root@k8s-master02:/etc/keepalived/
scp /etc/keepalived/check_apiserver.sh root@k8s-master03:/etc/keepalived/
1.4、启动Keepalived
systemctl enable keepalived --now; systemctl restart keepalived.service ;systemctl status keepalived.service
1.5、测试keepalived
ip a   
#查看VIP在那个节点   systemctl stop keepalived.service   
#VIP所在节点停止服务,观察是否飘移VIP  systemctl restart keepalived.service   
#重启服务后,VIP将迁回

2、配置Haproxy

2.1、安装Haproxy
yum install -y haproxy
2.2、配置haproxy.cfg
globalmaxconn  2000ulimit-n  16384log  127.0.0.1 local0 errstats timeout 30sdefaultslog globalmode  httpoption  httplogtimeout connect 5stimeout client  50stimeout server  50stimeout http-request 15stimeout http-keep-alive 15sfrontend monitor-inbind *:33305mode httpoption httplogmonitor-uri /monitorlisten statsbind    *:8006mode    httpstats   enablestats   hide-versionstats   uri       /statsstats   refresh   30sstats   realm     Haproxy\ Statisticsstats   auth      admin:adminfrontend k8s-masterbind 0.0.0.0:16443bind 127.0.0.1:16443mode tcpoption tcplogtcp-request inspect-delay 5sdefault_backend k8s-masterbackend k8s-mastermode tcpoption tcplogoption tcp-checkbalance roundrobindefault-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 250 maxqueue 256 weight 100server k8s-master01 10.255.210.1:6443  checkserver k8s-master02 10.255.210.2:6443  checkserver k8s-master03 10.255.210.3:6443  check
2.3、启动haproxy
systemctl restart haproxy.service;systemctl status haproxy.service
2.4、查看端口
 ss -alnupt |grep 16443
tcp    LISTEN     0      2000      *:16443                 *:*                   users:(("haproxy",pid=53056,fd=6))
tcp    LISTEN     0      2000   127.0.0.1:16443                 *:*                   users:(("haproxy",pid=53056,fd=7))
[root@k8s-master02 ~]# ss -alnupt |grep 6443
tcp    LISTEN     0      2000      *:16443                 *:*                   users:(("haproxy",pid=53056,fd=6))
tcp    LISTEN     0      2000   127.0.0.1:16443                 *:*                   users:(("haproxy",pid=53056,fd=7))

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com