【云原生】kubernetes在Pod中init容器的作用和使用

目录

[Pod 中 init 容器](#Pod 中 init 容器)

[1 init 容器特点](#1 init 容器特点)

[2 使用 init 容器](#2 使用 init 容器)


Pod 中 init 容器

Init 容器是一种特殊容器,在Pod 内的应用容器启动之前运行。Init 容器可以包括一些应用镜像中不存在的实用工具和安装脚本。

1 init 容器特点

init 容器与普通的容器非常像,除了如下几点:

  • 它们总是运行到完成。如果 Pod 的 Init 容器失败,kubelet 会不断地重启该 Init 容器直到该容器成功为止。 然而,如果 Pod 对应的 restartPolicy 值为 "Never",并且 Pod 的 Init 容器失败, 则 Kubernetes 会将整个 Pod 状态设置为失败。

  • 每个都必须在下一个启动之前成功完成。

  • 同时 Init 容器不支持 lifecyclelivenessProbereadinessProbestartupProbe, 因为它们必须在 Pod 就绪之前运行完成。

  • 如果为一个 Pod 指定了多个 Init 容器,这些容器会按顺序逐个运行。 每个 Init 容器必须运行成功,下一个才能够运行。当所有的 Init 容器运行完成时, Kubernetes 才会为 Pod 初始化应用容器并像平常一样运行。

  • Init 容器支持应用容器的全部字段和特性,包括资源限制、数据卷和安全设置。 然而,Init 容器对资源请求和限制的处理稍有不同。

2 使用 init 容器

官网地址: Init 容器 | Kubernetes

在 Pod 的规约中与用来描述应用容器的 containers 数组平行的位置指定 Init 容器。

bash 复制代码
apiVersion: v1
kind: Pod
metadata:
  name: init-demo
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', 'echo init-myservice is running! && sleep 5']
  - name: init-mydb
    image: busybox:1.28
    command: ['sh', '-c', 'echo init-mydb is running! && sleep 10']
  • 查看启动详细
bash 复制代码
$ kubectl describe pod init-demo
​
# 部分结果
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  2m16s  default-scheduler  Successfully assigned default/init-demo to k8s-node2
  Normal  Pulling    2m16s  kubelet            Pulling image "busybox:1.28"
  Normal  Pulled     118s   kubelet            Successfully pulled image "busybox:1.28" in 17.370617268s (17.370620685s including waiting)
  Normal  Created    118s   kubelet            Created container init-myservice
  Normal  Started    118s   kubelet            Started container init-myservice
  Normal  Pulled     112s   kubelet            Container image "busybox:1.28" already present on machine
  Normal  Created    112s   kubelet            Created container init-mydb
  Normal  Started    112s   kubelet            Started container init-mydb
  Normal  Pulled     101s   kubelet            Container image "busybox:1.28" already present on machine
  Normal  Created    101s   kubelet            Created container myapp-container
  Normal  Started    101s   kubelet            Started container myapp-container
相关推荐
fyakm5 小时前
K8s故障排查:常见问题定位与解决
docker·容器·kubernetes
2301_810746315 小时前
CKA冲刺40天笔记 - day10 K8S namespace
笔记·容器·kubernetes·k8s
abcy0712135 小时前
k8s ipc-namespace进程间通信隔离类型详解
docker·容器·kubernetes
Justice link5 小时前
K8S基本配置
运维·docker·容器
观熵5 小时前
SaaS 系统的自动化部署结构设计实战指南:基于 K8s + Helm 的工程落地路径
运维·kubernetes·自动化·saas 架构
若涵的理解5 小时前
一文读懂K8S kubectl 命令,运维小白必看!
运维·docker·kubernetes
java_logo5 小时前
2025 年 11 月最新 Docker 镜像源加速列表与使用指南
linux·运维·docker·容器·运维开发·kylin
牛肉胡辣汤5 小时前
【详解】K8S集群卸载清理
云原生·容器·kubernetes
峰顶听歌的鲸鱼6 小时前
Kubernetes管理
运维·笔记·云原生·容器·kubernetes·云计算
三不原则11 小时前
实战:混沌工程入门,模拟服务器宕机的故障演练
运维·kubernetes·chaos mesh