爱死你了 发表于 2019-1-31 11:53:17

etcd第二篇etcdctl详解

  关于etcd的安装请参考etcd第一篇
本篇主要讲解etcdctl命令使用
查看集群状态
etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status
http://i2.运维网.com/images/blog/201808/16/08fedda64bda34bc88e79bcaa621c31c.png
我们看到node2成为leader
查看集群成员
etcdctl --endpoints=$ENDPOINTS member list
http://i2.运维网.com/images/blog/201808/16/772b3fc8fde003b7818d72dafac1f8a5.png
删除成员:
MEMBER_ID=fa6333c794b010d8
etcdctl --endpoints=${HOST_1}:2379,${HOST_2}:2379,${HOST_3}:2379 \
member remove ${MEMBER_ID}
我们看到如下结果
http://i2.运维网.com/images/blog/201808/16/005a48a71078093861d464ed252c39fc.png
同时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}
我们做一个测试,是否主节点写入或删除,其余两个节点也会写入或删除,结果如下(表示正确)
# etcdctl --endpoints=${HOST_1}:2379,${HOST_2}:2379,${HOST_3}:2379 put foo "a"      
OK
# etcdctl --endpoints=${HOST_1}:2379,${HOST_2}:2379,${HOST_3}:2379 get foo
foo
a
  # etcdctl --endpoints=${HOST_1}:2379,${HOST_2}:2379,${HOST_3}:2379 get foo
foo
a
  # etcdctl --endpoints=${HOST_1}:2379,${HOST_2}:2379,${HOST_3}:2379 del foo
1
http://i2.运维网.com/images/blog/201808/16/efb648f22aa1a92045af26475b663fc3.png
创建snapshot
etcdctl --endpoints=$ENDPOINTS snapshot save my.db
etcdctl --write-out=table --endpoints=$ENDPOINTS snapshot status my.db
http://i2.运维网.com/images/blog/201808/16/12df8762e0f0096b293c98747799be1d.png
数据迁移:(一般不用,因为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



页: [1]
查看完整版本: etcd第二篇etcdctl详解