【镜像制作】制作k8s的yaml配置的备份镜像

文章目录

简介

通过shell脚本写一个定期备份k8syaml配置的镜像,然后通过cronjob来实现定期执行,将备份文件上传到ceph存储或者是存放到NAS存储上。

一.备份代码

通过kubectl导出yaml配置文件,然后进行压缩并上传到ceph存储。

bash 复制代码
#!/bin/bash

bucket="yt-k8s-yamlbak"
key_access="864XMJ0U9BDFBUI8OOxxxx"
key_secret="bNzAXe8DzGGshi23aKoykfYiy2XeFR1U2Oxxxxx"
host="192.168.12.158:8000" #endpoint

Root="/data/yaml"
clusterName="yt-k8s"
nameSpaces=$(kubectl get ns|grep -v "NAME"|awk '{print $1}'|xargs)
types="deploy service cronjob ing cm secret serviceAccount sts pvc hpa"

#判断是否存在bucket,不存在就创建bucket
scode=$(curl -o /dev/null --max-time 60 --connect-timeout 60 -w "%{http_code}" http://$host/$bucket/ --silent)
if [ $scode == "404" ];then
        acl="x-amz-acl:public-read"
        relativePath="/${bucket}"
        current=`TZ=GMT LANG=en_US date "+%a, %d %b %Y %H:%M:%S GMT"`
        stringToSign="PUT\n\n\n${current}\n${acl}\n${relativePath}"
        signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${key_secret} -binary | base64`
        curl -A "CreateBucket" -s -v -X PUT --max-time 60 --connect-timeout 60 "http://${host}${relativePath}" -H "Authorization: AWS ${key_access}:${signature}" -H "Date: ${current}" -H "Host: ${host}" -H "${acl}"
        if [ $? -ne 0 ];then
                echo "`date +%Y-%m-%dT%H:%M:%S` bucket create fail"
        fi
        echo "`date +%Y-%m-%dT%H:%M:%S` bucket create ok"
fi

#循环命名空间,并将命名空间下的资源导出成yaml配置文件。
for ns in ${nameSpaces[@]}
do
        for tps in ${types[@]}
        do
                mkdir $Root/$clusterName/$ns/$tps -p
                for name in $(kubectl get $tps -n $ns|awk '{print $1}'|grep -v NAME)
                do
                        kubectl get $tps $name -n $ns -o yaml > $Root/$clusterName/$ns/$tps/${name}.yaml
                done
        done
done

#将导出的yaml文件进行压缩成zip文件,然后上传到ceph对象存储
dh=$(date +%Y%m%d%H%M%S)
cd $Root
zip -r ${dh}.zip $clusterName
rm -rf $Root/${clusterName}
dt=$(date +%Y/%m/%d)
BakRoot="/data/$clusterName/$dt"
relativePath="/${bucket}/$dt/${dh}.zip"
contentType="application/x-zip-compressed"
acl="x-amz-acl:public-read"
current=`TZ=GMT LANG=en_US date "+%a, %d %b %Y %H:%M:%S GMT"`
stringToSign="PUT\n\n${contentType}\n${current}\n${acl}\n${relativePath}"
signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${key_secret} -binary | base64`
curl -A "UploadLog" -s -v --max-time 1800 --connect-timeout 60 -X PUT -T "$Root/${dh}.zip" -H "Host: ${host}" -H "Date: ${current}" -H "${acl}" -H "Content-Type: ${contentType}" -H "Authorization: AWS ${key_access}:${signature}" "http://${host}${relativePath}" -v >/tmp/t.log 2>&1
cat /tmp/t.log
cat /tmp/t.log|grep "HTTP/1.1 200 OK"
if [ $? -eq 0 ];then
        echo "`date +%Y-%m-%dT%H:%M:%S` upload $Root/${dh}.zip success"
        [ -d $BakRoot ] || mkdir -p $BakRoot
        mv -fv $Root/${dh}.zip $BakRoot/ 
else
        echo "`date +%Y-%m-%dT%H:%M:%S` upload $Root/${dh}.zip fail"
fi
rm -fv /tmp/t.log

二.dockerfile代码

需要将kubectl和config配置文件拷贝到root下。

bash 复制代码
# VERSION 1 - EDITION 1
# # # Author: wangbikang

FROM alpine:3.18 

MAINTAINER docker_user wangbikang@pconline.com.cn

COPY run.sh /data/
COPY kubectl /bin/
RUN mkdir -p /root/.kube
COPY config /root/.kube/
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
RUN apk add --no-cache curl coreutils  openssl zip bash

CMD ["/data/run.sh"]

三.cronjob配置

cronjob配置,每小时只想能够一次,然后将文件压缩备份一份到nas

yaml 复制代码
apiVersion: batch/v1
kind: CronJob
metadata:
  labels:
    app: yt-k8s-yamlbak 
  name: yt-k8s-yamlbak
  namespace: auto-public 
spec:
  schedule: "2 * * * *"
  suspend: false
  jobTemplate:
    spec:
      activeDeadlineSeconds: 3600
      backoffLimit: 1
      template:
        spec:
          containers:
          - name: task 
            image: pcgroup-registry-vpc.cn-shenzhen.cr.aliyuncs.com/public/k8s-yaml-backup:20240607154517 
            imagePullPolicy: IfNotPresent
            command: ["bash","/data/run.sh"]
            volumeMounts:
            - mountPath: /data/yt-k8s-yamlbak
              name: static-html
              readOnly: false
          restartPolicy: Never
          volumes:
            - name: static-html
              nfs:
                server: 2e347xxx-mfg89.cn-shenzhen.nas.aliyuncs.com
                path: /yt-k8s-yamlbak/
相关推荐
川石课堂软件测试1 小时前
性能测试|docker容器下搭建JMeter+Grafana+Influxdb监控可视化平台
运维·javascript·深度学习·jmeter·docker·容器·grafana
昌sit!7 小时前
K8S node节点没有相应的pod镜像运行故障处理办法
云原生·容器·kubernetes
追风林8 小时前
mac 本地docker-mysql主从复制部署
mysql·macos·docker
A ?Charis10 小时前
Gitlab-runner running on Kubernetes - hostAliases
容器·kubernetes·gitlab
城南vision10 小时前
Docker学习—Docker核心概念总结
java·学习·docker
wclass-zhengge10 小时前
Docker篇(Docker Compose)
运维·docker·容器
北漂IT民工_程序员_ZG11 小时前
k8s集群安装(minikube)
云原生·容器·kubernetes
梦魇梦狸º14 小时前
腾讯轻量云服务器docker拉取不到镜像的问题:拉取超时
docker·容器·github
鬼才血脉14 小时前
docker+mysql配置
mysql·adb·docker
詹姆斯爱研究Java14 小时前
Docker 的基本概念和优势
docker