关于etcd的安装请参考etcd第一篇 本篇主要讲解etcdctl命令使用 查看集群状态 etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status 我们看到node2成为leader 查看集群成员 etcdctl --endpoints=$ENDPOINTS member list 删除成员: MEMBER_ID=fa6333c794b010d8 etcdctl --endpoints=${HOST_1}:2379,${HOST_2}:2379,${HOST_3}:2379
member remove ${MEMBER_ID} 我们看到如下结果 同时node3的etcd已经停止工作 添加成员: 注意:添加已经删除的需要将node3的data.etcd必须删除 这里以添加node3为例 在节点上执行如下命令: NAME_1=node1 NAME_2=node2 NAME_3=node3 HOST_1=172.16.80.201 HOST_2=172.16.80.202 HOST_3=172.16.80.203 etcdctl --endpoints=${HOST_1}:2379,${HOST_2}:2379 member add ${NAME_3} --peer-urls=http://${HOST_3}:2380 启动新添加的节点,这里使用--initial-cluster-state existing NAME_1=node1 NAME_2=node2 NAME_3=node3 HOST_1=172.16.80.201 HOST_2=172.16.80.202 HOST_3=172.16.80.203 TOKEN=token-01 CLUSTER_STATE=existing CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380 THIS_NAME=${NAME_3} THIS_IP=${HOST_3} etcd --data-dir=data.etcd --name ${THIS_NAME} \

--initial-advertise-peer-urls http://${THIS_IP}:2380
--listen-peer-urls http://${THIS_IP}:2380
--advertise-client-urls http://${THIS_IP}:2379
--listen-client-urls http://${THIS_IP}:2379
--initial-cluster ${CLUSTER}
--initial-cluster-state ${CLUSTER_STATE}
--initial-cluster-token ${TOKEN} 我们做一个测试,是否主节点写入或删除,其余两个节点也会写入或删除,结果如下(表示正确) [root@node2 ~]# etcdctl --endpoints=${HOST_1}:2379,${HOST_2}:2379,${HOST_3}:2379 put foo "a"
OK [root@node2 ~]# etcdctl --endpoints=${HOST_1}:2379,${HOST_2}:2379,${HOST_3}:2379 get foo foo a

[root@node3 ~]# etcdctl --endpoints=${HOST_1}:2379,${HOST_2}:2379,${HOST_3}:2379 get foo foo a

[root@node2 ~]# etcdctl --endpoints=${HOST_1}:2379,${HOST_2}:2379,${HOST_3}:2379 del foo 1 创建snapshot etcdctl --endpoints=$ENDPOINTS snapshot save my.db etcdctl --write-out=table --endpoints=$ENDPOINTS snapshot status my.db 数据迁移:(一般不用,因为etcd是集群,可以添加节点的方式来实现数据迁移,然后删除原有的节点) etcdctl --endpoints=$ENDPOINT migrate --data-dir="default.etcd" --wal-dir="default.etcd/member/wal" 权限 etcdctl --endpoints=${ENDPOINTS} role add root etcdctl --endpoints=${ENDPOINTS} role grant-permission root readwrite foo etcdctl --endpoints=${ENDPOINTS} role get root

etcdctl --endpoints=${ENDPOINTS} user add root etcdctl --endpoints=${ENDPOINTS} user grant-role root root etcdctl --endpoints=${ENDPOINTS} user get root

etcdctl --endpoints=${ENDPOINTS} auth enable #now all client requests go through auth

etcdctl --endpoints=${ENDPOINTS} --user=root:123 put foo bar etcdctl --endpoints=${ENDPOINTS} get foo etcdctl --endpoints=${ENDPOINTS} --user=root:123 get foo etcdctl --endpoints=${ENDPOINTS} --user=root:123 get foo1

参考文章:https://coreos.com/etcd/docs/latest