3. 部署环境准备:操作系统配置与容器运行时安装
Kubernetes 集群的稳定性始于底层操作系统的正确配置。本章将详细讲解在 CentOS 7/8 或 Rocky Linux 8/9 环境下,如何完成内核参数调优、防火墙与 SELinux 策略调整、容器运行时(containerd)安装,以及高可用负载均衡器(HAProxy + Keepalived)的部署。这些步骤是集群部署前的“地基工程”,任何疏漏都可能导致后续节点注册失败、Pod 网络异常或证书轮换故障。
3.1 操作系统基础配置
3.1.1 内核参数调优
Kubernetes 对网络和文件系统有较高要求,必须调整以下关键内核参数。建议在所有节点(包括 Master 和 Worker)上统一执行。
net.bridge.bridge-nf-call-iptables 确保 iptables 规则对 bridge 流量生效;net.ipv4.ip_forward 开启 IP 转发,是 Pod 网络通信的基础;vm.swappiness 设置为 0 或 10,减少内存交换,提升性能。
# 加载 br_netfilter 模块
modprobe br_netfilter
echo 'br_netfilter' > /etc/modules-load.d/k8s.conf
# 设置内核参数
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
net.ipv4.tcp_tw_reuse = 1
vm.swappiness = 0
kernel.pid_max = 65536
fs.file-max = 2097152
fs.inotify.max_user_instances = 8192
fs.inotify.max_user_watches = 524288
EOF
# 立即生效
sysctl --system
| 参数名 | 推荐值 | 作用说明 |
|---|---|---|
| net.bridge.bridge-nf-call-iptables | 1 | 确保 iptables 规则对容器网桥生效 |
| net.ipv4.ip_forward | 1 | 开启 IP 转发,Pod 间通信必需 |
| vm.swappiness | 0 或 10 | 减少内存交换,提升性能 |
| fs.inotify.max_user_watches | 524288 | 防止 inotify 资源耗尽 |
3.1.2 防火墙与 SELinux 策略
在生产环境中,建议关闭防火墙或仅开放必要端口。SELinux 建议设置为 permissive 模式,避免容器挂载卷时出现权限问题。
# 关闭防火墙(生产环境请按需开放端口)
systemctl stop firewalld
systemctl disable firewalld
# 设置 SELinux 为 permissive 模式
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
# 验证状态
getenforce # 应输出 Permissive
container_runtime_t 上下文,并确保 selinux-policy 版本支持容器运行时。不推荐在 Kubernetes 节点上使用 enforcing 模式,除非经过充分测试。
3.2 容器运行时安装:containerd
Kubernetes 从 v1.24 起移除了对 Docker 的直接支持,推荐使用 containerd 作为容器运行时。以下步骤适用于 x86_64 架构的 Linux 系统。
3.2.1 安装 containerd
# 安装依赖
yum install -y yum-utils device-mapper-persistent-data lvm2
# 添加 Docker 官方仓库(containerd 也在此仓库中)
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 安装 containerd
yum install -y containerd.io
# 生成默认配置文件
mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml
# 启动并设置开机自启
systemctl start containerd
systemctl enable containerd
3.2.2 配置 containerd 使用 systemd cgroup 驱动
Kubernetes 推荐使用 systemd cgroup 驱动,以确保节点稳定性。修改 /etc/containerd/config.toml 文件:
# 编辑配置文件
vim /etc/containerd/config.toml
# 找到 [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
# 修改 SystemdCgroup 为 true
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true
# 同时设置 sandbox_image 为国内镜像(可选)
[plugins."io.containerd.grpc.v1.cri"]
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"
# 重启 containerd
systemctl restart containerd
ctr version 或 crictl version 检查 containerd 是否正常运行。如果 crictl 未安装,可通过 yum install -y cri-tools 安装。
3.3 负载均衡器部署:HAProxy + Keepalived
在生产环境中,Kubernetes API Server 必须通过负载均衡器对外暴露,以实现高可用。本节使用 HAProxy 作为七层负载均衡器,Keepalived 提供 VIP(虚拟 IP)漂移能力。
3.3.1 架构说明
部署两台负载均衡节点(LB-1 和 LB-2),共享一个 VIP。当主节点故障时,VIP 自动漂移到备用节点,保证 API Server 访问不中断。
3.3.2 安装与配置 Keepalived
在两台 LB 节点上安装 Keepalived,并配置 VRRP 实例。
# 安装 Keepalived
yum install -y keepalived
# 主节点 (LB-1) 配置 /etc/keepalived/keepalived.conf
cat <<EOF > /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_DEVEL
}
vrrp_script check_haproxy {
script "/usr/bin/killall -0 haproxy"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
192.168.1.100/24
}
track_script {
check_haproxy
}
}
EOF
# 备节点 (LB-2) 配置,仅修改 state 为 BACKUP,priority 为 90
# 启动 Keepalived
systemctl start keepalived
systemctl enable keepalived
3.3.3 安装与配置 HAProxy
在两台 LB 节点上安装 HAProxy,配置后端指向所有 Master 节点的 API Server(6443 端口)。
# 安装 HAProxy
yum install -y haproxy
# 配置 /etc/haproxy/haproxy.cfg
cat <<EOF > /etc/haproxy/haproxy.cfg
global
log /dev/log local0
maxconn 4096
user haproxy
group haproxy
defaults
log global
mode tcp
option tcplog
option dontlognull
retries 3
timeout connect 5s
timeout client 50s
timeout server 50s
frontend k8s-api
bind *:6443
mode tcp
default_backend k8s-masters
backend k8s-masters
mode tcp
balance roundrobin
server master1 192.168.1.11:6443 check inter 3s fall 3 rise 2
server master2 192.168.1.12:6443 check inter 3s fall 3 rise 2
server master3 192.168.1.13:6443 check inter 3s fall 3 rise 2
EOF
# 启动 HAProxy
systemctl start haproxy
systemctl enable haproxy
curl -k https://192.168.1.100:6443/version 测试 VIP 是否正常响应。如果返回 Kubernetes API 版本信息,说明配置成功。
3.4 配置验证清单
完成上述步骤后,请逐项检查以下配置,确保集群部署环境就绪。
| 检查项 | 验证命令 | 预期结果 |
|---|---|---|
| 内核参数 | sysctl net.ipv4.ip_forward |
返回 1 |
| SELinux 状态 | getenforce |
Permissive |
| containerd 运行状态 | systemctl status containerd |
active (running) |
| cgroup 驱动 | crictl info | grep cgroupDriver |
systemd |
| VIP 可达性 | ping 192.168.1.100 |
正常响应 |
| API Server 负载均衡 | curl -k https://192.168.1.100:6443/version |
返回 JSON 版本信息 |