diff --git a/content/zh/docs/tasks/debug-application-cluster/debug-init-containers.md b/content/zh/docs/tasks/debug-application-cluster/debug-init-containers.md new file mode 100644 index 0000000000..95d1abc891 --- /dev/null +++ b/content/zh/docs/tasks/debug-application-cluster/debug-init-containers.md @@ -0,0 +1,224 @@ +--- +reviewers: +- bprashanth +- enisoc +- erictune +- foxish +- janetkuo +- kow3ns +- smarterclayton +title: 调试 Init 容器 +content_template: templates/task +--- + + + +{{% capture overview %}} + + + +此页显示如何核查与 init 容器执行相关的问题。 +下面的示例命令行将 Pod 称为 ``,而 init 容器称为 `` 和 ``。 + +{{% /capture %}} + +{{% capture prerequisites %}} + +{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} + + + +* 您应该熟悉 [Init 容器](/docs/concepts/abstractions/init-containers/)的基础知识。 +* 您应该已经[配置好一个 Init 容器](/docs/tasks/configure-pod-container/configure-pod-initialization/#creating-a-pod-that-has-an-init-container/)。 + +{{% /capture %}} + +{{% capture steps %}} + + + +## 检查 Init 容器的状态 + +显示你的 Pod 的状态: + +```shell +kubectl get pod +``` + + + +例如,状态 `Init:1/2` 表明两个 Init 容器中的一个已经成功完成: + +``` +NAME READY STATUS RESTARTS AGE + 0/1 Init:1/2 0 7s +``` + + + +更多状态值及其含义请参考[了解 Pod 的状态](#understanding-pod-status)。 + + + +## 获取 Init 容器详情 + + + +查看 Init 容器运行的更多详情: + +```shell +kubectl describe pod +``` + + + +例如,对于包含两个 Init 容器的 Pod 应该显示如下信息: + +``` +Init Containers: + : + Container ID: ... + ... + State: Terminated + Reason: Completed + Exit Code: 0 + Started: ... + Finished: ... + Ready: True + Restart Count: 0 + ... + : + Container ID: ... + ... + State: Waiting + Reason: CrashLoopBackOff + Last State: Terminated + Reason: Error + Exit Code: 1 + Started: ... + Finished: ... + Ready: False + Restart Count: 3 + ... +``` + + + +您还可以通过读取 Pod Spec 上的 `status.initContainerStatuses` 字段以编程方式了解 Init 容器的状态: + +```shell +kubectl get pod nginx --template '{{.status.initContainerStatuses}}' +``` + + + +此命令将返回与原始 JSON 中相同的信息. + + + +## 通过 Init 容器访问日志 + + + +一起传递 Init 容器名称与 Pod 名称来访问它的日志。 + +```shell +kubectl logs -c +``` + + + +运行 shell 脚本打印命令的init容器,执行 shell 脚本。 +例如,您可以在 Bash 中通过在脚本的开头运行 `set -x` 来实现。 + +{{% /capture %}} + +{{% capture discussion %}} + + + +## 了解 Pod 的状态 + + + +以 `Init:` 开头的 Pod 状态汇总了 Init 容器执行的状态。 +下表介绍调试 Init 容器时可能看到的一些状态值示例。 + + + +状态 | 含义 +------ | ------- +`Init:N/M` | Pod 包含 `M` 个 Init 容器,其中 `N` 个已经运行完成。 +`Init:Error` | Init 容器已执行失败。 +`Init:CrashLoopBackOff` | Init 容器反复执行失败。 +`Pending` | Pod 还没有开始执行 Init 容器。 +`PodInitializing` or `Running` | Pod 已经完成执行 Init 容器。 + +{{% /capture %}} +