一、环境参数
- linux系统为centos7
- kubernetes版本为v1.28.2
- containerd版本为1.6.28
二、报错内容
执行初始化命令kubeadm init命令时报错,内容如下
error execution phase preflight: [preflight] Some fatal errors occurred:[ERROR CRI]: container runtime is not running: output: time="2024-02-28T16:26:42+08:00" level=fatal msg="validate service connection: CRI v1 runtime API is not implemented for endpoint \"unix:///var/run/containerd/containerd.sock\": rpc error: code = Unimplemented desc = unknown service runtime.v1.RuntimeService"
, error: exit status 1

三、解决方法
1. 查看containerd状态
systemctl status containerd
如果containerd正常运行(Active状态为active(running))
2. config.toml文件查看
查看containerd的config.toml文件
 
 如果有内容如下
 则进行修改
# 注释或将"cri"删掉
disabled_plugins = ["cri"]
3.重启containerd
修改的内容重启后生效
systemctl restart containerd
四、原因分析及参考
-  一个关于版本的背景故事 
 根据k8s官网的介绍,kubernets 自v 1.24.0 后,移除了 docker.shim(k8s集成的docker),替换采用 containerd 作为容器运行时。因此需要安装 containerd
 而containerd是docker的子项目,现在他俩分开了,所以可以单独安装containerd
-  关于containerd的介绍 
 常用的容器运行时有docker、containerd、CRI-O等
 containerd是一个CRI(Container Runtime Interface)组件,在容器运行时调用containerd组件来创建、运行、销毁容器等
 CRI组件遵循OCI规范,通过runc实现与操作系统内核的交互,然后实现创建和运行容器
 docker使用containerd作为运行时,k8s使用containerd、CRI-O等
-  报错内容中的内容分析 
 CRI Container Runtime Interface 容器运行时接口
 container runtime is not running 容器运行时未启动
 validate service connection 无效的服务连接
 CRI v1 runtime API is not implemented for endpoint “unix:///var/run/containerd/containerd.sock” 容器运行时接口 v1 运行时 接口 没有实现节点文件sock,应该就是此文件未找到
-  containerd安装的默认禁用(重点) 
 使用安装包安装的containerd会默认禁用作为容器运行时的功能,即安装包安装containerd后默认禁用containerd作为容器运行时
 这个时候使用k8s就会报错了,因为没有容器运行时可以用
 开启方法就是将/etc/containerd/config.toml文件中的disabled_plugins的值的列表中不包含cri
 修改后重启containerd才会生效
五、kubernetes k8s拉取镜像失败
异常:由于国内网络原因,kubeadm init会卡住不动,一卡就是半个小时,然后报出这种问题
I0228 16:54:27.451719   12390 version.go:256] remote version is much newer: v1.29.2; falling back to: stable-1.28
failed to pull image "registry.k8s.io/kube-apiserver:v1.28.7": output: E0228 16:57:04.871630   12789 remote_image.go:171] "PullImage from image service failed" err="rpc error: code = Unknown desc = failed to pull and unpack image \"registry.k8s.io/kube-apiserver:v1.28.7\": failed to resolve reference \"registry.k8s.io/kube-apiserver:v1.28.7\": failed to do request: Head \"https://us-west2-docker.pkg.dev/v2/k8s-artifacts-prod/images/kube-apiserver/manifests/v1.28.7\": dial tcp 64.233.188.82:443: i/o timeout" image="registry.k8s.io/kube-apiserver:v1.28.7"
time="2024-02-28T16:57:04+08:00" level=fatal msg="pulling image: rpc error: code = Unknown desc = failed to pull and unpack image \"registry.k8s.io/kube-apiserver:v1.28.7\": failed to resolve reference \"registry.k8s.io/kube-apiserver:v1.28.7\": failed to do request: Head \"https://us-west2-docker.pkg.dev/v2/k8s-artifacts-prod/images/kube-apiserver/manifests/v1.28.7\": dial tcp 64.233.188.82:443: i/o timeout"
, error: exit status 1
To see the stack trace of this error execute with --v=5 or higher
原因显而易见,是因为要下载registry.k8s.io的docker镜像,但是国内连不上
解决方法
使用阿里云镜像
 运行kubeadm init时加上阿里云镜像的参数--image-repository=registry.aliyuncs.com/google_containers
kubeadm init --image-repository=registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.18.5
等待一段时间即可成功!