搜索

3.3.2. Init 容器

download PDF

init 容器 是在 pod 应用程序容器启动前启动的 pod 中的容器。Init 容器可以共享卷,执行网络操作,并在剩余的容器启动前执行计算。Init 容器也可以阻止或延迟应用程序容器的启动,直到满足一些前提条件为止。

当 pod 启动时,在网络和卷初始化后,初始容器会按顺序启动。每个 init 容器都必须成功退出,然后调用下一个容器。如果 init 容器启动(因为运行时无法启动)或退出失败,则会根据 Pod 重启策略 来重试它。

直到所有 init 容器都成功前,pod 无法就绪。

有关一些 init 容器使用示例,请参阅 Kubernetes 文档。

以下示例概述了一个包含两个 init 容器的简单 pod。第一个 init 容器会等待 myservice,第二个则等待 mydb。两个容器都成功后,Pod 就会启动。

Init 容器 Pod 对象定义(YAML)示例

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice 1
    image: busybox
    command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
  - name: init-mydb 2
    image: busybox
    command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']

1
指定 myservice 容器。
2
指定 mydb 容器。

每个 init 容器都具有 app 容器的所有字段,readinessProbe 除外。init 容器必须退出才能继续,且无法定义除完成之外的就绪度。

init 容器可以在 pod 上包含 activeDeadlineSecondslivenessProbe,以防止 init 容器永远失败。活动截止时间包括 init 容器。

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.