etcdv3的操作脚本
来源:互联网 发布:php 打印数组内容 编辑:程序博客网 时间:2024/06/14 16:39
etcd集群操作有时比较麻烦,目前写成脚本来执行。
不过还不是很灵活,需要进一步完善。
#!/bin/bash##############定义变量##################全局配置信息TOKEN=etcd-kdcloudDATA_DIR=/opt/etcdv3/data.etcdLOG_FILE=/opt/etcdv3/run.logCLUSTER=kdcloud1=http://10.10.10.9:2380,kdcloud2=http://10.10.10.25:2380,kdcloud3=http://10.10.10.26:2380ENDPOINTS=http://10.10.10.9:2379,http://10.10.10.25:2379,http://10.10.10.26:2379#当前节点的监听IP,修改为THIS_NAME对应的IPTHIS_IP=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|grep -v 172.|awk '{print $2}'|tr -d "addr:"`#当前节点名称,需要修改为实际值THIS_NAME=kdcloud1########################################显示使用方法usage(){ echo ""echo "version etcd > 3.2.0" echo "usage:" echo "" echo $0" new" echo " create and run new etcd cluster" echo "" echo $0" start" echo " when etcd cluster is not running, start the cluster" echo "" echo $0" stop" echo " stop the etcd cluster" echo "" echo $0" restart" echo " stop and start the etcd cluster" echo "" echo $0" add" echo " add a new member to existing etcd cluster. maybe you shoud remove the old member which is crash" echo "" echo $0" snapshot" echo " snapshot etcd cluster for backup or restore, save to path DATA_DIR/snapshot/{timestamp}.db" echo "" echo $0" restore [snapshot db path]" echo " restore etcd cluster from snapshot db" echo "" echo $0" status" echo " for command: etcdctl --write-out=table --endpoints=${ENDPOINTS} endpoint status" echo "" echo $0" list" echo " for command: etcdctl member list" echo ""}status(){etcdctl --write-out=table --endpoints=${ENDPOINTS} endpoint status}list(){etcdctl member list}new(){ #先判断data-dir是否存在,如果存在将不能运行,防止把当前运行的集群信息给覆盖了 if [ -e ${DATA_DIR} ]; then echo "New etcd cluster fail. because etcd data dir exist, cann't new and run a new etcd cluster." return 1 fi nohup etcd --data-dir=${DATA_DIR} --name ${THIS_NAME} \ --initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://0.0.0.0:2380 \ --advertise-client-urls http://${THIS_IP}:2379 --listen-client-urls http://0.0.0.0:2379 \ --initial-cluster ${CLUSTER} --auto-compaction-retention=1 \ --initial-cluster-token ${TOKEN} --initial-cluster-state new > ${LOG_FILE} 2>&1 & if [ $? -ne 0 ]; then return 1 fi return 0}start(){ ID=`ps -ef|grep "name ${THIS_NAME}"| grep -v "grep" | awk '{print $2}'` if [ "" = "$ID" ]; then echo "start etcd member,name=${THIS_NAME}" else echo "etcd member is running now,pid=$ID,name=${THIS_NAME}" return 1 fi nohup etcd --data-dir=${DATA_DIR} --name ${THIS_NAME} \--listen-peer-urls http://0.0.0.0:2380 --listen-client-urls http://0.0.0.0:2379 \--advertise-client-urls http://${THIS_IP}:2379 --auto-compaction-retention=1 > ${LOG_FILE} 2>&1 & if [ $? -ne 0 ]; then return 1 fi return 0}stop(){ ID=`ps -ef|grep "name ${THIS_NAME}"| grep -v "grep" | awk '{print $2}'` if [ "" = "$ID" ]; then echo "etcd member not run,name=${THIS_NAME}" return 1 fi kill -9 $ID if [ $? -ne 0 ] ;then echo "kill etcd member fail,pid=$pid,name=${THIS_NAME}" return 1 fi return 0}add(){etcdctl --endpoints=${ENDPOINTS} member add ${THIS_NAME} --peer-urls=http://${THIS_IP}:2380if [ $? -ne 0 ]; then return 1 finohup etcd --data-dir=${DATA_DIR} --name ${THIS_NAME} \--initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://0.0.0.0:2380 --listen-client-urls http://0.0.0.0:2379 \--advertise-client-urls http://${THIS_IP}:2379 --initial-cluster-token ${TOKEN} --auto-compaction-retention=1 \--initial-cluster ${CLUSTER} --initial-cluster-state existing > ${LOG_FILE} 2>&1 &if [ $? -ne 0 ]; then return 1 fi return 0}snapshot(){mkdir -p ${DATA_DIR}/snapshotts=` date "+%Y-%m-%d_%H:%M:%S"`etcdctl snapshot save ${DATA_DIR}/snapshot/${ts}.db}restore(){db=$1if [ "" = "$db" ]; thenusagereturn 0fiif [ ! -f $db ]; thenecho "snapshot db file not exist"return 1fietcdctl snapshot restore ${db} --data-dir=${DATA_DIR} --name ${THIS_NAME} --initial-cluster ${CLUSTER} --initial-cluster-token ${TOKEN} --initial-advertise-peer-urls http://${THIS_IP}:2380if [ $? -ne 0 ]; thenecho "snapshot restore ${db} fail" return 1 fistartif [ $? -ne 0 ]; then return 1 fireturn 0}#==========================================#执行命令#==========================================result=0if [ "$1" == "new" ]; then new result=$?elif [ "$1" == "start" ]; then start result=$?elif [ "$1" == "stop" ]; then stop result=$?elif [ "$1" == "add" ]; then add result=$?elif [ "$1" == "status" ]; then statuselif [ "$1" == "list" ]; then listelif [ "$1" == "snapshot" ]; thensnapshotresult=$?elif [ "$1" == "restore" ]; thenrestore $2result=$?elif [ "$1" == "restart" ]; then stop result=$? if [ $result -ne 0 ]; then exit 1 fi start result=$?else usagefiif [ $result -ne 0 ]; then echo "$1 fail"else echo "$1 success"fiexit $result
阅读全文
0 0
- etcdv3的操作脚本
- shell脚本的一些操作
- js脚本的日期操作
- 操作记录脚本的实现
- 脚本操作
- 对图片进行操作的脚本
- 通过脚本实现操作的自动化
- VBS脚本操作数据库的例子
- 完整的注册表操作实例 VBS脚本
- perl脚本对execl的操作
- [原创]shell对xml操作的脚本
- Gmail相关的邮件操作脚本
- shell脚本echo的换行操作
- 实验室的一段Python脚本 “备份文件操作”
- java操作脚本的包script简介
- 通过脚本实现指定的Monkey操作
- 查询某张表上所有dml操作的脚本
- Sql2005 K掉死锁的操作脚本
- 第一行代码第二版(郭霖著)笔记之第十一章(百度地图)
- 流程python学习笔记:第二章(1)
- 关于MAVEN项目包冲突问题
- 设置MySQL数据库权限及用户密码
- 用jspSmartUpload组件实现文件上传、下载
- etcdv3的操作脚本
- ListView扩展实现XListView、ZListView
- Express中use挂载中间件的方法
- 万丈高楼平地起,勿在浮沙筑高台--论程序员基础知识的重要性
- spring与mybatis三种整合方法
- HDU 1257 贪心
- K-近邻算法
- linux下mysql的root密码忘记解决方法
- 08 WebGL 着色器编程语言GLSL ES的运算符优先级