我的知识库 / Kubernetes / kubeadm 安装 k8s (containerd)
使用 kubeadm 安装 k8s 集群,是社区推荐的安装方式,本文档将介绍使用 kubeadm 安装 k8s 集群的详细过程。
Notes:
禁用交换分区:
# 临时禁用交换分区
sudo swapoff -a
vim /etc/fstab
# 注释掉 swap 分区的配置
配置系统:
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
# sysctl params required by setup, params persist across reboots
cat <<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
# Apply sysctl params without reboot
sudo sysctl --system
参照 containerd 安装。
export LATEST=$(curl -s https://api.github.com/repos/containerd/containerd/releases/latest | jq -r .tag_name)
LATEST=${LATEST#v}
wget https://github.com/containerd/containerd/releases/download/v$LATEST/containerd-$LATEST-linux-amd64.tar.gz
sudo tar Cxzvf /usr/local containerd-$LATEST-linux-amd64.tar.gz
# systemd 配置
sudo wget https://raw.githubusercontent.com/containerd/containerd/main/containerd.service -O /lib/systemd/system/containerd.service
sudo systemctl daemon-reload
sudo systemctl enable --now containerd.service
sudo systemctl restart containerd.service
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
# 国内网络使用下面命令替换
# curl -fsSL https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
# 国内网络使用下面命令替换
# cat << EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
# deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
# EOF
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
早于 Debian 12 和 Ubuntu 22.04 的版本,/etc/apt/keyrings 目录默认不存在,需要手动创建:
sudo mkdir -m 755 /etc/apt/keyrings
国内网络提前拉取 registry.k8s.io/pause:3.9
镜像:
docker pull registry.aliyuncs.com/google_containers/pause:3.9
docker tag registry.aliyuncs.com/google_containers/pause:3.9 registry.k8s.io/pause:3.9
使用 kubeadm init
初始化集群:
sudo kubeadm init \
--pod-network-cidr 10.244.0.0/16 \
--kubernetes-version 1.29.0 \
--control-plane-endpoint=<EXTERNAL_IP>:6443 \
--ignore-preflight-errors=Swap
Notes:
--control-plane-endpoint
端点在执行环境是可以访问的,如果参数值为服务器的公网 IP,那么你可能需要对安全组开通 6443 端口;--image-repository registry.aliyuncs.com/google_containers
。初始化完成后,执行以下命令,配置集群访问环境:
mkdir -p ~/.kube
sudo cp -i /etc/kubernetes/admin.conf ~/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
查看集群节点状态:
kubectl get nodes
如果允许 Pod 调度到 Master 节点,那么需要去除 Master 节点的污点:
kubectl taint nodes --all node-role.kubernetes.io/control-plane-
安装 flannel
网络插件:
kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
flannel 默认使用 CIDR 为 10.244.0.0/16
,需要与 kubeadm init
时指定的 --pod-network-cidr
参数一致。
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
在多数情况下,metrics-server 由于证书问题无法正常启动,需要修改 metrics-server
的 Deployment
配置,添加 --kubelet-insecure-tls
参数:
kubectl edit deployment metrics-server -n kube-system
...
spec:
...
template:
...
spec:
containers:
- args:
- --cert-dir=/tmp
- --secure-port=4443
- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
- --kubelet-use-node-status-port
- --metric-resolution=15s
- --kubelet-insecure-tls
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --name=nginx --port=80 --target-port=80 --type=NodePort