在deployment.yaml中设置image 为k8s.io/tomcat:8.5-jdk8,且这个镜像在本地存在:
[root@node1 tmp]# ctr -n k8s.io images list | grep tomcat
docker.io/kubeguide/tomcat-app:v1 application/vnd.docker.distribution.manifest.v2+json sha256:f2c465e01647be9a35c914205dc58912130e915426559cb60a7d12e9f4cc455e 352.5 MiB linux/amd64 io.cri-
k8s.io/tomcat:8.5-jdk8 application/vnd.docker.distribution.manifest.v2+json sha256:67f6f1f8e51fa8fdceb056c5b889b887ab8e00094e33fa981e35344511e937aa 151.6 MiB linux/amd64 io.cri-containerd.image=manag
但是在创建pod的过程中仍然从远处拉取,造成pod启动失败:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 30s default-scheduler Successfully assigned default/tomcat-749d98df56-hmlt9 to node1
Normal Pulled 2s (x5 over 29s) kubelet Container image "k8s.io/tomcat:8.5-jdk8" already present on machine
Warning Failed 2s (x5 over 29s) kubelet Error: failed to get image from containerd "sha256:6113f55245f6bb372e59de99ee3e9d848eef0bf64a07fcaf80e015f60ae8db08": image "docker.io/library/tomcat:8.5-jdk8": not found
解决方法:
发现这个问题和K8S的工作方式有关,Kubernetes 默认使用 docker.io/library/
作为镜像仓库的前缀。如果您 Deployment
或 Pod
的 image
中只写了 tomcat:8.5-jdk8
或 docker.io/tomcat:8.5-jdk8
,Kubernetes 实际上会自动补全为 docker.io/library/tomcat:8.5-jdk8。
所以将本地镜像的名称通过tag改为docker.io/library/tomcat:8.5-jdk8:
ctr -n k8s.io images tag docker.io/tomcat:8.5-jdk8 docker.io/library/tomcat:8.5-jdk8
并将deployment中的镜像名称改为以上名称,则pod创建成功。