本文介绍了确保Kubernetes部署已完成并且所有Pod已更新且可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

部署状态表示您可以查看部署observedGenerationgeneration,以及何时observedGeneration >= generation则部署成功.很好,但是我有兴趣知道新容器何时在我的Pod的所有 all 中实际运行,因此,如果我命中了一项服务,我肯定知道我正在命中一个代表该容器的服务器.最新部署的容器.

The status of a deployment indicates that you can look at a deployments observedGeneration vs generation and when observedGeneration >= generation then the deployment succeeded. That's fine, but I'm interested in knowing when the new container is actually running in all of my pods, so that if I hit a service I know for sure I'm hitting a server that represents the latest deployed container.

K8S Slack成员的另一个提示:

Another tip from a K8S Slack member:

kubectl get deployments | grep <deployment-name> | sed 's/ /,/g' | cut -d ' ' -f 4

我部署了错误的映像,结果为ErrImagePull,但是部署仍然报告了正确数量的8个最新副本(可用副本为7个).

I deployed a bad image, resulting in ErrImagePull, yet the deployment still reported the correct number of 8 up-date-date replicas (available replicas was 7).

推荐答案

更新#2: Kubernetes 1.5将随附更好的kubectl rollout status版本,并在1.6中进一步改进,可能会取代我的自定义解决方案/脚本在下面列出.

Update #2: Kubernetes 1.5 will ship with a much better version of kubectl rollout status and improve even further in 1.6, possibly replacing my custom solution/script laid out below.

更新#1:我已将答案变成托管在以下脚本上Github 到现在为止,PR已有少量改善.

Update #1: I have turned my answer into a script hosted on Github which has received a small number of improving PRs by now.

原始答案:

首先,我相信您得到的kubectl命令是不正确的:它用逗号替换所有空白,但在用空格分隔后尝试获取第4个字段.

First of all, I believe the kubectl command you got is not correct: It replaces all white spaces by commas but then tries to get the 4th field after separating by white spaces.

为了验证是否已将部署(或其升级)部署到所有Pod,我认为您应该检查可用副本的数量是否与所需副本的数量相匹配.也就是说,kubectl输出中的AVAILABLEDESIRED列是否相等.虽然您可以通过

In order to validate that a deployment (or upgrade thereof) made it to all pods, I think you should check whether the number of available replicas matches the number of desired replicas. That is, whether the AVAILABLE and DESIRED columns in the kubectl output are equal. While you could get the number of available replicas (the 5th column) through

kubectl get deployment nginx | tail -n +2 | awk '{print $5}'

以及所需的副本数(第二列)至

and the number of desired replicas (2nd column) through

kubectl get deployment nginx | tail -n +2 | awk '{print $2}'

一种更干净的方法是使用kubectl的jsonpath输出,尤其是如果您也要考虑官方文档中提到的生成要求.

a cleaner way is to use kubectl's jsonpath output, especially if you want to take the generation requirement that the official documentation mentions into account as well.

这是我写的一个快速bash脚本,希望在命令行上为其指定部署名称,等待观察到的世代成为指定的副本,然后等待可用的副本达到指定副本的数量:

Here's a quick bash script I wrote that expects to be given the deployment name on the command line, waits for the observed generation to become the specified one, and then waits for the available replicas to reach the number of the specified ones:

#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset

deployment=

get_generation() {
  get_deployment_jsonpath '{.metadata.generation}'
}

get_observed_generation() {
  get_deployment_jsonpath '{.status.observedGeneration}'
}

get_replicas() {
  get_deployment_jsonpath '{.spec.replicas}'
}

get_available_replicas() {
  get_deployment_jsonpath '{.status.availableReplicas}'
}

get_deployment_jsonpath() {
  local readonly _jsonpath="$1"

  kubectl get deployment "${deployment}" -o "jsonpath=${_jsonpath}"
}

if [[ $# != 1 ]]; then
  echo "usage: $(basename $0) <deployment>" >&2
  exit 1
fi

readonly deployment="$1"

readonly generation=$(get_generation)
echo "waiting for specified generation ${generation} to be observed"
while [[ $(get_observed_generation) -lt ${generation} ]]; do
  sleep .5
done
echo "specified generation observed."

readonly replicas="$(get_replicas)"
echo "specified replicas: ${replicas}"

available=-1
while [[ ${available} -ne ${replicas} ]]; do
  sleep .5
  available=$(get_available_replicas)
  echo "available replicas: ${available}"
done

echo "deployment complete."

这篇关于确保Kubernetes部署已完成并且所有Pod已更新且可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 17:16