简体   繁体   English

Jenkins 中的 Kubernetes 部署状态

[英]Kubernetes deployment status in Jenkins

I am writing a Jenkins Global pipeline library where I have a stage to deploy my docker image to K8s cluster.我正在编写一个 Jenkins Global 管道库,在那里我有一个阶段可以将我的 docker 映像部署到 K8s 集群。 So after building my docker image during CI process I am promoting(deploying) the image to multiple environments(sequentially lower to higher).因此,在 CI 过程中构建我的 docker 镜像后,我将镜像提升(部署)到多个环境(按顺序从低到高)。 So, to get the correct status of deployment after running所以,要在运行后获得正确的部署状态

kubectl apply -f Application-k8s-file.yaml

I used following command in a shell step.我在 shell 步骤中使用了以下命令。

kubectl rollout status deployment deployment_name

Things goes well if my deployment does not have error but if my deployment has some error(might be some code bug, application does not start) then this command kubectl rollout status deployment <deployment name> runs infinitely(as k8s retries again and again to redeploy) and my Jenkins job runs infinitely(till the Job timeout).如果我的部署没有错误但如果我的部署有一些错误(可能是一些代码错误,应用程序没有启动)那么这个命令kubectl rollout status deployment <deployment name>无限运行(因为 k8s 一次又一次地重试重新部署)并且我的 Jenkins 作业无限运行(直到作业超时)。

So to find a hack I tried a logic to put the timeout on this command and calculations are something like this:因此,为了找到一个 hack,我尝试了一种逻辑,将超时置于此命令上,计算如下:

timeout = (number of pods * liveness probe time + number of pods* 10) seconds超时 =(pod 数 * 活性探测时间 + pod 数 * 10)秒

Not sure if this calculation is correct or not.不确定这个计算是否正确。

My code snippet looks like this我的代码片段看起来像这样

        sh(returnStdout: true,script:"#!/bin/sh +e\n timeout --preserve-status ${timeout_value} kubectl rollout status deployment ${deploymentName} --kubeconfig='/opt/kubernetes-secrets/${env}/kubeconfig' 2>tmpfile; echo \$? > tmpfile1")
    def readThisFile = readFile "tmpfile1.txt"

def var=readThisFile.toInteger()

           if(var==0)
           {
             echo "deployment successful"
           }
       else{"do something else"}

This works well initially but later I find that k8s "kubectl rollout status deployment " command doesn't give exit code 0 until all the pods get scheduled and old get terminated completely which sometimes take time.这最初效果很好,但后来我发现 k8s“kubectl rollout status deployment”命令不会给出退出代码 0,直到所有 pod 都被安排好并且旧的 pod 完全终止,这有时需要时间。

What I basically want is a minimal calculated timeout value.我基本上想要的是一个最小的计算超时值。

My K8s file have parameters like this:我的 K8s 文件有这样的参数:

   spec:
     minReadySeconds: 30

    livenessProbe:
      httpGet:
        path: /ping
        port: 80
      initialDelaySeconds: 45
      periodSeconds: 5
      timeoutSeconds: 60
    name: test-dummyservice
    ports:
    - containerPort: 80
    readinessProbe:
      httpGet:
        path: /health
        port: 80
      initialDelaySeconds: 60
      periodSeconds: 120
      timeoutSeconds: 60

I did not find anything specific related to this in K8s documentation.我在 K8s 文档中没有找到任何与此相关的特定内容。 Anyone facing same challenge?有人面临同样的挑战吗?

You should take a look at progressDeadlineSeconds .你应该看看progressDeadlineSeconds Once this exceeds the deadline the rollout status will exit out.一旦超过最后期限, rollout status将退出。

kubectl rollout status deployment ng                                                                                                                      
Waiting for rollout to finish: 2 out of 7 new replicas have been updated...
error: deployment "ng" exceeded its progress deadline

https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#progress-deadline-seconds https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#progress-deadline-seconds

您可以添加如下timeout标志,这是文档https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands

kubectl rollout status deployment deployment_name --watch --timeout=5m

If you don't want to wait for the rollout to finish then you can use --watch=false .如果您不想等待部署完成,则可以使用--watch=false

kubectl rollout status deployment deployment_name --watch=false

Now, you can check with this command for a duration with specific interval.现在,您可以使用此命令检查具有特定间隔的持续时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM