温馨提示×

centos k8s部署需注意啥

小樊
40
2026-09-06 11:13:52
栏目: 智能运维

CentOS 上部署 Kubernetes(k8s),有几个关键点和“坑”需要特别注意。下面按系统准备 → 安装 → 网络/存储 → 运维安全来梳理。


一、系统版本与内核(非常重要)

1. CentOS 版本选择

  • 推荐 CentOS 7.9 / CentOS Stream 8/9
  • ❌ 不建议用 CentOS 8(已 EOL)
  • Rocky / AlmaLinux 也是很好的替代

2. 内核要求

  • k8s 推荐 内核 ≥ 4.x
  • 若用 ** overlay / eBPF / Cilium**,建议:
    uname -a
    
    • CentOS 7 默认 3.10,很多 CNI 不友好

二、系统初始化(必做)

1. 关闭防火墙(或放通端口)

systemctl stop firewalld
systemctl disable firewalld

生产环境建议用 iptables / firewalld 规则,而不是直接关


2. 关闭 SELinux

setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

3. 关闭 swap

swapoff -a
sed -i '/swap/d' /etc/fstab

kubelet 默认不允许 swap


4. 修改内核参数

cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF

sysctl --system

加载模块:

modprobe br_netfilter

三、容器运行时(CRI)

推荐选择

运行时 说明
containerd ✅ 官方推荐
docker ❌ 已不再直接支持(需 dockershim)

containerd 安装要点

yum install -y containerd
systemctl enable --now containerd

修改:

# /etc/containerd/config.toml
SystemdCgroup = true

四、Kubernetes 安装注意

1. 使用国内源(很重要)

cat > /etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
EOF

2. 安装版本一致

yum install -y kubelet kubeadm kubectl
systemctl enable kubelet

kubelet / kubeadm / kubectl 版本必须一致


五、初始化集群

kubeadm init \
  --pod-network-cidr=10.244.0.0/16 \
  --image-repository registry.aliyuncs.com/google_containers

⚠ 常见坑:

  • --pod-network-cidr 必须与 CNI 匹配
  • 国内必须指定 image 仓库

六、网络(CNI)选择

CNI 适合
Flannel 简单、稳定
Calico 安全、性能
Cilium eBPF、高级

示例(Flannel):

kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml

七、节点与资源

1. Master 节点

  • 至少 2C / 4G
  • 生产不建议跑 Pod(可去污点)
kubectl taint nodes --all node-role.kubernetes.io/master-

2. 时间同步

yum install -y chrony
systemctl enable --now chronyd

八、存储注意

  • CentOS 默认 xfs/ext4
  • 使用 Local PV / NFS / Ceph 时注意:
    • 权限
    • SELinux
    • 挂载参数

九、安全 & 运维

  • 禁用 root 登录
  • 配置 kubeconfig 权限
  • 开启 audit-log
  • 定期升级 k8s(小版本)

十、常见错误速查

问题 原因
node NotReady CNI 未装
kubelet 起不来 swap / SELinux
镜像拉不到 源未配置
DNS 失败 CoreDNS / 防火墙

如果你愿意,我可以:

  • ✅ 给你一套 CentOS 7 kubeadm 一键脚本
  • ✅ 对比 CentOS vs Ubuntu 部署差异
  • ✅ 针对 生产 / 测试环境给方案

你现在是 学习 / 测试 / 生产

0