计划重启Kubernetes Pod而不造成停机 [英] Scheduled restart of Kubernetes pod without downtime

查看:115
本文介绍了计划重启Kubernetes Pod而不造成停机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有6个正在运行的Pod副本,我希望每5分钟重新启动\重新创建一次.

I have 6 replicas of a pod running which I would like to restart\recreate every 5 minutes.

这需要进行滚动更新-以便不立即终止所有更新,也不会造成停机.我该如何实现?

This needs to be a rolling update - so that all are not terminated at once and there is no downtime. How do I achieve this?

我尝试使用cron作业,但似乎不起作用:

I tried using cron job, but seems not to be working :

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: scheduled-pods-recreate
spec:
  schedule: "*/5 * * * *"
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: ja-engine
            image: app-image
            imagePullPolicy: IfNotPresent
          restartPolicy: OnFailure

尽管作业已成功创建并按照以下说明进行了安排,但它似乎从未运行过:

Although the job was created successfully and scheduled as per description below, it seems to have never run:

Name:                       scheduled-pods-recreate
Namespace:                  jk-test
Labels:                     <none>
Annotations:                <none>
Schedule:                   */5 * * * *
Concurrency Policy:         Forbid
Suspend:                    False
Starting Deadline Seconds:  <unset>
Selector:                   <unset>
Parallelism:                <unset>
Completions:                <unset>
Pod Template:
  Labels:  <none>
  Containers:
   ja-engine:
    Image:           image_url
    Port:            <none>
    Host Port:       <none>
    Environment:     <none>
    Mounts:          <none>
  Volumes:           <none>
Last Schedule Time:  Tue, 19 Feb 2019 10:10:00 +0100
Active Jobs:         scheduled-pods-recreate-1550567400
Events:
  Type    Reason            Age   From                Message
  ----    ------            ----  ----                -------
  Normal  SuccessfulCreate  23m   cronjob-controller  Created job scheduled-pods-recreate-1550567400

第一件事,如何确保它正在运行,以便重新创建Pod?

So first thing, how do I ensure that it is running so the pods are recreated?

我又如何确保没有停机时间?

Also how can I ensure no downtime?

cronjob的更新版本:

The updated version of the cronjob:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - kubectl patch deployment runners -p '{"spec":{"template":{"spec":{"containers":[{"name":"jp-runner","env":[{"name":"START_TIME","value":"'$(date +%s)'"}]}]}}}}' -n jp-test
          restartPolicy: OnFailure

吊舱不是以消息重新启动重启失败的容器和错误"开始的,如下所示:

The pods are not starting with the message Back-off restarting failed container and error as given below:

State:          Terminated
      Reason:       Error
      Exit Code:    127

推荐答案

Kubernetes目前没有滚动重启功能,但是您可以使用以下命令作为一种解决方法来重启特定部署中的所有Pod:
(用真实的名称替换部署名称和pod名称)

There is no rolling-restart functionality in Kubernetes at the moment, but you can use the following command as a workaround to restart all pods in the specific deployment:
(replace deployment name and pod name with the real ones)

kubectl patch deployment mydeployment -p '{"spec":{"template":{"spec":{"containers":[{"name":"my-pod-name","env":[{"name":"START_TIME","value":"'$(date +%s)'"}]}]}}}}'


要计划它,您可以在主节点上创建一个cron任务以定期运行此命令.
用户拥有的任务应具有正确的kubectl配置(~/.kube/config),并具有更改提到的部署对象的权限.


To schedule it, you can create a cron task on the master node to run this command periodically.
User owned the task should have correct kubectl configuration (~/.kube/config) with permissions to change the mentioned deployment object.

可以从/etc/kubernetes/admin.conf复制默认集群管理员配置:
(通常由kubeadm init创建):

Default cluster admin configuration can be copied from /etc/kubernetes/admin.conf :
(it is usually created by kubeadm init):

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config


两种类型的 部署更新策略 可以指定:重新创建(.spec.strategy.type==Recreate.)和滚动更新(.spec.strategy.type==RollingUpdate).


Two types of deployment update strategy can be specified: Recreate (.spec.strategy.type==Recreate.) and Rolling update (.spec.strategy.type==RollingUpdate).

只有使用滚动更新策略,您才能避免服务停机.您可以指定 maxUnavailable maxSurge 参数可控制滚动更新过程.

Only by using Rolling Update strategy you can avoid service downtime. You can specify maxUnavailable and maxSurge parameters in the deployment YAML to control the rolling update process.

这篇关于计划重启Kubernetes Pod而不造成停机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆