我们如何在 kubernetes 中查看缓存的图像?

How can we see cached images in kubernetes?

我正在使用 kops 作为 kubernetes 部署。

我注意到,如果 imagepullpolicy 未设置为 always

,则无论何时在部署文件中输入具有相同标签编号的图像,系统都会采用之前的图像

有什么方法可以在 kubernetes 环境中查看容器的所有缓存图像?

假设我在部署中有一个图像 test:56 当前 运行 并且之前使用过 test:1test:55,那么 kubernetes 是否缓存这些图像?如果是,在哪里可以找到这些?

  • 对您的环境的评论:

    I noticed that whenever an image with same tag number is entered in deployment file the system takes the previous image if imagepullpolicy is not set to always

A pre-pulled image 可用于预加载某些图像以提高速度或作为对私有注册表进行身份验证的替代方法,从而优化性能。

docker 将始终缓存本地使用的所有图像。

由于您使用的是 EKS,请记住,如果您有节点健康管理(意味着节点出现故障时将被替换),新节点将不会从旧节点缓存图像,因此它始终是将图像存储在 your Cloud Provider Registry 或本地注册表之类的注册表中是个好主意。

  • 让我们来回答您的第一个问题:

    Is there any way in which I can see all the cached images of a container in kubernetes environment ?

是的,您必须使用 docker images 列出您环境中存储的图像。

  • 第二个问题:

    Like suppose I have an image test:56 currently running in a deployment and test:1 to test:55 were used previously, so does Kubernetes cache those images ? and if yes where can those be found ?

我为你准备了一个例子:

  • 我根据busybox官方镜像部署了几个pods:
$ kubectl run busy284 --generator=run-pod/v1 --image=busybox:1.28.4
pod/busy284 created
$ kubectl run busy293 --generator=run-pod/v1 --image=busybox:1.29.3
pod/busy284 created
$ kubectl run busy284 --generator=run-pod/v1 --image=busybox:1.28
pod/busy28 created
$ kubectl run busy284 --generator=run-pod/v1 --image=busybox:1.29
pod/busy29 created
$ kubectl run busy284 --generator=run-pod/v1 --image=busybox:1.30
pod/busy284 created
$ kubectl run busybox --generator=run-pod/v1 --image=busybox
pod/busybox created

现在让我们检查存储在docker images

中的图像
$ docker images
REPOSITORY                                TAG                   IMAGE ID            CREATED             SIZE
k8s.gcr.io/kube-proxy                     v1.17.3               ae853e93800d        5 weeks ago         116MB
k8s.gcr.io/kube-controller-manager        v1.17.3               b0f1517c1f4b        5 weeks ago         161MB
k8s.gcr.io/kube-apiserver                 v1.17.3               90d27391b780        5 weeks ago         171MB
k8s.gcr.io/kube-scheduler                 v1.17.3               d109c0821a2b        5 weeks ago         94.4MB
kubernetesui/dashboard                    v2.0.0-beta8          eb51a3597525        3 months ago        90.8MB
k8s.gcr.io/coredns                        1.6.5                 70f311871ae1        4 months ago        41.6MB
k8s.gcr.io/etcd                           3.4.3-0               303ce5db0e90        4 months ago        288MB
kubernetesui/metrics-scraper              v1.0.2                3b08661dc379        4 months ago        40.1MB
busybox                                   latest                83aa35aa1c79        10 days ago         1.22MB
busybox                                   1.30                  64f5d945efcc        10 months ago       1.2MB
busybox                                   1.29                  758ec7f3a1ee        15 months ago       1.15MB
busybox                                   1.29.3                758ec7f3a1ee        15 months ago       1.15MB
busybox                                   1.28                  8c811b4aec35        22 months ago       1.15MB
busybox                                   1.28.4                8c811b4aec35        22 months ago       1.15MB

您可以看到所有推送的图片列表。

最好使用命令 docker system prune 清除系统中的旧资源,以便不时释放服务器上的 space。

如有任何疑问,请在评论中告诉我。