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

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: 在基于 `k8s` 平台的容器化部署环境中,有时候需要快速的实现部署文件的迁移备份,当 `k8s` 平台部署一个 `app` 时,都会相应的产生一堆 `yaml` 文件,如果 `yaml` 文件数量较少,我们可以人工手动的方式进行拷贝,但是当 `yaml` 文件数量多,并且该 `k8s` 平台部署了多个 `app` 时,如果在采用...

需求说明

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

image.png

功能实现

实现思路,这里主要是应用了 【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

参考文档

相关实践学习
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。 &nbsp; &nbsp; 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
目录
相关文章
|
9月前
|
关系型数据库 MySQL Shell
MySQL 备份 Shell 脚本:支持远程同步与阿里云 OSS 备份
一款自动化 MySQL 备份 Shell 脚本,支持本地存储、远程服务器同步(SSH+rsync)、阿里云 OSS 备份,并自动清理过期备份。适用于数据库管理员和开发者,帮助确保数据安全。
|
9月前
|
关系型数据库 Shell 网络安全
定期备份数据库:基于 Shell 脚本的自动化方案
本篇文章分享一个简单的 Shell 脚本,用于定期备份 MySQL 数据库,并自动将备份传输到远程服务器,帮助防止数据丢失。
|
Kubernetes 应用服务中间件 nginx
k8s学习--YAML资源清单文件托管服务nginx
k8s学习--YAML资源清单文件托管服务nginx
274 2
k8s学习--YAML资源清单文件托管服务nginx
|
缓存 监控 Shell
如何使用 HBase Shell 进行数据的实时监控和备份?
如何使用 HBase Shell 进行数据的实时监控和备份?
275 5
|
Kubernetes Cloud Native 流计算
Flink-12 Flink Java 3分钟上手 Kubernetes云原生下的Flink集群 Rancher Stateful Set yaml详细 扩容缩容部署 Docker容器编排
Flink-12 Flink Java 3分钟上手 Kubernetes云原生下的Flink集群 Rancher Stateful Set yaml详细 扩容缩容部署 Docker容器编排
357 3
|
Kubernetes Docker Python
dockercompose与k8s的pod文件的爱恨情仇
dockercompose与k8s的pod文件的爱恨情仇
|
Kubernetes Linux 容器
1.xshell传不了文件输出0000如何解决.....2.k8s中metalLB文件内容
1.xshell传不了文件输出0000如何解决.....2.k8s中metalLB文件内容
114 1
|
Kubernetes Docker Perl
k8s常见故障--yaml文件检查没有问题 pod起不来(一直处于创建中)
k8s常见故障--yaml文件检查没有问题 pod起不来(一直处于创建中)
463 1
|
3月前
|
Java 测试技术 数据安全/隐私保护
通过yaml文件配置自动化测试程序
通过yaml文件可以将自动化测试环境,测试数据和测试行为分开,请看一下案例
103 4

热门文章

最新文章

推荐镜像

更多