一个shell对比内容脚本,镜像的digests对比,注意格式
#!/bin/bash#本地获取digests值dg=$(docker images --digests | grep bin_spinsrv | grep k8sv33 | awk '{print $3}')echo $dg > /root/t3.json#远程获取digests值dj=$(curl --header "Accept: application/vnd.docker.dist
·
#!/bin/bash
#本地获取digests值
dg=$(docker images --digests | grep bin_spinsrv | grep k8sv33 | awk '{print $3}')
echo $dg > /root/t3.json
#远程获取digests值
dj=$(curl --header "Accept: application/vnd.docker.distribution.manifest.v2+json" -I -X GET http://192.168.1.7:5000/v2/biinsrv/manifests/k8sv33 | grep Digest | awk '{print$2}')
echo $dj > /root/t4.json
#diff和cmp都可以对比 ,但是diff -w 忽略全部的空格字符。 -b 不检查空格字符的不同
#这里没有加-w或者-b,cat 文件内容一样digests的值,ls -l的时候看字节不同
df=$(diff -w /root/t3.json /root/t4.json)
echo $df
#查看是否输出空值
if [ "$df" = "" ]; then
echo "IS NULL"
else
echo "NOT NULL"
fi
#srvname=$(kubectl get pods | grep spinsrv | awk '{print$1}')
#echo $srvname
#kubectl delete pods $srvname
注意格式工整问题 ,不然结果就可能不一样
dj=$(curl --header "Accept: application/vnd.docker.distribution.manifest.v2+json" -I -X GET http://192.168.1.7:5000/v2/biinsrv/manifests/k8sv33 | grep Digest | awk '{print$2}')
dg=$(docker images --digests | grep bin_spinsrv | grep k8sv33 | awk '{print $3}')
#!/bin/bash
serviceList="123srv 456srv 789srv 234srv 345srv 567srv 678srv"
for item in $serviceList
do
# echo "开始检测服务$item"
valuea=`kubectl describe pods $(kubectl get pods -o wide|grep $item| awk '{print $1}')|grep '@sha256:'|awk '{print $3}'|awk -F"@" '{print $2}'|sed 's/[[:space:]]//g'`
echo "###本地k8s###"
echo $item
echo $valuea
valueb=$(curl --header "Accept: application/vnd.docker.distribution.manifest.v2+json" -I -X GET http://192.168.1.7:5000/v2/bin_${item}/manifests/latest |sed -n '/Docker-Content-Digest/'p|awk '{print $2}'|sed 's/[[:space:]]//g')
echo "***远程镜像仓库***"
echo $valueb
if [ $valuea == $valueb ]; then
echo "无需更新"
else
echo "要更新"
#srvname=$(kubectl get pods | grep $item | awk '{print$1}')
#echo $srvname
#kubectl delete pods $srvname
fi
echo "----------------------"
done
最终版 ,这里$#,可以改成 $@ 就可以多个服务了 ,而不是一个或者全部
#!/bin/bash
serviceList0="[ 123srv 456srv 789srv 234srv 345srvsrv 567srv 678srvsrv ]"
serviceList="123srv 456srv 789srv 234srv 345srvsrv 567srv 678srvsrv"
updateSrvList=""
if [[ $# == 1 ]]; then
echo "指定服务更新模式"
serviceList=$1
elif [[ $# > 1 ]]; then
echo "传参错误!选择$serviceList0中一个"
exit
fi
for item in $serviceList
do
echo "开始检测服务$item"
tmp=`kubectl describe pods $(kubectl get pods -o wide|grep $item| awk '{print $1}')`
valuea=`kubectl describe pods $(kubectl get pods -o wide|grep $item| awk '{print $1}')|grep '@sha256:'|awk '{print $3}'|awk -F"@" '{print $2}'|sed 's/[[:space:]]//g'
`
if [[ ${#valuea} != 71 ]];then
echo "未找到服务${item},选择$serviceList0中一个"
continue
fi
valueb=$(curl --header "Accept: application/vnd.docker.distribution.manifest.v2+json" -s -I -X GET http://192.168.1.70:5000/v2/bin_${item}/manifests/latest |sed -n '/Docker-Content-Digest/'p|awk '{print $2}'|sed 's/[[:space:]]//g')
if [[ ${#valueb} != 71 ]];then
echo "未找到服务${item},选择$serviceList0中一个"
continue
fi
if [[ $valuea == $valueb ]]; then
echo "无需更新"
else
updateSrvList=${updateSrvList}$item","
echo "要更新!!!!!"
srvname=$(kubectl get pods | grep $item | awk '{print$1}')
kubectl delete pods $srvname
fi
echo "----------------------"
done
if [[ ${#updateSrvList} != 0 ]];then
echo "更新了:"$updateSrvList
else
echo "此次操作没有任何更新"
fi
更多推荐
已为社区贡献13条内容
所有评论(0)