k8s新版本使用container而不是docker导致创建pod一直提示证书问题

使用 Harbor 仓库作为 Kubernetes 集群私有仓库

Harbor 仓库信息

  • 内网地址:hub.rainsc.com
  • IP 地址:192.168.66.100

问题背景

在许多版本的教程中,会建议在 Docker 的配置中添加忽略证书的列表。然而,截至 2024 年 8 月 24 日,这些教程支持的 Kubernetes 版本已经无法直接安装。一方面是因为这些教程大多基于 CentOS 7,使用的是 4.4 内核,而 CentOS 相关的所有镜像源都已经删除了内核相关文件。

当前我的内核版本是:

[root@master01 ~]# uname -a
Linux master01 5.14.0-496.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Mon Aug 12 20:37:54 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

我直接按照当前内核支持的版本安装了最新的 kubectl 工具:

[root@master01 ~]# kubectl version
Client Version: v1.29.8
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.29.8


[root@master01 ~]# kubectl get nodes -o jsonpath='{.items[*].status.nodeInfo.containerRuntimeVersion}'
containerd://1.7.20 containerd://1.7.20 containerd://1.7.20

容器运行时的变化

Kubernetes 并不直接管理容器运行时,而是通过 kubelet 与容器运行时进行交互。在不同的 Kubernetes 版本中,支持的容器运行时有所变化:

  1. Kubernetes 1.20 及之前版本

    • 默认容器运行时: Docker
    • Docker Shim: Kubernetes 通过 Docker Shim(dockershim)与 Docker 交互。
  2. Kubernetes 1.21 至 1.23

    • 容器运行时: Kubernetes 1.21 开始,逐步弃用 Docker Shim,鼓励使用其他 CRI 兼容的容器运行时。
    • 推荐替代容器运行时: containerdCRI-O
  3. Kubernetes 1.24 及之后版本

    • 弃用 Docker Shim: Docker Shim 被正式弃用,不再支持通过 Docker Shim 与 Docker 交互。
    • 推荐容器运行时: containerdCRI-O

Harbor 部署及问题解决

在环境中,我在 192.168.66.100 上部署了 Docker 版本的 Harbor。尝试使用以下命令创建 Pod 时遇到问题:

kubectl run nginx-deployment --image=hub.rainsc.com/library/mynginx:v1 --port=80

该操作提示证书错误,尝试将 Harbor 的证书添加到受信任列表,并修改 Docker 配置文件,但无效。
类似于下面这段 可能不完全一样 因为已经救活了 就不去作死了
下面这个错误展示的是配置文件写错了 但是ai会直接告诉你证书问题 相关tls之类的提示全都说证书问题 千万别碰证书 没有意义

[root@master01 ~]# crictl --runtime-endpoint /run/containerd/containerd.sock pull hub.rainsc.com/library/helloworld:v1
I0824 19:51:33.643312   65083 util_unix.go:103] "Using this endpoint is deprecated, please consider using full URL format" endpoint="/run/containerd/containerd.sock" URL="unix:///run/containerd/containerd.sock"
E0824 19:51:33.691725   65083 remote_image.go:171] "PullImage from image service failed" err="rpc error: code = Unknown desc = failed to pull and unpack image \"hub.rainsc.com/library/helloworld:v1\": failed to resolve reference \"hub.rainsc.com/library/helloworld:v1\": failed to do request: Head \"https://hub.rainsc.com/v2/library/helloworld/manifests/v1\": tls: failed to verify certificate: x509: certificate signed by unknown authority" image="hub.rainsc.com/library/helloworld:v1"
FATA[0000] pulling image: rpc error: code = Unknown desc = failed to pull and unpack image "hub.rainsc.com/library/helloworld:v1": failed to resolve reference "hub.rainsc.com/library/helloworld:v1": failed to do request: Head "https://hub.rainsc.com/v2/library/helloworld/manifests/v1": tls: failed to verify certificate: x509: certificate signed by unknown authority

解决方案

由于 Kubernetes 1.24 之后不再使用 Docker,而是使用 containerd,我们需要正确配置 containerdconfig.toml 文件。

  1. 编辑 /etc/containerd/config.toml 文件,找到 [plugins."io.containerd.grpc.v1.cri".registry.auths] 配置节,添加以下内容:
[plugins."io.containerd.grpc.v1.cri".registry.auths]

[plugins."io.containerd.grpc.v1.cri".registry.configs]
  [plugins."io.containerd.grpc.v1.cri".registry.configs."hub.rainsc.com".tls]
    insecure_skip_verify = true
  [plugins."io.containerd.grpc.v1.cri".registry.configs."hub.rainsc.com".auth]
    username = "admin"
    password = "Harbor12345"
[plugins."io.containerd.grpc.v1.cri".registry.headers]

[plugins."io.containerd.grpc.v1.cri".registry.mirrors]
  [plugins."io.containerd.grpc.v1.cri".registry.mirrors."hub.rainsc.com"]
    endpoint = ["http://hub.rainsc.com"]
  1. 编辑 /var/lib/kubelet/config.yaml 文件,指定 containerRuntimeEndpoint
containerRuntimeEndpoint: "unix:///run/containerd/containerd.sock"
  1. 绑定containerd容器给crictl
    这个文件默认是不存在的 需要手动创建后填
echo "runtime-endpoint: unix:///run/containerd/containerd.sock" | sudo tee /etc/crictl.yaml > /dev/null
[root@node02 ~]# cat /etc/crictl.yaml
cat: /etc/crictl.yaml: 没有那个文件或目录
[root@node02 ~]# echo "runtime-endpoint: unix:///run/containerd/containerd.sock" | sudo tee /etc/crictl.yaml > /dev/null
[root@node02 ~]# cat /etc/crictl.yaml
runtime-endpoint: unix:///run/containerd/containerd.sock

  1. 配置完成后,使用以下命令拉取镜像:
[root@master01 ~]# crictl pull hub.rainsc.com/library/helloworld:v1
Image is up to date for sha256:6974669be52b12a9103072cbad3e13fbf119b76aa09747f19a821a5eaad34be1
  1. 创建 Pod:
[root@master01 ~]# kubectl run helloworld --image=hub.rainsc.com/library/helloworld:v1 --port=8123
pod/helloworld created
[root@master01 ~]# kubectl get pods
NAME         READY   STATUS    RESTARTS   AGE
helloworld   1/1     Running   0          9s

总结

通过正确配置 containerd 和 Kubernetes 配置文件,解决了由于弃用 Docker Shim 引发的镜像拉取问题。现在可以正常使用 Harbor 仓库来创建和管理 Pod。

posted @ 2024-08-24 20:54  rainsc  阅读(127)  评论(0编辑  收藏  举报