k8s发布版本状态查询shell

#!/bin/bash

NAMESPACE=$1
TARGET_VERSION=$2
INTERVAL=${3:-10} # Default check interval is 10 seconds

if [ -z "$NAMESPACE" ] || [ -z "$TARGET_VERSION" ]; then
  echo "Usage: $0 <namespace> <target-version> [check-interval-seconds]"
  exit 1
fi

echo "Monitoring pod versions in namespace '$NAMESPACE' to match version '$TARGET_VERSION' every $INTERVAL seconds"

while true; do
  # Get all pods in the namespace
  PODS=$(kubectl get pods -n $NAMESPACE -o jsonpath='{.items[*].metadata.name}')
  
  ALL_MATCH=true
  
  for POD in $PODS; do
    # Get the image version of each container in the pod
    CONTAINERS=$(kubectl get pod $POD -n $NAMESPACE -o jsonpath='{.spec.containers[*].image}' | grep -e fsts-data -e fsts-system -e fsts-inventory -e fsts-shdr -e fsts-gateway)
    
    for CONTAINER in $CONTAINERS; do
      IMAGE_VERSION=$(echo $CONTAINER | awk -F: '{print $2}')
      
      if [ "$IMAGE_VERSION" != "$TARGET_VERSION" ]; then
        echo "Pod $POD has container with image version $IMAGE_VERSION which does not match target version $TARGET_VERSION"
        ALL_MATCH=false
      else
        echo "Pod $POD has container with image version $IMAGE_VERSION which matches target version $TARGET_VERSION"
      fi
    done
  done

  if [ "$ALL_MATCH" = true ]; then
    echo "All pods in namespace '$NAMESPACE' have containers with the target version '$TARGET_VERSION'."
    break
  else
    echo "Not all pods have the target version. Waiting for $INTERVAL seconds before next check..."
    sleep $INTERVAL
  fi
done

echo "Version check complete."
echo "发布结束,所有应用服务版本发布至: "$TARGET_VERSION

NAMESPACE=$1
TARGET_VERSION=$2 是脚本在执行是传入参数。拉取发布版本,10秒查询一次。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值