Pod

#!/bin/bash

# 指定保存目录
SAVE_DIR="./pods_yaml"

# 创建保存目录
mkdir -p $SAVE_DIR

# 获取所有pod名称
PODS=$(kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.metadata.namespace}/{.metadata.name}{"\n"}{end}')

# 循环遍历所有pod,获取yaml文件并保存
for POD in $PODS
do
  NAMESPACE=$(echo $POD | cut -d '/' -f 1)
  NAME=$(echo $POD | cut -d '/' -f 2)
  kubectl get pod $NAME -n $NAMESPACE -o yaml > $SAVE_DIR/$NAMESPACE-$NAME.yaml
done

echo "保存完成!"

All 1

这里是备份Kubernetes各种资源为YAML文件的脚本,分别备份了ConfigMap、DaemonSet、StatefulSet、Deployment等资源:

#!/bin/bash

# Set the backup directory
backup_dir=$(date +%Y%m%d_%H%M%S)
mkdir -p $backup_dir

# Backup ConfigMaps in all namespaces
for ns in $(kubectl get namespaces -o name | cut -d '/' -f 2); do
  mkdir -p $backup_dir/$ns
  kubectl get configmaps -o name -n $ns | xargs -I{} kubectl get {} -n $ns -o yaml > $backup_dir/$ns/configmaps.yaml
done

# Backup DaemonSets in all namespaces
for ns in $(kubectl get namespaces -o name | cut -d '/' -f 2); do
  mkdir -p $backup_dir/$ns
  kubectl get daemonsets -o name -n $ns | xargs -I{} kubectl get {} -n $ns -o yaml > $backup_dir/$ns/daemonsets.yaml
done

# Backup StatefulSets in all namespaces
for ns in $(kubectl get namespaces -o name | cut -d '/' -f 2); do
  mkdir -p $backup_dir/$ns
  kubectl get statefulsets -o name -n $ns | xargs -I{} kubectl get {} -n $ns -o yaml > $backup_dir/$ns/statefulsets.yaml
done

# Backup Deployments in all namespaces
for ns in $(kubectl get namespaces -o name | cut -d '/' -f 2); do
  mkdir -p $backup_dir/$ns
  kubectl get deployments -o name -n $ns | xargs -I{} kubectl get {} -n $ns -o yaml > $backup_dir/$ns/deployments.yaml
done

# Compress the backup directory
tar -czvf $backup_dir.tar.gz $backup_dir

# Delete the backup directory
rm -rf $backup_dir

您可以根据需要修改备份的资源类型,并将其保存到相应的 YAML 文件中。该脚本将逐个备份ConfigMap、DaemonSet、StatefulSet、Deployment等资源,以便您可以更好地控制备份的内容。

All 2

以下是备份Kubernetes所有资源为YAML文件的脚本,包括所有资源类型:

#!/bin/bash

# Set the backup directory
backup_dir=$(date +%Y%m%d_%H%M%S)
mkdir -p $backup_dir

# Backup all resources
kubectl api-resources --verbs=list --namespaced -o name \
  | xargs -n 1 kubectl get --show-kind --ignore-not-found -o yaml \
    --export -n <namespace> > $backup_dir/all-resources.yaml

# Backup ConfigMaps in all namespaces
for ns in $(kubectl get namespaces -o name | cut -d '/' -f 2); do
  mkdir -p $backup_dir/$ns
  kubectl get configmaps -o name -n $ns | xargs -I{} kubectl get {} -n $ns -o yaml > $backup_dir/$ns/configmaps.yaml
done

# Backup Secrets in all namespaces
for ns in $(kubectl get namespaces -o name | cut -d '/' -f 2); do
  mkdir -p $backup_dir/$ns
  kubectl get secrets -o name -n $ns | xargs -I{} kubectl get {} -n $ns -o yaml > $backup_dir/$ns/secrets.yaml
done

# Backup CronJobs in all namespaces
for ns in $(kubectl get namespaces -o name | cut -d '/' -f 2); do
  mkdir -p $backup_dir/$ns
  kubectl get cronjobs -o name -n $ns | xargs -I{} kubectl get {} -n $ns -o yaml > $backup_dir/$ns/cronjobs.yaml
done

# Compress the backup directory
tar -czvf $backup_dir.tar.gz $backup_dir

# Delete the backup directory
rm -rf $backup_dir

该脚本将备份所有资源为一个 YAML 文件,并将 ConfigMap、Secret、CronJob 分别备份到它们各自的 YAML 文件中。您可以根据需要选择备份的资源类型,并将其保存到相应的 YAML 文件中。最后,脚本将压缩备份目录并删除原始目录。

Logo

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

更多推荐