apiVersion: v1 kind: Pod metadata: name: ml-worker spec: restartPolicy: Never # Pod 本身不会自动重启,除非被显式指定。 initContainers: - name: setup-environment image: registry.k8s.io/busybox:1.27.2 command: ['sh', '-c', 'echo "Setting up environment"'] # 这个 Init 容器只运行一次,用于准备环境。 # 在触发 RestartAllContainers 动作后,此容器会再次运行。 - name: watcher-sidecar image: registry.k8s.io/busybox:1.27.2 # 在真实场景中,这里通常会使用一个专用的 watcher 镜像。 # 此命令用于模拟 watcher 以特定的退出码退出。 command: ['sh', '-c', 'sleep 60; exit 88'] restartPolicy: Always restartPolicyRules: - action: RestartAllContainers onExit: exitCodes: # 退出码 88 会触发整个 Pod 的完全重启。 operator: In values: [88] containers: - name: main-application image: registry.k8s.io/busybox:1.27.2 command: ['sh', '-c', 'echo "Application is running"; sleep 3600']