搭建高可用 k8s master 基于 Haproxy 和 Keepalived

此文档只描述搭建高可用 Haproxy 和 Keepalived 用虚拟 ip 代理 api-server 监听的 6443 端口,不介绍高可用 master 节点上的相关 k8s 组件。

三台 master 节点和虚拟 ip,下面我将用 01、02、03 来代替各三台 master 节点:

10.1.11.77  k8s-master01

10.1.11.78  k8s-master02

10.1.11.79  k8s-master03

10.1.11.236  keepalived 的虚拟 ip(请确保为局域网内的空闲 ip,且与 k8s 节点同一网段)

三台节点安装 Haproxy 和 Keepalived,

yum install keepalived haproxy -y

Haproxy

编辑 Haproxy 的配置文件,三台节点的配置文件相同。请注意前端配置 k8s-master,我这里代理端口为 16443,因此记得修改 kubeconfig 配置文件中的 api-server 终点 'controlPlaneEndpoint: 10.1.11.236:16443'。若是想用默认端口 6443,则 haproxy 配置中的前端 bind 修改为一行 '10.1.11.236:6443',相应 kubeconfig 配置文件中的 api-server 终点 'controlPlaneEndpoint: 10.1.11.236:6443'。controlPlaneEndpoint 对应的 ip 一定为虚拟 ip,才能实现高可用。

配置中的后端 ip 则需要修改为自己集群内的 ip 地址和主机名。

vim /etc/haproxy/haproxy.cfg
global
  maxconn  2000
  ulimit-n  16384
  log  127.0.0.1 local0 err
  stats timeout 30s

defaults
  log global
  mode  http
  option  httplog
  timeout connect 5000
  timeout client  50000
  timeout server  50000
  timeout http-request 15s
  timeout http-keep-alive 15s

frontend monitor-in
  bind *:33305
  mode http
  option httplog
  monitor-uri /monitor

frontend k8s-master
  bind 0.0.0.0:16443 #想用默认端口 6443,则修改为一行 bind 10.1.11.236:6443
  bind 127.0.0.1:16443
  mode tcp
  option tcplog
  tcp-request inspect-delay 5s
  default_backend k8s-master

backend k8s-master
  mode tcp
  option tcplog
  option tcp-check
  balance roundrobin
  default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 250 maxqueue 256 weight 100
  server k8s-master01	10.1.11.77:6443  check #主机名和 ip 修改为自己集群的 
  server k8s-master02	10.1.11.78:6443  check
  server k8s-master03	10.1.11.79:6443  check

Keepalived

keepalived 的配置每台 master 节点是不一样的,下面是 01 的配置文件,注释中会描述出 02、03 的配置。

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
    router_id LVS_DEVEL
    script_user root
    enable_script_security
}
vrrp_script chk_apiserver {
    script "/etc/keepalived/check_apiserver.sh"
    interval 5
    weight -5
    fall 2  
    rise 1
}
vrrp_instance VI_1 {
    state MASTER  #02、03 这里修改为  state BACKUP
    interface bond0  #修改为对应的网卡接口
    mcast_src_ip 10.1.11.77 #02修改为 mcast_src_ip 10.1.11.78,03修改为 mcast_src_ip 10.1.11.79
    virtual_router_id 51
    priority 101  #02 这里修改为 priority 100,03 这里修改为 priority 99
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass K8SHA_KA_AUTH
    }
    virtual_ipaddress {
        10.1.11.236 #你可以修改为自己想要的虚拟 ip 地址,三台机器配置相同
    }
    track_script {
       chk_apiserver
    }
}

编写 keepalived 的健康检查脚本。脚本的目的是通过检查 HAProxy 进程的运行状态来确定服务的健康状态。如果该节点上的 HAProxy 进程未运行,脚本将停止对应节点上的 Keepalived 服务,以避免在主服务器未能正常运行的情况下继续提供服务。

vim /etc/keepalived/check_apiserver.sh
#!/bin/bash

err=0
for k in $(seq 1 3)
do
    check_code=$(pgrep haproxy)
    if [[ $check_code == "" ]]; then
        err=$(expr $err + 1)
        sleep 1
        continue
    else
        err=0
        break
    fi
done

if [[ $err != "0" ]]; then
    echo "systemctl stop keepalived"
    /usr/bin/systemctl stop keepalived
    exit 1
else
    exit 0
fi

给脚本加上可执行权限

chmod +x /etc/keepalived/check_apiserver.sh

启动 Haproxy 和 Keepalived

三台 master 节点上启动 haproxy 和 keepalived

systemctl daemon-reload && systemctl enable --now haproxy && systemctl enable --now keepalived

三台节点上测试 Keepalived 的虚拟 ip 

[root@k8s-master01 ~]# ping 10.1.11.236 -c 4
PING 10.1.11.236 (10.1.11.236) 56(84) bytes of data.
64 bytes from 10.1.11.236: icmp_seq=1 ttl=64 time=0.464 ms
64 bytes from 10.1.11.236: icmp_seq=2 ttl=64 time=0.063 ms
64 bytes from 10.1.11.236: icmp_seq=3 ttl=64 time=0.062 ms
64 bytes from 10.1.11.236: icmp_seq=4 ttl=64 time=0.063 ms

--- 10.1.11.236 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3106ms
rtt min/avg/max/mdev = 0.062/0.163/0.464/0.173 ms
[root@k8s-master01 ~]# telnet 10.1.11.236 16443
Trying 10.1.11.236...
Connected to 10.1.11.236.
Escape character is '^]'.
Connection closed by foreign host.

如果ping不通且telnet没有出现 ] ,则认为VIP不可以,需要排查keepalived的问题,比如防火墙和selinux,haproxy和keepalived的状态,监听端口等

所有节点查看防火墙状态必须为disable和inactive:systemctl status firewalld

所有节点查看selinux状态,必须为disable:getenforce

master节点查看haproxy和keepalived状态:systemctl status keepalived haproxy

master节点查看监听端口:netstat -anplut |grep 16443

若是以上都没问题,且为为云服务器,需要和云服务器管理员一起排查问题。

代理其他服务

场景不同可以忽略此块内容。k8s 集群三台 master 节点上同样有部署 DNS 服务和 ingress(nginx)服务 nodeport 暴露方式,也给其他服务添加上高可用,只需要需改 Haproxy 的配置文件。

在 Haproxy 的配置文件中追加以下配置,三台节点配置相同。

DNS服务监听得是 0.0.0.0。然后内网机器填写 DNS 服务器得时候可以填写 10.1.11.236。则可以实现 DNS 的高可用。

集群内的 ingress 服务。再给 k8s 内的服务配置域名得时候,解析得 ip 就可以填写为 vip:10.1.11.236。则可以实现解析得高可用。

frontend ft_80
  bind 10.1.11.236:80
  mode http
  tcp-request inspect-delay 5s
  default_backend bk_80

backend bk_80
  mode http
  balance roundrobin
  server k8s-master01	10.1.11.77:80  check
  server k8s-master02	10.1.11.78:80  check
  server k8s-master03	10.1.11.79:80  check

frontend ft_443
  bind 10.1.11.236:443
  mode http
  tcp-request inspect-delay 5s
  default_backend bk_443

backend bk_443
  mode http
  balance roundrobin
  server k8s-master01	10.1.11.77:443  check
  server k8s-master02	10.1.11.78:443  check
  server k8s-master03	10.1.11.79:443  check

frontend ft_53
  bind 10.1.11.236:53
  mode tcp
  tcp-request inspect-delay 5s
  default_backend bk_53

backend bk_53
  mode tcp
  balance roundrobin
  server k8s-master01	10.1.11.77:53  check
  server k8s-master02	10.1.11.78:53  check
  server k8s-master03	10.1.11.79:53  check

有疑问或者错误可以在评论区留言

  • 11
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值