写在最前
1. 前置条件
2. 部署流程
yum -y install haproxy keepalived
2.1 haproxy
cat >/etc/haproxy/haproxy.cfg<<"EOF"
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:6443
bind 127.0.0.1:6443
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-master1 172.31.0.12:6443 check
server k8s-master2 172.31.0.13:6443 check
server k8s-master3 172.31.0.14:6443 check
EOF
2.2 keepalived
如果每个 ha 都出现了虚拟 ip可以将多播调整为单播然后重启服务,unicast_src_ip 与 unicast_peer,最重要的还是要检查一下彼此的防火墙是否已经关闭了。
cat >/etc/keepalived/keepalived.conf<<"EOF"
! 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
interface ens33
mcast_src_ip 172.31.0.10
virtual_router_id 51
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass K8SHA_KA_AUTH
}
virtual_ipaddress {
172.31.0.100
}
track_script {
chk_apiserver
}
unicast_src_ip 172.31.0.10
unicast_peer {
172.31.0.11
}
}
EOF
2.2.1 ha1
cat >/etc/keepalived/keepalived.conf<<"EOF"
! 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
interface ens33
mcast_src_ip 172.31.0.10
virtual_router_id 51
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass K8SHA_KA_AUTH
}
virtual_ipaddress {
172.31.0.100
}
track_script {
chk_apiserver
}
unicast_src_ip 172.31.0.10
unicast_peer {
172.31.0.11
}
}
EOF
2.2.2 ha2
cat >/etc/keepalived/keepalived.conf<<"EOF"
! 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 BACKUP
interface ens33
mcast_src_ip 172.31.0.11
virtual_router_id 51
priority 99
advert_int 2
authentication {
auth_type PASS
auth_pass K8SHA_KA_AUTH
}
virtual_ipaddress {
172.31.0.100
}
track_script {
chk_apiserver
}
unicast_src_ip 172.31.0.11
unicast_peer {
172.31.0.10
}
}
EOF
2.2.3 健康检查脚本
cat > /etc/keepalived/check_apiserver.sh <<"EOF"
#!/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
EOF
# 授权执行权限
chmod +x /etc/keepalived/check_apiserver.sh
2.3 启动服务
systemctl daemon-reload
systemctl enable --now haproxy
systemctl enable --now keepalived
ip address show
2.4 配置解释
haproxy
1. global
maxconn 2000
: 设置最大连接数为 2000。ulimit-n 16384
: 设置文件描述符限制为 16384。log 127.0.0.1 local0 err
: 设置日志地址为本地 127.0.0.1,使用local0
日志源,记录错误日志。stats timeout 30s
: 设置统计信息的超时时间为 30 秒。
2. defaults
log global
: 使用全局日志设置。mode http
: 设置 HAProxy 的默认工作模式为 HTTP。option httplog
: 启用 HTTP 日志记录。timeout connect 5000
: 设置连接超时时间为 5000ms。timeout client 50000
: 设置客户端超时时间为 50000ms。timeout server 50000
: 设置服务器超时时间为 50000ms。timeout http-request 15s
: 设置 HTTP 请求超时时间为 15 秒。timeout http-keep-alive 15s
: 设置 HTTP keep-alive 超时时间为 15 秒。
3. frontend monitor-in
bind *:33305
: 监听所有 IP 地址上的 33305 端口。mode http
: 设置为 HTTP 模式。option httplog
: 启用 HTTP 日志。monitor-uri /monitor
: 设置监控路径为/monitor
,用于健康检查。
4. frontend k8s-master
bind 0.0.0.0:6443
: 监听所有 IP 地址上的 6443 端口(Kubernetes API 端口)。bind 127.0.0.1:6443
: 监听本地回环地址上的 6443 端口。mode tcp
: 设置为 TCP 模式(由于 Kubernetes API 是基于 TCP 的)。option tcplog
: 启用 TCP 日志。tcp-request inspect-delay 5s
: 设置 TCP 请求的检查延迟时间为 5 秒。default_backend k8s-master
: 指定默认的后端是k8s-master
。
5. backend k8s-master
mode tcp
: 后端使用 TCP 模式。option tcplog
: 启用 TCP 日志。option tcp-check
: 启用 TCP 健康检查。balance roundrobin
: 使用轮询算法进行负载均衡。default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 250 maxqueue 256 weight 100
: 后端服务器的健康检查设置:inter 10s
: 每 10 秒进行一次健康检查。downinter 5s
: 如果服务器失败,等待 5 秒后重试。rise 2
: 服务器需要连续 2 次健康检查成功才认为健康。fall 2
: 服务器需要连续 2 次失败才认为不健康。slowstart 60s
: 启动时延迟 60 秒才能处理请求。maxconn 250
: 限制每个服务器的最大连接数为 250。maxqueue 256
: 最大队列长度为 256。weight 100
: 设置服务器的权重为 100(权重越大,流量越多)。
server k8s-master1 172.31.0.12:6443 check
: 设置一个名为k8s-master1
的后端服务器,IP 地址为172.31.0.12
,端口为 6443,并启用健康检查。server k8s-master2 172.31.0.13:6443 check
: 设置一个名为k8s-master2
的后端服务器,IP 地址为172.31.0.13
,端口为 6443,并启用健康检查。server k8s-master3 172.31.0.14:6443 check
: 设置一个名为k8s-master3
的后端服务器,IP 地址为172.31.0.14
,端口为 6443,并启用健康检查。
总结:
这个配置文件用于设置 HAProxy 作为负载均衡器,将流量分发到三个 Kubernetes master 节点(
k8s-master1
、k8s-master2
和k8s-master3
)。它还设置了监控 URI (
/monitor
),并定义了与 Kubernetes API 端口(6443)相关的前端和后端配置。使用了 TCP 模式进行流量的负载均衡,并且为每个后端服务器配置了健康检查。
keepalived
1. global_defs
router_id LVS_DEVEL
: 设置路由器的 ID 为LVS_DEVEL
,这是一个全局的标识符。script_user root
: 设置执行脚本的用户为root
。enable_script_security
: 启用脚本安全性检查。
2. vrrp_script chk_apiserver
这是一个 VRRP(虚拟路由冗余协议)脚本配置,用于监控 Kubernetes API 服务器的状态。
script "/etc/keepalived/check_apiserver.sh"
: 指定一个外部脚本(/etc/keepalived/check_apiserver.sh
)来检查 API 服务器的健康状况。interval 5
: 设置脚本检查的时间间隔为 5 秒。weight -5
: 如果脚本检测到问题,减少该实例的权重,影响主备切换的决策。fall 2
: 如果脚本失败连续 2 次,视为失败。rise 1
: 如果脚本成功连续 1 次,视为恢复。
3. vrrp_instance VI_1
这是一个 VRRP 实例配置,定义了虚拟路由器的行为。
state MASTER
: 设置此实例的初始状态为MASTER
,即该节点是主节点。interface ens33
: 设置要绑定的网络接口为ens33
,即该接口用于处理 VRRP。mcast_src_ip 172.31.0.10
: 设置源 IP 地址为172.31.0.10
,用于多播(Multicast)。virtual_router_id 51
: 设置虚拟路由器的 ID 为 51,这是一个唯一标识符,确保集群中不同的 VRRP 实例不发生冲突。priority 100
: 设置该节点的优先级为 100,优先级较高的节点会成为主节点。advert_int 2
: 设置广告间隔为 2 秒,指示主节点向备节点广播自己的状态。authentication
: 配置身份验证方式,确保 VRRP 实例间的通信安全。auth_type PASS
: 使用简单密码认证。auth_pass K8SHA_KA_AUTH
: 密码为K8SHA_KA_AUTH
,用于验证通信。
virtual_ipaddress
: 配置虚拟 IP 地址。172.31.0.100
: 配置虚拟 IP 地址为172.31.0.100
,所有节点都共享该 IP 地址。
track_script
: 配置脚本监控虚拟 IP 的状态。chk_apiserver
: 通过chk_apiserver
脚本来检测 Kubernetes API 服务器是否健康。
unicast_src_ip 172.31.0.10
: 设置 VRRP 的源 IP 地址为172.31.0.10
,用于单播。unicast_peer { 172.31.0.11 }
: 配置与另一个节点(IP 地址为172.31.0.11
)之间的单播通信,以进行状态同步。
总结:
该配置文件使用 Keepalived 实现高可用性,通过 VRRP 协议在多个节点之间共享虚拟 IP(
172.31.0.100
)。chk_apiserver
脚本负责检查 Kubernetes API 服务器的状态,确保在 API 服务器不可用时进行主备切换。配置了主节点(
MASTER
)和备节点之间的优先级、身份验证等参数,确保高可用和故障转移。