shell脚本导出k8s资源文件

#!/bin/bash

useage(){
    echo "useage:"
    echo "  dump_k8s_yaml.sh DUMPDIR [NAMESPACE]"
}

if [ $# -lt 1 ];then
    useage
    exit
fi

NS=$2
dumpDir=$1

resourceList=(
deploy
services
endpoints
ingress
secrets
pvc
cm
)

printList(){
  for aa in ${resourceList[@]};
  do
    aList=$(kubectl  -n $NS get $aa |grep -v NAME  |awk '{print $1}')
    if [ ! "${aList[*]}"x == "x" ];then
      [ -d $dumpDir/$aa ] || mkdir -p $dumpDir/$aa
      for i in $aList;
      do
        echo $aa $i
        kubectl -n $NS get $aa $i -o yaml > $dumpDir/$aa/$i.yaml
      done
    fi
  done
}

# create namespaces yaml
if [ ! -d $dumpDir ];then
  mkdir -p $dumpDir
fi


kubectl  get namespaces $NS -o yaml > $dumpDir/namespaces.yaml

# create pv yaml
pvList=$(kubectl get pv |grep "$NS/" |awk '{print $1}')
if [ ! "${pvList[*]}"x == "x" ];then
  [ -d $dumpDir/pv ] || mkdir -p $dumpDir/pv
  for i in ${pvList[@]}
  do
    echo pv $i
    kubectl get pv $i -o yaml > $dumpDir/pv/$i.pv.yaml
  done
fi

printList

使用用法:./dum_k8s_yaml $Dir $NAMESPACE

./dump_k8s_yaml ./dmp dmp

打印日志信息如下:

[root@kubernetes1 opt]# ./dump_k8s_yaml.sh ./dmp dmp
pv legodatacollector-pv-dmp
pv pvc-04e19d7c-7da9-4171-8742-60e0724e8141
deploy datacollector-deploy
deploy dmpbizbigdata-deploy
deploy dmpbizbus-deploy
deploy dmpbizconvert-deploy
deploy dmpbizschedule-deploy
deploy dmpbizschedulemgr-deploy
deploy dmpbizservice-deploy
deploy dmpdatahunt-deploy
deploy dmpdataparser-deploy
deploy dmpdataservice-deploy
deploy dmpdatasubmit-deploy
deploy dmpdatawarehouse-deploy
deploy dmpgateway-deploy
deploy dmpweb-deploy
services dmpbizbigdata-svc
services dmpbizbus-svc
services dmpbizconvert-svc
services dmpbizschedule-svc
services dmpbizschedulemgr-svc
services dmpbizservice-svc
services dmpdatahunt-svc
services dmpdataparser-svc
services dmpdataservice-svc
services dmpdatasubmit-svc
services dmpdatawarehouse-svc
services dmpgateway-svc
services dmpweb-svc
endpoints dmpbizbigdata-svc
endpoints dmpbizbus-svc
endpoints dmpbizconvert-svc
endpoints dmpbizschedule-svc
endpoints dmpbizschedulemgr-svc
endpoints dmpbizservice-svc
endpoints dmpdatahunt-svc
endpoints dmpdataparser-svc
endpoints dmpdataservice-svc
endpoints dmpdatasubmit-svc
endpoints dmpdatawarehouse-svc
endpoints dmpgateway-svc
endpoints dmpweb-svc
ingress dmpweb-ingress
secrets default-token-ktkgv
secrets harbor-secret
pvc legodatacollector-pvc
pvc pinpoint-pvc
cm datacollector-cm
cm dmpbizbigdata-cm
cm dmpbizbus-cm
cm dmpbizconvert-cm
cm dmpbizschedule-cm
cm dmpbizschedulemgr-cm
cm dmpbizservice-cm
cm dmpdatahunt-cm
cm dmpdataparser-cm
cm dmpdataservice-cm
cm dmpdatasubmit-cm
cm dmpdatawarehouse-cm
cm dmpgateway-cm
cm dmpweb-cm
cm kube-root-ca.crt

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐