k8s高可用配置
约 673 字大约 2 分钟
2025-11-16
提示
- haproxy是一个四层(TCP)/七层(HTTP)代理和负载均衡器,用来转发流量到后端节点。
- keepalived用于实现虚拟 IP(VIP)的高可用管理,通常依赖 VRRP 协议来实现主备切换。
keepalived 配置文件
提示
keepalived由两部分组成,服务配置文件和健康检查脚本,该脚本将被定期调用以验证持有虚拟 IP 的节点是否仍然可运行
vim /etc/keepalived/keepalived.conf! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script check_apiserver {
script "/etc/keepalived/check_apiserver.sh"
interval 3
weight -2
fall 10
rise 2
}
vrrp_instance VI_1 {
state ${STATE}
interface ${INTERFACE}
virtual_router_id ${ROUTER_ID}
priority ${PRIORITY}
authentication {
auth_type PASS
auth_pass ${AUTH_PASS}
}
virtual_ipaddress {
${APISERVER_VIP}
}
track_script {
check_apiserver
}
}有一些bash变量样式的占位符需要填写:
${STATE}一个MASTER和其他所有主机BACKUP,因此虚拟 IP 最初将分配给MASTER;${INTERFACE}是参与虚拟 IP 协商的网络接口,例如eth0;${ROUTER_ID}在同一子网中的所有集群之间应是唯一的,同时在同一个Keepalived集群的所有主机之间应保持一致。许多发行版将其值预先配置为51;${PRIORITY}控制平面节点上的值应高于备份节点上的值,因此101和100分别就足够了;${AUTH_PASS}``keepalived对于所有集群主机都应该相同,例如42;${APISERVER_VIP}是集群主机之间协商的虚拟 IP 地址keepalived。
健康检查脚本
#!/bin/sh
errorExit() {
echo "*** $*" 1>&2
exit 1
}
curl -sfk --max-time 2 https://localhost:${APISERVER_DEST_PORT}/healthz -o /dev/null || errorExit "Error GET https://localhost:${APISERVER_DEST_PORT}/healthz"${APISERVER_DEST_PORT}使用 Kubernetes 与 API 服务器通信的端口填写占位符,这是 haproxy 或您的负载均衡器将监听的端口。
haproxy 配置文件
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# https://www.haproxy.org/download/1.8/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
# utilize system-wide crypto-policies
ssl-default-bind-ciphers PROFILE=SYSTEM
ssl-default-server-ciphers PROFILE=SYSTEM
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
bind *:16443
mode tcp
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
use_backend static if url_static
default_backend app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
server static 127.0.0.1:6443 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
mode tcp
balance roundrobin
server k8s-master01 x.x.x.x:6443 check