CentOS Stream 9 安装 Kubernetes
步骤 1: 系统准备
禁用防火墙和 SELinux
bashsystemctl disable firewalld sed -i '/selinux/s/enforcing/disabled/' /etc/selinux/config
禁用交换分区 Kubernetes 要求交换分区禁用。编辑
/etc/fstab
文件注释掉与swap
相关的行:bashsudo swapoff -a sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
设置主机名 为各节点配置唯一的主机名:
bashsudo hostnamectl set-hostname <your-hostname>
加载必要的内核模块 Kubernetes 需要以下内核模块加载:
bashcat <<EOF | sudo tee /etc/modules-load.d/k8s.conf overlay br_netfilter EOF sudo modprobe overlay sudo modprobe br_netfilter
配置 sysctl 参数 配置网络选项使得 Kubernetes 网络正常工作:
bashcat <<EOF | sudo tee /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-iptables = 1 net.bridge.bridge-nf-call-ip6tables = 1 net.ipv4.ip_forward = 1 EOF sudo sysctl --system
配置国内 YUM 源
bashcurl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-9.repo
步骤 2: 安装容器运行时
Kubernetes 支持多种容器运行时,例如 containerd
和 CRI-O
。我们这里选用 containerd
。
安装 containerd
bashsudo dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo sudo dnf update sudo dnf install -y containerd
配置 containerd
bashsudo mkdir -p /etc/containerd containerd config default | sudo tee /etc/containerd/config.toml
找到
SystemdCgroup
并设置为true
(默认为false
)。编辑/etc/containerd/config.toml
:toml[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options] SystemdCgroup = true
启用并启动 containerd
bashsudo systemctl enable --now containerd
步骤 3: 安装 Kubernetes 组件
添加 Kubernetes YUM 源
bashcat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ enabled=1 gpgcheck=0 repo_gpgcheck=0 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF
安装 kubeadm、kubelet 和 kubectl
bashsudo dnf install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
启用 kubelet
bashsudo systemctl enable --now kubelet
步骤 4: 使用 kubeadm 初始化 Kubernetes 集群
初始化控制平面节点
bashsudo kubeadm init --pod-network-cidr=10.244.0.0/16
--pod-network-cidr=10.244.0.0/16
是为 Flannel CNI 插件指定的 Pod 网络 CIDR,你可以根据实际选择不同的 CNI 插件和参数。
为当前用户配置 kubeconfig 文件
bashmkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
部署网络插件 示例:安装 Flannel CNI 插件
bashkubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
步骤 5: 加入其他节点到集群
在控制平面节点完成初始化后,kubeadm init
命令会输出用于加入工作节点的命令,例如:
bash
kubeadm join <control-plane-host>:<port> --token <token> \
--discovery-token-ca-cert-hash sha256:<hash>
在工作节点上运行上述命令以加入集群。
步骤 6: 验证集群状态
检查节点状态:
bashkubectl get nodes
检查 Pod 和组件状态:
bashkubectl get pods -A