批量导出deploy、svc、ingress等信息
#!/bin/bash
#Auth: zisefeizhu Time: 20210609 Version: v1
DEPLOY=`/usr/local/bin/kubectl get deployment -n zisefeizhu | awk '{print $1}'|grep -E "service|server" `
SVC=`/usr/local/bin/kubectl get svc -n zisefeizhu | awk '{$1=""; print $1}'`
INGRESS=`/usr/local/bin/kubectl get ingress -n zisefeizhu | awk '{$1=""; print $1}'`
pwd
if [ ! -d "$PWD/zisefeizhu" ]; then
mkdir -p /Users/edz/test/zisefeizhu
else
echo "/zisefeizhu 目录已经存在"
fi
mkdir -p $PWD/zisefeizhu/deploy
mkdir -p $PWD/zisefeizhu/svc
mkdir -p $PWD/zisefeizhu/ingress
for deploy in $DEPLOY
do
/usr/local/bin/kubectl get deployment $deploy -n zisefeizhu -o yaml > $PWD/zisefeizhu/deploy/"${deploy}".yaml;
done
for svc in $SVC
do
/usr/local/bin/kubectl get svc $svc -n zisefeizhu -o yaml > $PWD/zisefeizhu/svc/"${svc}".yaml;
done
for ingress in $INGRESS
do
/usr/local/bin/kubectl get ingress $ingress -n zisefeizhu -o yaml > $PWD/zisefeizhu/ingress/"${ingress}".yaml;
done
删除一些附加字段
for object in $(find . -name '*.yaml')
do
objectName=$object
/usr/local/bin/yq eval 'del(.metadata.annotations)' $object > ${object}.1
object=${object}.1
/usr/local/bin/yq eval 'del(.metadata.creationTimestamp)' $object > ${object}.1
object=${object}.1
/usr/local/bin/yq eval 'del(.metadata.generation)' $object > ${object}.1
object=${object}.1
/usr/local/bin/yq eval 'del(.metadata.resourceVersion)' $object > ${object}.1
object=${object}.1
/usr/local/bin/yq eval 'del(.metadata.selfLink)' $object > ${object}.1
object=${object}.1
/usr/local/bin/yq eval 'del(.metadata.uid)' $object > ${object}.1
object=${object}.1
/usr/local/bin/yq eval 'del(.status)' $object > $objectName
#sed -i '' 's/namespace: zisefeizhu/namespace: stage-zisefizhu/g' $objectName
rm -rf *.1
done