etcdv3的操作脚本

 etcd集群操作有时比较麻烦,目前写成脚本来执行。

不过还不是很灵活,需要进一步完善。


#!/bin/bash

##############定义变量#################
#全局配置信息
TOKEN=etcd-kdcloud
DATA_DIR=/opt/etcdv3/data.etcd
LOG_FILE=/opt/etcdv3/run.log
CLUSTER=kdcloud1=http://10.10.10.9:2380,kdcloud2=http://10.10.10.25:2380,kdcloud3=http://10.10.10.26:2380
ENDPOINTS=http://10.10.10.9:2379,http://10.10.10.25:2379,http://10.10.10.26:2379


#当前节点的监听IP,修改为THIS_NAME对应的IP
THIS_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}:2380
	
	if [ $? -ne 0 ]; then
        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 --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}/snapshot
	ts=` date "+%Y-%m-%d_%H:%M:%S"`
	etcdctl snapshot save ${DATA_DIR}/snapshot/${ts}.db
}


restore(){
	db=$1
	if [ "" = "$db" ]; then
		usage
		return 0
	fi
	
	if [ ! -f $db ]; then
		echo "snapshot db file not exist"
		return 1
	fi
	
	
	etcdctl snapshot restore ${db}  --data-dir=${DATA_DIR} --name ${THIS_NAME} --initial-cluster ${CLUSTER} --initial-cluster-token ${TOKEN} --initial-advertise-peer-urls http://${THIS_IP}:2380
	
	if [ $? -ne 0 ]; then
		echo "snapshot restore ${db} fail"
        return 1
    fi
	
	start
	
	if [ $? -ne 0 ]; then            
        return 1
    fi
	
	return 0
}
	

#==========================================
#执行命令
#==========================================
result=0
if [ "$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
        status
elif [ "$1" == "list" ]; then
        list
elif [ "$1" == "snapshot" ]; then
	snapshot
	result=$?
elif [ "$1" == "restore" ]; then
	restore $2
	result=$?
elif [ "$1" == "restart" ]; then
        stop
        result=$?
        if [ $result -ne 0 ]; then
                exit 1
        fi
        start
        result=$?
else
        usage
fi

if [ $result -ne 0 ]; then
        echo "$1 fail"
else
        echo "$1 success"
fi

exit $result


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值