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


原创粉丝点击