升级使用某镜像版本的Deployments
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
OLD_IMAGE=$1
NEW_IMAGE=$2
CONTAINER_NAME=$3
NAME_SPACE=$4
# 获取使用指定镜像的所有 Deployments
DEPLOYMENTS=$(kubectl -n $NAME_SPACE get deployments -o json | jq '.items[] | select(.spec.template.spec.initContainers[].image == "'$OLD_IMAGE'") | .metadata.name')
# 检查是否找到任何 Deployment
if [ -z "$DEPLOYMENTS" ]; then
echo "没有找到使用镜像 $OLD_IMAGE 的 Deployment"
exit 0
fi
# 遍历每个 Deployment 并更新镜像
for DEPLOY in $DEPLOYMENTS; do
# 去除 DEPLOY 中可能存在的引号
DEPLOY=$(echo "$DEPLOY" | tr -d '"')
echo "正在更新 Deployment: $DEPLOY"
# 更新 Deployment 的镜像
kubectl -n $NAME_SPACE set image deployment/$DEPLOY $CONTAINER_NAME=$NEW_IMAGE
if [ $? -eq 0 ]; then
echo "Deployment $DEPLOY 更新成功"
else
echo "Deployment $DEPLOY 更新失败"
fi
done
echo "所有 Deployments 更新完成"