GKE 上的进程自行完成并重启
Process on GKE finishes and restarts by itself
我已经在 Google Kubernetes Engine 上创建了一个集群:
gcloud container clusters create training-cluster --num-nodes=1 --zone=us-central1-a --machine-type="n1-highmem-2" --scopes="gke-default,storage-rw"
我得到了凭据:
gcloud container clusters get-credentials --zone us-central1-a training-cluster
并应用我的 yaml 文件:
kubectl apply -f pod.yaml
yaml 文件如下所示:
apiVersion: v1
kind: Pod
metadata:
name: gke-training-pod
spec:
containers:
- name: my-custom-container
image: gcr.io/xyz/object-classification:gpu
args:
{I put my container arguments here}
我可以在日志中看到训练开始并最终结束。问题是它每次都重新启动,除非我删除我的集群。是否有我应该添加的论据来避免这种行为?
如果您是 运行 POD,则您也设置 restartPolicy: Never
Pod 的规范有一个 restartPolicy
字段,其可能的值为 Always、OnFailure 和 从不。默认值为始终。
The restartPolicy applies to all containers in the Pod. restartPolicy
only refers to restarts of the containers by the kubelet on the same
node. After containers in a Pod exit, the kubelet restarts them with
an exponential back-off delay (10s, 20s, 40s, …), that is capped at
five minutes. Once a container has executed for 10 minutes without any
problems, the kubelet resets the restart backoff timer for that
container.
否则
您可以使用 Kubernetes 中的 cronjobs 或 jobs 来来去去一次作业或容器结束 process.
https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/
如果你可以使用 cronjob 你可以添加 successfulJobsHistoryLimit: 0 所以一旦你的工作完成它会删除那个工作并且 从 cluster 中删除 pod 也自动 :
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
successfulJobsHistoryLimit: 0
failedJobsHistoryLimit: 0
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
如果你只想 运行 pod 一次(一旦 code/training 完成就停止生命周期),那么你必须在你的 pod yaml 中将 restartPolicy 更改为 Never 或 OnFailure定义文件。
containers:
- name: my-custom-container
image: gcr.io/xyz/object-classification:gpu
args:
{I put my container arguments here}
restartPolicy: Never
Always 意味着容器将重新启动,即使它以零退出代码退出(即成功)。当您不关心容器退出的原因时,这很有用,您只想确保它始终处于 运行ning(例如 Web 服务器)。这是默认值。
OnFailure 意味着容器只有在以非零退出代码退出时才会重新启动(即出现问题)。当您想要使用 pod 完成特定任务并确保它成功完成时,这很有用 - 如果没有成功,它将重新启动直到它完成。
Never表示不管容器为什么退出都不会重启
现在,如果您只想 运行 Pod 多次,最好的方法是使用 Kubernetes CronJobs/Jobs,正如 Harsh 提到的那样。在这种情况下,这将是最好的方法。
我已经在 Google Kubernetes Engine 上创建了一个集群:
gcloud container clusters create training-cluster --num-nodes=1 --zone=us-central1-a --machine-type="n1-highmem-2" --scopes="gke-default,storage-rw"
我得到了凭据:
gcloud container clusters get-credentials --zone us-central1-a training-cluster
并应用我的 yaml 文件:
kubectl apply -f pod.yaml
yaml 文件如下所示:
apiVersion: v1
kind: Pod
metadata:
name: gke-training-pod
spec:
containers:
- name: my-custom-container
image: gcr.io/xyz/object-classification:gpu
args:
{I put my container arguments here}
我可以在日志中看到训练开始并最终结束。问题是它每次都重新启动,除非我删除我的集群。是否有我应该添加的论据来避免这种行为?
如果您是 运行 POD,则您也设置 restartPolicy: Never
Pod 的规范有一个 restartPolicy
字段,其可能的值为 Always、OnFailure 和 从不。默认值为始终。
The restartPolicy applies to all containers in the Pod. restartPolicy only refers to restarts of the containers by the kubelet on the same node. After containers in a Pod exit, the kubelet restarts them with an exponential back-off delay (10s, 20s, 40s, …), that is capped at five minutes. Once a container has executed for 10 minutes without any problems, the kubelet resets the restart backoff timer for that container.
否则
您可以使用 Kubernetes 中的 cronjobs 或 jobs 来来去去一次作业或容器结束 process.
https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/
如果你可以使用 cronjob 你可以添加 successfulJobsHistoryLimit: 0 所以一旦你的工作完成它会删除那个工作并且 从 cluster 中删除 pod 也自动 :
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
successfulJobsHistoryLimit: 0
failedJobsHistoryLimit: 0
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
如果你只想 运行 pod 一次(一旦 code/training 完成就停止生命周期),那么你必须在你的 pod yaml 中将 restartPolicy 更改为 Never 或 OnFailure定义文件。
containers:
- name: my-custom-container
image: gcr.io/xyz/object-classification:gpu
args:
{I put my container arguments here}
restartPolicy: Never
Always 意味着容器将重新启动,即使它以零退出代码退出(即成功)。当您不关心容器退出的原因时,这很有用,您只想确保它始终处于 运行ning(例如 Web 服务器)。这是默认值。
OnFailure 意味着容器只有在以非零退出代码退出时才会重新启动(即出现问题)。当您想要使用 pod 完成特定任务并确保它成功完成时,这很有用 - 如果没有成功,它将重新启动直到它完成。
Never表示不管容器为什么退出都不会重启
现在,如果您只想 运行 Pod 多次,最好的方法是使用 Kubernetes CronJobs/Jobs,正如 Harsh 提到的那样。在这种情况下,这将是最好的方法。