【云原生】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
相关推荐
ん贤4 分钟前
Higress 详细笔记
笔记·云原生·higress
IT策士2 小时前
Docker 从 0 到 1 再到 Kubernetes 实战:第18篇 从 Docker Compose 到 Kubernetes 的思考
docker·容器·kubernetes
linmengmeng_13142 小时前
【总结】Docker 容器重建后 Nginx 502 问题排查与解决
nginx·docker·容器
qq_356408662 小时前
Kubernetes Rook-Ceph 高可用存储部署文档
ceph·容器·kubernetes
Plastic garden2 小时前
Docker(3)Docker 镜像 & Dockerfile
运维·docker·容器
m0_740859623 小时前
Docker安装常见数据库命令汇总(2026)
数据库·docker·容器
IT策士3 小时前
第16篇 实战:用 Docker Compose 编排 WordPress 与 MySQL
mysql·docker·容器
Cat_Rocky3 小时前
Jenkins通过kubernetes连接K8s集群
运维·kubernetes·jenkins
Plastic garden3 小时前
Docker(2)数据挂载
运维·docker·容器
Plastic garden3 小时前
Docker(4) Compose
运维·docker·容器