#! /bin/bash export ETCDCTL_API=3 #-----------------以下部分涉及etcd数据备份,需要修改相关变量参数--------- #etcd备份数据的目录 etcdpwd="/apps/etcd_bak" #备份数据的时间戳 bak_date=`date +"%Y-%m-%d"` #获取etcd集群中拥有leader的etcd ip,对应的etcd集群ip根据自己实际环境进行修改 etcdip=`etcdctl --endpoints=https://192.168.133.128:1159,https://192.168.133.129:1159,https://192.168.133.130:1159 --cacert="/etc/kubernetes/ssl/ca.crt" --cert="/etc/kubernetes/ssl/etcd_server.crt" --key="/etc/kubernetes/ssl/etcd_server.key" endpoint status | grep true | awk -F "," '{print $1}'` #-----------------以下部分涉及etcd数据的恢复,需要修改相关变量参数---------- #etcd备份需要恢复的数据 etcd_bak_db="$etcdpwd/etcd_2024-04-22.db" #etcd数据恢复的数据目录 etcd_data="/data/etcd_data/etcd" #etcd集群的ip地址,根据自己实际环境填写 etcd_cluster="https://192.168.133.128:1159,https://192.168.133.129:1159,https://192.168.133.130:1159" #获取etcd的name名称 etcd_name=`cat /etc/etcd/etcd.conf | grep ETCD_NAME | awk -F"=" '{print $2}' | awk -F"[\"]" '{print $2}'` #获取etcd的token cluster_token=`cat /etc/etcd/etcd.conf | grep ETCD_INITIAL_CLUSTER_TOKEN | awk -F "[\"]" '{print $2}'` #获取etcd的peer peer_urls=`cat /etc/etcd/etcd.conf | grep ETCD_INITIAL_ADVERTISE_PEER_URLS |awk -F "[\"]" '{print $2}'` #对etcd的leader进行数据备份 bak(){ etcdctl --endpoints=$etcdip --cacert="/etc/kubernetes/ssl/ca.crt" --cert="/etc/kubernetes/ssl/etcd_server.crt" --key="/etc/kubernetes/ssl/etcd_server.key" snapshot save $etcdpwd/etcd_$bak_date.db >>/dev/null 2>&1 if [ $? -eq 0 ] then echo "etcd备份完成" else echo "etcd备份失败,请检查etcd是否正常!" fi } #对etcd进行数据恢复 res(){ systemctl stop etcd sleep 10s rm -rf $etcd_data/* #此处--initial-cluster部分的etcd ip和端口需要根据实际环境进行替换 etcdctl --endpoints=$etcdip --name $etcd_name --initial-cluster "etcd_133_128=https://192.168.133.128:2380,etcd_133_129=https://192.168.133.129:2380,etcd_133_130=https://192.168.133.130:2380" --initial-cluster-token $cluster_token \ --initial-advertise-peer-urls $peer_urls --cacert="/etc/kubernetes/ssl/ca.crt" --cert="/etc/kubernetes/ssl/etcd_server.crt" --key="/etc/kubernetes/ssl/etcd_server.key" --data-dir=$etcd_data snapshot restore $etcd_bak_db if [ $? -eq 0 ] then echo "etcd恢复完成" else echo "etcd恢复失败,请检查etcd是否正常!" fi systemctl daemon-reload systemctl restart etcd if [ $? -eq 0 ] then echo "etcd启动成功" else echo "etcd启动失败,请检查etcd是否正常!" fi } #对etcd进行碎片整理 etcd_ip=`cat /etc/etcd/etcd.conf | grep ETCD_LISTEN_CLIENT_URLS | awk -F ',' '{print $1}' | awk -F '"' '{print $2}'` #获取etcd当前版本 rev=`etcdctl --endpoints=$etcd_ip --cacert="/etc/kubernetes/ssl/ca.crt" --cert="/etc/kubernetes/ssl/etcd_server.crt" --key="/etc/kubernetes/ssl/etcd_server.key" endpoint status --write-out="json" | egrep -o '"revision":[0-9]' | egrep -o '[0-9]'` defrag(){ etcdctl --endpoints=$etcd_ip --cacert="/etc/kubernetes/ssl/ca.crt" --cert="/etc/kubernetes/ssl/etcd_server.crt" --key="/etc/kubernetes/ssl/etcd_server.key" compact $rev etcdctl --endpoints=$etcd_ip --cacert="/etc/kubernetes/ssl/ca.crt" --cert="/etc/kubernetes/ssl/etcd_server.crt" --key="/etc/kubernetes/ssl/etcd_server.key" defrag etcdctl --endpoints=$etcd_ip --cacert="/etc/kubernetes/ssl/ca.crt" --cert="/etc/kubernetes/ssl/etcd_server.crt" --key="/etc/kubernetes/ssl/etcd_server.key" alarm disarm } #查看etcd集群状态 status(){ etcdctl --endpoints=$etcd_cluster --cacert="/etc/kubernetes/ssl/ca.crt" --cert="/etc/kubernetes/ssl/etcd_server.crt" --key="/etc/kubernetes/ssl/etcd_server.key" endpoint status -w table } echo "**********************************" echo "** **" echo "** << 功能选择 >> **" echo "** **" echo "** 1: 表示对etcd的数据备份 **" echo "** **" echo "** 2: 表示对etcd的数据恢复 **" echo "** **" echo "** 3: 表示etcd的碎片整理 **" echo "** **" echo "** 4: 表示etcd的状态查询 **" echo "**********************************" read -p "请输入你选择的功能:" key case "$key" in 1) bak ;; 2) res ;; 3) defrag ;; 4) status ;; *) esac
etcd多用途使用脚本
最新推荐文章于 2024-08-30 15:03:12 发布