目录
前言
在 Kubernetes 中使用 crictl 拉取镜像时,默认使用存在于公共镜像库(如 Docker Hub 或 Google Container Registry)的镜像。如果想使用私有镜像库,可通过配置 containerd 或 crictl 来实现。本文举例证明如何将镜像源配置为私有镜像库。
实验环境
运行下列环境设置:
- Kubernetes 版本: 1.24+
- Container Runtime: containerd
- 私有镜像库地址:
https://172.16.20.20
根据您说明,您将私有镜像库设为 172.16.20.20 ,并在 containerd 配置中实现自动切换。
正确配置 crictl 使用私有镜像库
步骤1:配置 containerd 镜像源
在 Kubernetes 中,默认是通过 containerd 进行镜像拉取。您需要修改 containerd 配置文件:
打开
/etc/containerd/config.toml:sudo nano /etc/containerd/config.toml找到
[plugins."io.containerd.grpc.v1.cri".registry.mirrors],添加私有镜像库配置:[plugins."io.containerd.grpc.v1.cri".registry.mirrors] [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"] endpoint = ["https://172.16.20.20"] [plugins."io.containerd.grpc.v1.cri".registry.mirrors."k8s.gcr.io"] endpoint = ["https://172.16.20.20"]重启 containerd :
sudo systemctl restart containerd
步骤2:添加 TLS 证书配置
如果私有镜像库使用的是非证书的 HTTP,需要配置允许不安全连接:
修改
/etc/containerd/config.toml,在相应镜像源配置中添加 TLS 选项:[plugins."io.containerd.grpc.v1.cri".registry.configs."172.16.20.20".tls] insecure_skip_verify = true重启 containerd :
sudo systemctl restart containerd
步骤3:验证 crictl 拉取镜像
在修改完成 containerd 配置后,通过 crictl 检查镜像源是否配置正确。
下载指定镜像:
crictl pull nginx:latest检查镜像是否已成功下载:
crictl images
如果拉取成功,返回中的镜像应该包含前缀 172.16.20.20 :
IMAGE TAG IMAGE ID SIZE
172.16.20.20/library/nginx latest <IMAGE_ID> <SIZE>
如果拉取失败,检查私有镜像库是否存在相应镜像,参考下文添加手动上传部分。
步骤4:如果镜像不存在
报错信息
root@master:/etc/containerd# crictl pull nginx:latest
E1231 17:27:29.937259 5597 remote_image.go:180] "PullImage from image service failed" err="rpc error: code = NotFound desc = failed to pull and unpack image \"docker.io/library/nginx:latest\": failed to resolve reference \"docker.io/library/nginx:latest\": docker.io/library/nginx:latest: not found" image="nginx:latest"
FATA[0000] pulling image: rpc error: code = NotFound desc = failed to pull and unpack image "docker.io/library/nginx:latest": failed to resolve reference "docker.io/library/nginx:latest": docker.io/library/nginx:latest: not found
如果私有镜像库中不存在 nginx:latest,需要将镜像上传到私有镜像库:
拉取镜像:
docker pull nginx:latest重命名为私有镜像库的路径:
docker tag nginx:latest 172.16.20.20/library/nginx:latest提交到私有镜像库:
docker push 172.16.20.20/library/nginx:latest重新通过
crictl pull拉取:crictl pull nginx:latest
总结
通过修改 containerd 配置,可以完成 Kubernetes 环境下 crictl 的私有镜像库配置。如果镜像不存在,可以通过手动提交或配置镜像代理来解决。将上进的步骤举行完成,即可使 Kubernetes 在私有环境下正常拉取镜像。