shell 脚本实现 k8s 集群环境下指定 ns 资源的 yaml 文件备份

在这里插入图片描述

需求说明

在基于 k8s 平台的容器化部署环境中,有时候需要快速的实现部署文件的迁移备份,当 k8s 平台部署一个 app 时,都会相应的产生一堆 yaml 文件,如果 yaml 文件数量较少,我们可以人工手动的方式进行拷贝,但是当 yaml 文件数量多,并且该 k8s 平台部署了多个 app 时,如果在采用人工手动的方式实现这些 yaml 文件的拷贝,可想而知这个工作量是相当多且繁琐的,而这些机械化的人工操作还会产生误差,无法保障文件内容质量,针对这种场景,因此我们采用 shell 脚本来实现快速的 yaml 文件迁移拷贝,相应的提升工作效率并保障 yaml 文件内容质量。

功能实现

实现思路,这里主要是应用了 【bash & kubectl 】组合知识点,实现对 k8s 平台上指定 namespacesns,命名空间)下指定资源的 yaml 文件拷贝,shell 脚本实现如下:

shell 脚本实现

dump-k8s-yaml.sh 工具 shell 脚本编写如下:

#!/bin/bash

useage(){
    echo "Useage:"
    echo "  bash ./dump-k8s-yaml.sh DUMPDIR KUBECONFIG [NAMESPACE]"
}
 
if [ $# -lt 1 ];then
    useage
    exit
fi

dumpDir=$1
KF=$2
NS=$3
 
resourceList=(
  #componentstatuses       # cs
  configmaps              # cm
  secrets
  persistentvolumeclaims  # pvc
  events                  # ev
  serviceaccounts         # sa
  endpoints               # ep
  services                # svc
  ingress
  daemonsets              # ds
  deployments             # deploy
  replicasets             # rs
  statefulsets            # sts
  jobs
  cronjobs                # cj
  pods                    # po
)

showResourceList(){
  kubectl --kubeconfig=$KF -n=$NS get nodes
  kubectl --kubeconfig=$KF --sort-by=.metadata.name -n=$NS get pods
  kubectl --kubeconfig=$KF -n=$NS get cm,secrets,pvc,ev,sa,ep,svc,ingress,ds,deploy,rs,sts,jobs,cj
}

printList(){
  for aa in ${resourceList[@]};
  do
    aList=$(kubectl --kubeconfig=$KF -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 --kubeconfig=$KF -n=$NS get $aa $i -o yaml > $dumpDir/$aa/$i.yaml
      done
    fi
  done
}

zipExec(){
  #sudo apt install zip unzip
  #zip -v
  #unzip -v
  zip -r -q $dumpDir.zip file $dumpDir
  rm -rf $dumpDir
}
 
# create namespaces yaml
if [ ! -d $dumpDir ];then
  mkdir -p -m 777 ./$dumpDir
fi

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

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

echo "----[showResourceList]-----------------------"
showResourceList
echo "----[printList]-----------------------"
printList
echo "----[zipExec]-----------------------"
zipExec
echo "export ${NS} ymal completed!"

:<<!
使用方法:
bash dump-k8s-yaml.sh ./demons ./kube.conf demons

举例:
bash ./dump-k8s-yaml.sh ./dotnet-escada ./kube.conf dotnet-escada
!

shell 使用方式

注意:使用此命令,需要获得 kubectl 访问 k8s 集群环境的相应权限(即 kubeconfig 配置信息);

前置工具环境安装

从上面的 dump-k8s-yaml.sh 文件中可以看到部分工具的依赖,说明如下:

  • xshell 工具安装(请参照);
  • shell 终端(此处使用的 XShell)配置好 kubectl 工具,并获取到 kubeconfig 文件;
  • linux 环境安装文件压缩 & 解压工具 zip & unzip
  • linux 环境安装 lrzsz 工具(说明 lrzsz 工具只适合传输小文件,不适合传输大型文件,通常情况 lrzsz 配合 xshell 工具使用);

说明:此处 linux 环境的工具安装命令,适用基于 Ubuntu 的发行版,本人使用的环境是 Debian 11。其他 linux 发行版请自行查看对应的安装命令即可,下面的 dump-k8s-yaml.sh 工具在 linux 环境均可通用;

1、配置 kubectl,并导出 kubeconfig 文件请自行参考相关资料;

2、linux 宿主机没有安装 zip & unzip 工具,执行如下命令:

# linux 安装 zip,unzip:
   sudo apt install zip unzip

# 查看版本:
   zip -v
   unzip -v

# zip 压缩文件:
   zip -r -q dump-yaml.zip file ./dump-yaml

# unzip 压缩文件:
   unzip dump-yaml.zip

3、linux 宿主机安装 lrzsz 工具,执行如下命令:

sudo apt update && apt install lrzsz
...
XShell 文件上传 & 下载:
# 上传文件:rz 
# 单个文件下载:sz file dump-yaml.zip
# 多文文件下载(文件中间使用空格分开):sz file dump-yaml-1.zip dump-yaml-2.zip

dump-k8s-yaml.sh 使用方式

注意:使用【xshell& lrzsz】工具,先把【dump-k8s-yaml.sh】工具上传到 linux 环境,并指定 kubeconfig 文件,此处该文件名称为 kube.conf,和 dump-k8s-yaml.sh 工具是在同一个目录环境下(DUMPDIR 指定方便路径), kubeconfig 文件也可以放在其他路径。

在 linux 宿主机中准备好上面这些基础环境的配置和 shell 客户端工具安装后,接下来我们就可以使用 dump-k8s-yaml.sh 实现 k8s 环境中指定资源的 yaml 文件导出了,使用方式如下:

输入命令 bash ./dump-k8s-yaml.sh
cloud@k8s-node-1:~$ bash ./dump-k8s-yaml.sh 
Useage:
  bash ./dump-k8s-yaml.sh DUMPDIR KUBECONFIG [NAMESPACE]
  • 参数说明:
  1. bash 指定 shell 终端类型;
  2. ./dump-k8s-yaml.sh 当前目录下的 dump-k8s-yaml.sh 工具;
  3. DUMPDIR 指定 .zip 文件路径;
  4. KUBECONFIG 指定 kubeconfig 文件路径;
  5. NAMESPACE 指定 k8s 环境中 ns 命名空间名称;
dump-k8s-yaml.sh 应用举例
  • 举例:导出 nsdotnet-escada 下指定资源的 yaml 文件,并保存到当前路径的 dotnet-escada 目录中;
bash ./dump-k8s-yaml.sh ./dotnet-escada ./kube.conf dotnet-escada

dump-k8s-yaml.sh 输出日志信息

依据上面使用说明执行命令,输出如下日志结构信息:

persistentvolumes pvc-7a0027c9-84f7-40f9-90b2-929d1514d156
----[showResourceList]-----------------------
NAME                   READY   STATUS    RESTARTS   AGE
...
----[printList]-----------------------
...
----[zipExec]-----------------------
export dotnet-escada ymal completed!

以上面的【举例】为依据,执行命令输出完整日志信息如下:

cloud@demo-k8s-node-1:~$ bash ./dump-k8s-yaml.sh ./dotnet-escada ./kube.conf dotnet-escada
persistentvolumes pvc-7a0027c9-84f7-40f9-90b2-929d1514d156
----[showResourceList]-----------------------
NAME                                                    READY   STATUS    RESTARTS   AGE
deploy-demo-mes-keeperfile-host-75f8878ff7-8q98x    1/1     Running   2          27h
deploy-demo-mes-redis-55fc5dd9cc-6vtkh              1/1     Running   2          27h
deploy-demo-nginx-host-ddddfc864-zxkjb              1/1     Running   5          27h
deploy-demo-smartworx-plugs-host-5d9bd5c99c-rgtxh   0/1     Evicted   0          28h
NAME                                                   DATA   AGE
configmap/demo-mes-authentication-service-config   1      30h
configmap/demo-mes-config                          1      30h
configmap/demo-mes-keeper-appseetings              2      30h
configmap/demo-mes-nginx-config                    1      29h
configmap/demo-mes-nginx-service-config            1      29h
configmap/demo-mes-redis-service-config            1      27h
configmap/kube-root-ca.crt                             1      31h

NAME                                                                              TYPE                                  DATA   AGE
secret/ceph-kubernetes-dynamic-user-01847765-90af-11ed-bbdc-06eca119f7b2-secret   Opaque                                1      31h
secret/default-token-k85zv                                                        kubernetes.io/service-account-token   3      31h

NAME                                             STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/pvc-demo-escada-data   Bound    pvc-7a0027c9-84f7-40f9-90b2-929d1514d156   10Gi       RWO            cephfs         31h

LAST SEEN   TYPE     REASON   OBJECT                        MESSAGE
3m24s       Normal   Sync     ingress/escada-inkelink-com   Scheduled for sync
3m22s       Normal   Sync     ingress/escada-inkelink-com   Scheduled for sync

NAME                     SECRETS   AGE
serviceaccount/default   1         31h

NAME                                      ENDPOINTS         AGE
endpoints/demo-mes-keeperfile-host    10.44.0.12:80     28h
endpoints/demo-mes-redis              10.44.0.45:6379   27h
endpoints/demo-nginx-host             10.44.0.20:80     27h
endpoints/demo-scada-base-host        <none>            28h
endpoints/demo-smartworx-host         <none>            28h
endpoints/demo-smartworx-plugs-host   <none>            28h

NAME                                    TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/demo-mes-keeperfile-host    ClusterIP      10.108.90.134    <none>        80/TCP         28h
service/demo-mes-redis              ClusterIP      10.99.71.77      <none>        6379/TCP       27h
service/demo-nginx-host             LoadBalancer   10.108.145.181   10.23.4.193   80:31995/TCP   27h
service/demo-scada-base-host        ClusterIP      10.110.121.140   <none>        80/TCP         28h
service/demo-smartworx-host         ClusterIP      10.110.63.82     <none>        80/TCP         28h
service/demo-smartworx-plugs-host   ClusterIP      10.105.115.242   <none>        80/TCP         28h

NAME                                            CLASS   HOSTS                 ADDRESS       PORTS   AGE
ingress.networking.k8s.io/escada-inkelink-com   nginx   escada.inkelink.com   10.23.4.192   80      27h

NAME                                                   READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/deploy-demo-mes-keeperfile-host    1/1     1            1           28h
deployment.apps/deploy-demo-mes-redis              1/1     1            1           27h
deployment.apps/deploy-demo-nginx-host             1/1     1            1           27h
deployment.apps/deploy-demo-scada-base-host        0/0     0            0           28h
deployment.apps/deploy-demo-smartworx-host         0/0     0            0           28h
deployment.apps/deploy-demo-smartworx-plugs-host   0/0     0            0           28h

NAME                                                              DESIRED   CURRENT   READY   AGE
replicaset.apps/deploy-demo-mes-keeperfile-host-75f8878ff7    1         1         1       28h
replicaset.apps/deploy-demo-mes-redis-55fc5dd9cc              1         1         1       27h
replicaset.apps/deploy-demo-nginx-host-ddddfc864              1         1         1       27h
replicaset.apps/deploy-demo-scada-base-host-6cb55ccd97        0         0         0       28h
replicaset.apps/deploy-demo-smartworx-host-7df5945c7          0         0         0       28h
replicaset.apps/deploy-demo-smartworx-host-856db4cd49         0         0         0       28h
replicaset.apps/deploy-demo-smartworx-host-9c5ff7859          0         0         0       26h
replicaset.apps/deploy-demo-smartworx-plugs-host-5d9bd5c99c   0         0         0       28h

NAME                                                        READY   STATUS    RESTARTS   AGE
pod/deploy-demo-mes-keeperfile-host-75f8878ff7-8q98x    1/1     Running   2          27h
pod/deploy-demo-mes-redis-55fc5dd9cc-6vtkh              1/1     Running   2          27h
pod/deploy-demo-nginx-host-ddddfc864-zxkjb              1/1     Running   5          27h
pod/deploy-demo-smartworx-plugs-host-5d9bd5c99c-rgtxh   0/1     Evicted   0          28h
----[printList]-----------------------
configmaps demo-mes-authentication-service-config
configmaps demo-mes-config
configmaps demo-mes-keeper-appseetings
configmaps demo-mes-nginx-config
configmaps demo-mes-nginx-service-config
configmaps demo-mes-redis-service-config
configmaps kube-root-ca.crt
secrets ceph-kubernetes-dynamic-user-01847765-90af-11ed-bbdc-06eca119f7b2-secret
secrets default-token-k85zv
persistentvolumeclaims pvc-demo-escada-data
events LAST
Error from server (NotFound): events "LAST" not found
events 3m26s
Error from server (NotFound): events "3m26s" not found
events 3m24s
Error from server (NotFound): events "3m24s" not found
serviceaccounts default
endpoints demo-mes-keeperfile-host
endpoints demo-mes-redis
endpoints demo-nginx-host
endpoints demo-scada-base-host
endpoints demo-smartworx-host
endpoints demo-smartworx-plugs-host
services demo-mes-keeperfile-host
services demo-mes-redis
services demo-nginx-host
services demo-scada-base-host
services demo-smartworx-host
services demo-smartworx-plugs-host
ingress escada-inkelink-com
No resources found in demo-dotnet-escada namespace.
deployments deploy-demo-mes-keeperfile-host
deployments deploy-demo-mes-redis
deployments deploy-demo-nginx-host
deployments deploy-demo-scada-base-host
deployments deploy-demo-smartworx-host
deployments deploy-demo-smartworx-plugs-host
replicasets deploy-demo-mes-keeperfile-host-75f8878ff7
replicasets deploy-demo-mes-redis-55fc5dd9cc
replicasets deploy-demo-nginx-host-ddddfc864
replicasets deploy-demo-scada-base-host-6cb55ccd97
replicasets deploy-demo-smartworx-host-7df5945c7
replicasets deploy-demo-smartworx-host-856db4cd49
replicasets deploy-demo-smartworx-host-9c5ff7859
replicasets deploy-demo-smartworx-plugs-host-5d9bd5c99c
No resources found in demo-dotnet-escada namespace.
No resources found in demo-dotnet-escada namespace.
No resources found in demo-dotnet-escada namespace.
pods deploy-demo-mes-keeperfile-host-75f8878ff7-8q98x
pods deploy-demo-mes-redis-55fc5dd9cc-6vtkh
pods deploy-demo-nginx-host-ddddfc864-zxkjb
pods deploy-demo-smartworx-plugs-host-5d9bd5c99c-rgtxh
----[zipExec]-----------------------
export dotnet-escada ymal completed!

dump-k8s-yaml.sh 执行完成后,在当前环境目录下会生成一个对应的 .zip 压缩文件,上面例子中对应产生的文件是 dotnet-escada.zip ,此时我们可以利用【xshell & lrzsz】工具下载 dotnet-escada.zip 文件,执行命令如下:

sz file dotnet-escada.zip

参考文档

  • kubectl 命令行工具,https://kubernetes.io/zh-cn/docs/reference/kubectl/
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ChaITSimpleLove

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值