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

参考文档

相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务&nbsp;ACK 容器服务&nbsp;Kubernetes&nbsp;版(简称&nbsp;ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情:&nbsp;https://www.aliyun.com/product/kubernetes
目录
相关文章
|
1天前
|
分布式计算 Hadoop Shell
使用shell脚本实现自动SSH互信功能
使用shell脚本实现自动SSH互信功能
10 1
|
2天前
|
Unix Shell Linux
轻松编写 AIX Shell 脚本
轻松编写 AIX Shell 脚本
10 1
|
3天前
|
监控 关系型数据库 Shell
Shell脚本入门:从基础到实践,轻松掌握Shell编程
Shell脚本入门:从基础到实践,轻松掌握Shell编程
|
3天前
|
关系型数据库 MySQL Shell
在Centos7中利用Shell脚本:实现MySQL的数据备份
在Centos7中利用Shell脚本:实现MySQL的数据备份
|
4天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
22 5
|
5天前
|
Shell 程序员 数据安全/隐私保护
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)
|
5天前
|
存储 Shell C语言
shell脚本 编程 变量 基本入门(详解)
shell脚本 编程 变量 基本入门(详解)
|
15天前
|
运维 Kubernetes 监控
Kubernetes 集群的持续性能优化实践
【4月更文挑战第26天】 在动态且不断增长的云计算环境中,维护高性能的 Kubernetes 集群是一个挑战。本文将探讨一系列实用的策略和工具,旨在帮助运维专家监控、分析和优化 Kubernetes 集群的性能。我们将讨论资源分配的最佳实践,包括 CPU 和内存管理,以及集群规模调整的策略。此外,文中还将介绍延迟和吞吐量的重要性,并提供日志和监控工具的使用技巧,以实现持续改进的目标。
|
22小时前
|
存储 运维 监控
Kubernetes 集群的持续监控与性能优化策略
【5月更文挑战第11天】在微服务架构日益普及的当下,Kubernetes 已成为容器编排的事实标准。随着其在不同规模企业的广泛采用,如何确保 Kubernetes 集群的高效稳定运行变得至关重要。本文将探讨一套系统的 Kubernetes 集群监控方法,并结合实践经验分享针对性能瓶颈的优化策略。通过实时监控、日志分析与定期审计的结合,旨在帮助运维人员快速定位问题并提出解决方案,从而提升系统的整体表现。
|
2天前
|
Kubernetes Java API
Kubernetes详解(三)——Kubernetes集群组件
Kubernetes详解(三)——Kubernetes集群组件
13 1