概述

在 Kubernetes(K8s)里,容器镜像拉取策略(ImagePullPolicy)决定了 K8s 在创建或重启 Pod 时,如何处理容器镜像的拉取操作。这一策略能够确保使用的镜像始终是最新的,或者使用本地已有的镜像以提升部署效率。

可以使用kubectl explain pod.spec.containers.imagePullPolicy查看资源的详细文档:

示例:

[root@master01 ~]# kubectl explain pod.spec.containers.imagePullPolicy
KIND: Pod
VERSION: v1 FIELD: imagePullPolicy <string> DESCRIPTION:
Image pull policy. One of Always, Never, IfNotPresent. Defaults to Always
if :latest tag is specified, or IfNotPresent otherwise. Cannot be updated.
More info:
https://kubernetes.io/docs/concepts/containers/images#updating-images Possible enum values:
- `"Always"` means that kubelet always attempts to pull the latest image.
Container will fail If the pull fails.
- `"IfNotPresent"` means that kubelet pulls if the image isn't present on
disk. Container will fail if the image isn't present and the pull fails.
- `"Never"` means that kubelet never pulls an image, but only uses a local
image. Container will fail if the image isn't present

通过文档可以发现,镜像拉取策略有三个类型,分别是Always、IfNotPresent、Never

也可以通过官方文档来查看:https://kubernetes.io/docs/concepts/containers/images#updating-images

镜像拉取策略分类详解

Never

Never表示永不拉取镜像,kubelet 不会尝试获取镜像。如果镜像已经以某种方式存在本地, kubelet 会尝试启动容器;否则,会启动失败。

示例:

[root@master01 ~/pod]# cat pod-tomcat.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-tomcat
spec:
containers:
- name: container-tomcat
image: tomcat:8.0
# 指定镜像拉取策略为Never
imagePullPolicy: Never
ports:
- name: http
containerPort: 8080
# 创建pod
[root@master01 ~/pod]# kubectl apply -f pod-tomcat.yaml
pod/pod-tomcat created
# 查看pod
[root@master01 ~/pod]# kubectl get po pod-tomcat
NAME READY STATUS RESTARTS AGE
pod-tomcat 0/1 ErrImageNeverPull 0 111s # 查看详情,发现并没有拉取镜像
[root@master01 ~/pod]# kubectl describe po pod-tomcat
Name: pod-tomcat
...省略万字内容
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m7s default-scheduler Successfully assigned default/pod-tomcat to node02
Normal SandboxChanged 2m5s kubelet Pod sandbox changed, it will be killed and re-created.
Warning ErrImageNeverPull 25s (x12 over 2m6s) kubelet Container image "tomcat:8.0" is not present with pull policy of Never
Warning Failed 25s (x12 over 2m6s) kubelet Error: ErrImageNeverPull

IfNotPresent

如果本地有镜像就使用本地的,如果没有就去远程仓库拉取

示例:

[root@master01 ~/pod]# cat pod-tomcat.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-tomcat
spec:
containers:
- name: container-tomcat
image: tomcat:8.0
# 指定镜像拉取策略为IfNotPresent
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 8080 # 创建pod
[root@master01 ~/pod]# kubectl apply -f pod-tomcat.yaml
pod/pod-tomcat created # 查看pod
[root@master01 ~/pod]# kubectl get pod pod-tomcat
NAME READY STATUS RESTARTS AGE
pod-tomcat 1/1 Running 0 43s #查看pod详情
[root@master01 ~/pod]# kubectl describe pod pod-tomcat
Name: pod-tomcat
Namespace: default
##...省略万字内容
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 64s default-scheduler Successfully assigned default/pod-tomcat to node02
Normal Pulling 63s kubelet Pulling image "tomcat:8.0"
Normal Pulled 23s kubelet Successfully pulled image "tomcat:8.0" in 40.129798231s (40.129811976s including waiting)
Normal Created 23s kubelet Created container container-tomcat
Normal Started 23s kubelet Started container container-tomcat

Always

如果本地有镜像,则对比本地镜像和远程仓库的摘要信息,若相同则使用本地缓存,若不同则重新拉取镜像。

如果本地没有镜像,则无需对比摘要信息,直接拉取镜像。

示例:

[root@master01 ~/pod]# cat pod-tomcat.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-tomcat
spec:
containers:
- name: container-tomcat
# 镜像更改为latest
image: tomcat:latest
# 指定镜像拉取策略为Always
imagePullPolicy: Always
ports:
- name: http
containerPort: 8080 # 创建pod
[root@master01 ~/pod]# kubectl apply -f pod-tomcat.yaml
pod/pod-tomcat created #查看pod
[root@master01 ~/pod]# kubectl get pod pod-tomcat
NAME READY STATUS RESTARTS AGE
pod-tomcat 1/1 Running 0 4m53s # 查看pod详情
[root@master01 ~/pod]# kubectl describe pod pod-tomcat
Name: pod-tomcat
Namespace: default
## ...省略万字内容
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 5m39s default-scheduler Successfully assigned default/pod-tomcat to node02
Normal Pulling 5m38s kubelet Pulling image "tomcat:latest"
Normal Pulled 4m55s kubelet Successfully pulled image "tomcat:latest" in 43.43020764s (43.430224485s including waiting)
Normal Created 4m55s kubelet Created container container-tomcat
Normal Started 4m55s kubelet Started container container-tomcat

镜像默认的拉取策略

官网的描述是这样的:

  • 如果你省略了 imagePullPolicy 字段,并且你为容器镜像指定了摘要, 那么 imagePullPolicy 会自动设置为 IfNotPresent
  • 如果你省略了 imagePullPolicy 字段,并且容器镜像的标签是 :latestimagePullPolicy 会自动设置为 Always
  • 如果你省略了 imagePullPolicy 字段,并且没有指定容器镜像的标签, imagePullPolicy 会自动设置为 Always
  • 如果你省略了 imagePullPolicy 字段,并且为容器镜像指定了非 :latest 的标签, imagePullPolicy 就会自动设置为 IfNotPresent

简单点来说就是如果没有指定镜像的版本,或者指定镜像的版本为 :latest,那么镜像拉取策略就是 Always。否则就是 IfNotPresent

K8s新手系列之Pod中容器的镜像拉取策略的更多相关文章

  1. kubernetes之pod生命周期,pod重启策略, 镜像拉取策略

    pod声明周期(状态):pending , running, succeeded, failed, unknown 挂起(Pending):Pod 已被 Kubernetes 系统接受,但有一个或者多 ...

  2. Kubernetes Pod 镜像拉取策略

    Kubernetes Pod 镜像拉取策略 官方文档:https://kubernetes.io/docs/concepts/containers/images/ • IfNotPresent:默认值 ...

  3. Pod镜像拉取策略imagePullPolicy

    默认值是IfNotPresent Always 总是拉取: 首先获取仓库镜像信息, 如果仓库中的镜像与本地不同,那么仓库中的镜像会被拉取并覆盖本地. 如果仓库中的镜像与本地一致,那么不会拉取镜像. 如 ...

  4. K8s 从懵圈到熟练 – 镜像拉取这件小事

    作者 | 声东 阿里云售后技术专家 导读:相比 K8s 集群的其他功能,私有镜像的自动拉取,看起来可能是比较简单的.而镜像拉取失败,大多数情况下都和权限有关.所以,在处理相关问题的时候,我们往往会轻松 ...

  5. docker中容器和镜像的区别

    自学docker过程中一直搞不明白 镜像容器的关系,网上查阅看到一篇文章觉得讲的很好,转载记录. 转自 http://blog.csdn.net/chszs/article/details/48252 ...

  6. RocketMQ中PullConsumer的消息拉取源码分析

    在PullConsumer中,有关消息的拉取RocketMQ提供了很多API,但总的来说分为两种,同步消息拉取和异步消息拉取 同步消息拉取以同步方式拉取消息都是通过DefaultMQPullConsu ...

  7. Kubernetes Pod中容器的Liveness、Readiness和Startup探针

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 探针的作用 在Kubernetes的容器生命周期管理中,有三种探针,首先要知道,这探针是属于容器的,而不是Pod: 存 ...

  8. Kubernetes笔记(五):了解Pod(容器组)

    Kubernetes 中, 容器总是以 Pod(容器组)的方式进行调度与运行.因此对 Pod 的理解与掌握是学习 Kubernetes 的基础. 理解 Pod Pod(容器组)是 Kubernetes ...

  9. k8s之深入解剖Pod(一)

    上文说了一下k8s的简单使用,接下来就让我们来具体深入了解一下Pod.为了避免篇幅太长,所以会分成几篇. 目录: Pod定义详解 静态Pod Pod容器共享Volume 一.Pod定义详解 先看一个简 ...

  10. k8s核心资源之Pod概念&入门使用讲解(三)

    目录 1. k8s核心资源之Pod 1.1 什么是Pod? 1.2 Pod如何管理多个容器? 1.3 Pod网络 1.4 Pod存储 1.5 Pod工作方式 1.5.1 自主式Pod 1.5.2 控制 ...

随机推荐

  1. Kali 关闭自动锁屏功能

    Kali 关闭自动锁屏功能 1.点击 [开始] -> [设置] -> [电源管理器] 2.选择 [安全性],将 [自动锁定会话] 选为 [从不],将 [当系统休眠时锁定屏幕] 取消勾选,点 ...

  2. Linux重要的日志文件

    1./var/log/boot.log 该文件记录了系统在引导过程中发生的事件,就是Linux系统开机自检过程显示的信息 2./var/log/syslog 只记录警告信息,常常是系统出问题的信息,所 ...

  3. Mac使用docker安装Doris

    一.编译源码 (1)拉取编译镜像docker pull apache/incubator-doris:build-env-1.2 (2)Mac电脑上拉取源码git clone https://gith ...

  4. 《刚刚问世》系列初窥篇-Java+Playwright自动化测试-12- iframe操作-上篇(详细教程)

    1.简介 原估计宏哥这里就不对iframe这个知识点做介绍和讲解了,因为前边的窗口切换就为这种网页处理提供了思路,另一个原因就是虽然iframe很强大,但是现在很少有网站用它了.但是还是有小伙伴或者童 ...

  5. 用python做时间序列预测三:时间序列分解

    在初始概念篇中,我们简单提到了时间序列由趋势.周期性.季节性.误差构成,本文将介绍如何将时间序列的这些成分分解出来.分解的使用场景有很多,比如当我们需要计算该时间序列是否具有季节性,或者我们要去除该时 ...

  6. Win10、Win11老游戏运行补丁(cnc-ddraw),适用于红色警戒、帝国时代等经典游戏

    Win11.Win10老游戏运行补丁(cnc-ddraw),适用广泛,红色警戒(红警),直接复制到游戏目录,然后即可畅玩.再也不需要修改:管理员运行,兼容性运行,更改DPI.cnc-ddraw 可以修 ...

  7. oracle - [11] 那些年使用的emp表和dept表

    那些年在学习编程时,Oracle自带的emp表和dept表,本文进行整理和记录,以便于在今后的学习和工作中作为示例数据. 雇员表(emp) CREATE TABLE EMP( EMPNO NUMBER ...

  8. idea社区版配置springboot项目问题分析及处理

    前言 记录一次使用IDEA社区版配置SpringBoot项目的经历,包括遇到的问题及解决过程 IDEA版本:IntelliJ IDEA 2024.2.3 (Community Edition) 问题描 ...

  9. postman 如何比较两台电脑的脚本是否一样

  10. 三分钟构建高性能WebSocket服务 | 超优雅的Springboot整合Netty方案

    前言 每当使用SpringBoot进行Weboscket开发时,最容易想到的就是spring-boot-starter-websocket(或spring-websocket).它可以让我们使用注解, ...