使用kubeoperator安装的k8s 版本1.20.14 将节点上的容器运行时从 Docker Engine 改为 containerd

2022-11-15,,,,

官方文档:https://kubernetes.io/zh-cn/docs/tasks/administer-cluster/migrating-from-dockershim/change-runtime-containerd/

背景描述:

1.当前k8s版本是 v1.20.14,使用kubeoperator进行安装的,但是使用kubeoperator不能对k8s进行大版本升级

2.安装的容器运行时是dockershim-> Docker Engine,调用路径如下:

3.鉴于k8s版本1.24以后推荐容器运行时使用containerd,因此需要一个测试环境,把现有k8s集群中使用的dockershim-> Docker Engine 换成 containerd

注意:

1.官方提供的还有:将 Docker Engine 节点从 dockershim 迁移到 cri-dockerd。地址:https://kubernetes.io/zh-cn/docs/tasks/administer-cluster/migrating-from-dockershim/migrate-dockershim-dockerd/

意味着,在k8s1.24版本以上也能继续使用Docker Engine,只不过需要把原先的dockershim替换成cri-dockerd。

1、检查状态并停止服务
systemctl daemon-reload
systemctl stop docker
systemctl disable docker
systemctl stop kubelet
systemctl stop containerd.io # 没安装 systemctl status kubelet.service || systemctl status docker.service 2、升级并配置containerd # 默认好像没安装,因此不用升级,直接进行安装
# yum update -y containerd.io-1.4.12 yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast yum -y install containerd.io #版本:1.6.6 直接生产默认配置文件:containerd config default > /etc/containerd/config.toml #在/etc/containerd/config.toml 中 注释 disabled_plugins 这一行 ==》 这一步不用做 #sed -i '/disabled_plugins/s/^/#/' /etc/containerd/config.toml 配置代理 设置本地、service和pod的段为no_proxy =》 这一步不用做 mkdir /etc/systemd/system/containerd.service.d cat > /etc/systemd/system/containerd.service.d/http_proxy.conf << EOF
[Service]
Environment="HTTP_PROXY=http://10.2.13.41:8888/"
Environment="HTTPS_PROXY=http://10.2.13.41:8888/"
Environment="NO_PROXY=xx.com,10.95.0.0/16,10.96.0.0/16,cluster.local,localhost"
EOF # 重启containerd 设置自启动
systemctl daemon-reload
systemctl restart containerd # systemctl start containerd
systemctl enable containerd.service # 查看状态
systemctl status containerd -l # 查看代理是否配置成功
systemctl show --property=Environment containerd 3、修改kubelet使用containerd的配置文件:
sed -i 's/"\(.\+\)"/"\1 --container-runtime=remote --container-runtime-endpoint=unix:\/\/\/run\/containerd\/containerd\.sock"/g' /var/lib/kubelet/kubeadm-flags.env 编辑文件 /var/lib/kubelet/kubeadm-flags.env,将 containerd 运行时添加到标志中:
--container-runtime=remote 和 --container-runtime-endpoint=unix:///run/containerd/containerd.sock"。 kubectl edit no develop-master-1 kubectl edit no develop-worker-1
kubectl edit no develop-worker-2 更改 kubeadm.alpha.kubernetes.io/cri-socket 值,将其从 /var/run/dockershim.sock 改为你所选择的 CRI 套接字路径 (例如:unix:///run/containerd/containerd.sock)。 # 重启服务并查看状态
systemctl daemon-reload
systemctl start kubelet
systemctl status kubelet.service -l # crictl配置
cat <<EOF | sudo tee /etc/crictl.yaml
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 0
debug: false
EOF 查看升级前组件版本:
ctr -v && crictl -v
查看镜像:
ctr namespaces list
ctr -n k8s.io container list # kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
develop-master-1 Ready control-plane,etcd,master,worker 187d v1.20.14 192.168.2.163 <none> CentOS Linux 7 (Core) 3.10.0-1160.49.1.el7.x86_64 docker://20.10.7
develop-worker-1 Ready worker 187d v1.20.14 192.168.2.164 <none> CentOS Linux 7 (Core) 3.10.0-1160.49.1.el7.x86_64 docker://20.10.7
develop-worker-2 Ready worker 187d v1.20.14 192.168.2.165 <none> CentOS Linux 7 (Core) 3.10.0-1160.49.1.el7.x86_64 containerd://1.6.6

问题:

1.使用yum方式安装containerd后,需要生产默认配置文件

containerd config default > /etc/containerd/config.toml
systemctl restart containerd

然后需要修改里面的默认的sandbox_image ,以及镜像仓库等。

尚未解决的问题:配置私有镜像仓库时需要配置拉取镜像使用的用户名和密码,这个还不知道怎么配置

2.之前的pod在启动时,有些公共镜像拉取不下来,这是因为containerd拉取镜像的时候必须是全路径的,也就是说必须带上仓库地址

后续情况:

问题1的私有仓库账号密码没法解决,已经回滚仍使用docker了。

使用kubeoperator安装的k8s 版本1.20.14 将节点上的容器运行时从 Docker Engine 改为 containerd的相关教程结束。

《使用kubeoperator安装的k8s 版本1.20.14 将节点上的容器运行时从 Docker Engine 改为 containerd.doc》

下载本文的Word格式文档,以方便收藏与打印。