确保Kubernetes部署已完成,所有pod都已更新并可用

dev*_*vth 16 bash kubernetes

部署状态表明您可以查看部署observedGenerationvs generation以及何时observedGeneration >= generation部署成功.这很好,但我很想知道新容器何时实际运行在我的所有 pod中,所以如果我点击服务,我肯定知道我正在点击代表最新部署容器的服务器.

来自K8S Slack成员的另一个提示:

kubectl get deployments | grep <deployment-name> | sed 's/ /,/g' | cut -d ' ' -f 4
Run Code Online (Sandbox Code Playgroud)

我部署了一个坏图像,ErrImagePull但部署仍然报告了8个最新日期副本的正确数量(可用副本为7).

Tim*_*ann 33

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

更新#1:我已将我的答案转换为在Github托管脚本,脚本现已收到少量改进的PR.

原始答案:

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

为了验证部署(或升级)是否适用于所有pod,我认为您应该检查可用副本的数量是否与所需副本的数量相匹配.也就是说,输出中的AVAILABLEDESIRED列是否kubectl相等.虽然您可以获得可用副本的数量(第5列)

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

和所需复制品的数量(第2列)到

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

kubectl简洁的方法是使用jsonpath输出,特别是如果你想要考虑官方文档提到的生成要求.

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

#!/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."
Run Code Online (Sandbox Code Playgroud)

  • 更新后通过引用1.5/1.6中大大改进的`kubectl rollout status`来回答. (3认同)