我是靠谱客的博主 瘦瘦诺言,这篇文章主要介绍ETCD官网文档系列(二)Demo,现在分享给大家,希望可以做个参考。

Demo

创建一个集群

THIS_IP=172.20.134.175
THIS_NAME1=etcd-0
THIS_NAME2=etcd-1
THIS_NAME3=etcd-2
CLUSTER_STATE=new
TOKEN=token-01
CLUSTER=${THIS_NAME1}=http://${THIS_IP}:2380,${THIS_NAME2}=http://${THIS_IP}:2381,${THIS_NAME3}=http://${THIS_IP}:2382
# etcd-0
nohup etcd --data-dir=data.etcd1 --name ${THIS_NAME1} --initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://${THIS_IP}:2380 --advertise-client-urls http://${THIS_IP}:2377 --listen-client-urls http://${THIS_IP}:2377 --initial-cluster ${CLUSTER} --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN} &
# etcd-1
nohup etcd --data-dir=data.etcd2 --name ${THIS_NAME2} --initial-advertise-peer-urls http://${THIS_IP}:2381 --listen-peer-urls http://${THIS_IP}:2381 --advertise-client-urls http://${THIS_IP}:2378 --listen-client-urls http://${THIS_IP}:2378 --initial-cluster ${CLUSTER} --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN} &
# etcd-2
nohup etcd --data-dir=data.etcd3 --name ${THIS_NAME3} --initial-advertise-peer-urls http://${THIS_IP}:2382 --listen-peer-urls http://${THIS_IP}:2382 --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} &

设置和获取key

etcdctl --endpoints=$ENDPOINTS put foo "Hello World!"
etcdctl --endpoints=$ENDPOINTS get foo
etcdctl --endpoints=$ENDPOINTS --write-out="json" get foo

按照key前缀 获取

etcdctl --endpoints=$ENDPOINTS put web1 value1
etcdctl --endpoints=$ENDPOINTS put web2 value2
etcdctl --endpoints=$ENDPOINTS put web3 value3
etcdctl --endpoints=$ENDPOINTS get web --prefix

删除key

etcdctl --endpoints=$ENDPOINTS put key myvalue
etcdctl --endpoints=$ENDPOINTS del key
etcdctl --endpoints=$ENDPOINTS put k1 value1
etcdctl --endpoints=$ENDPOINTS put k2 value2
etcdctl --endpoints=$ENDPOINTS del k --prefix

事务写

etcdctl --endpoints=$ENDPOINTS put user1 bad
etcdctl --endpoints=$ENDPOINTS txn --interactive
compares:
value("user1") = "bad"
success requests (get, put, delete):
del user1
failure requests (get, put, delete):
put user1 good

watch

watch to get notified of future changes:

etcdctl --endpoints=$ENDPOINTS watch stock1
etcdctl --endpoints=$ENDPOINTS put stock1 1000
etcdctl --endpoints=$ENDPOINTS watch stock --prefix
etcdctl --endpoints=$ENDPOINTS put stock1 10
etcdctl --endpoints=$ENDPOINTS put stock2 20

lease 租约

lease to write with TTL

etcdctl --endpoints=$ENDPOINTS lease grant 300
# lease 2be7547fbc6a5afa granted with TTL(300s)
etcdctl --endpoints=$ENDPOINTS put sample value --lease=2be7547fbc6a5afa
etcdctl --endpoints=$ENDPOINTS get sample
etcdctl --endpoints=$ENDPOINTS lease keep-alive 2be7547fbc6a5afa
etcdctl --endpoints=$ENDPOINTS lease revoke 2be7547fbc6a5afa
# or after 300 seconds
etcdctl --endpoints=$ENDPOINTS get sample

分布式锁

etcdctl --endpoints=$ENDPOINTS lock mutex1
# another client with the same name blocks
etcdctl --endpoints=$ENDPOINTS lock mutex1

Elections 选举

elect for leader election:

etcdctl --endpoints=$ENDPOINTS elect one p1
# another client with the same name blocks
etcdctl --endpoints=$ENDPOINTS elect one p2

集群状态

etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status
+---------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
|
ENDPOINT
|
ID
| VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+---------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| 172.20.134.175:2377 | 2667b8786f60ba45 |
3.5.0 |
25 kB |
true |
false |
2 |
99 |
99 |
|
| 172.20.134.175:2378 | afb64943e96342a9 |
3.5.0 |
25 kB |
false |
false |
2 |
99 |
99 |
|
| 172.20.134.175:2379 | adba38c86ecd56f5 |
3.5.0 |
33 kB |
false |
false |
2 |
99 |
99 |
|
+---------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
etcdctl --endpoints=$ENDPOINTS endpoint health -w table
+---------------------+--------+----------+-------+
|
ENDPOINT
| HEALTH |
TOOK
| ERROR |
+---------------------+--------+----------+-------+
| 172.20.134.175:2377 |
true | 3.1436ms |
|
| 172.20.134.175:2379 |
true | 3.3374ms |
|
| 172.20.134.175:2378 |
true |
3.07ms |
|
+---------------------+--------+----------+-------+

Snapshot

snapshot to save point-in-time snapshot of etcd database

snapshot只能送一个etcd节点请求,因此--endpoints 标志应该只包含一个endpoint

ENDPOINTS=$HOST_1:2379
etcdctl --endpoints=$ENDPOINTS snapshot save my.db
Snapshot saved at my.db
etcdctl --write-out=table --endpoints=$ENDPOINTS snapshot status my.db
+---------+----------+------------+------------+
|
HASH
| REVISION | TOTAL KEYS | TOTAL SIZE |
+---------+----------+------------+------------+
| c55e8b8 |
9 |
13 | 25 kB
|
+---------+----------+------------+------------+

Migrate V2->V3

migrate从V2将数据迁移到V3

# write key in etcd version 2 store
export ETCDCTL_API=2
etcdctl --endpoints=http://$ENDPOINT set foo bar
# read key in etcd v2
etcdctl --endpoints=$ENDPOINTS --output="json" get foo
# stop etcd node to migrate, one by one
# migrate v2 data
export ETCDCTL_API=3
etcdctl --endpoints=$ENDPOINT migrate --data-dir="default.etcd" --wal-dir="default.etcd/member/wal"
# restart etcd node after migrate, one by one
# confirm that the key got migrated
etcdctl --endpoints=$ENDPOINTS get /foo

Member

创建一个集群

# For each machine
TOKEN=my-etcd-token-1
CLUSTER_STATE=new
NAME_1=etcd-node-1
NAME_2=etcd-node-2
NAME_3=etcd-node-3
HOST_1=10.240.0.13
HOST_2=10.240.0.14
HOST_3=10.240.0.15
CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380
# For node 1
THIS_NAME=${NAME_1}
THIS_IP=${HOST_1}
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}
# For node 2
THIS_NAME=${NAME_2}
THIS_IP=${HOST_2}
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}
# For node 3
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}

member removemember add

# get member ID
export ETCDCTL_API=3
HOST_1=10.240.0.13
HOST_2=10.240.0.14
HOST_3=10.240.0.15
etcdctl --endpoints=${HOST_1}:2379,${HOST_2}:2379,${HOST_3}:2379 member list
# remove the member
MEMBER_ID=278c654c9a6dfd3b
etcdctl --endpoints=${HOST_1}:2379,${HOST_2}:2379,${HOST_3}:2379 
member remove ${MEMBER_ID}
# add a new member (node 4)
export ETCDCTL_API=3
NAME_1=etcd-node-1
NAME_2=etcd-node-2
NAME_4=etcd-node-4
HOST_1=10.240.0.13
HOST_2=10.240.0.14
HOST_4=10.240.0.16 # new member
etcdctl --endpoints=${HOST_1}:2379,${HOST_2}:2379 
member add ${NAME_4} 
--peer-urls=http://${HOST_4}:2380

启动新成员 并设置标志--initial-cluster-state existing

# 如果新的member是在同一个disk space 上启动,请确保删除 老member的data directory
# restart with 'existing' flag
TOKEN=my-etcd-token-1
CLUSTER_STATE=existing
NAME_1=etcd-node-1
NAME_2=etcd-node-2
NAME_4=etcd-node-4
HOST_1=10.240.0.13
HOST_2=10.240.0.14
HOST_4=10.240.0.16 # new member
CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_4}=http://${HOST_4}:2380
THIS_NAME=${NAME_4}
THIS_IP=${HOST_4}
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}

Auth

export ETCDCTL_API=3
ENDPOINTS=localhost:2379
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

最后

以上就是瘦瘦诺言最近收集整理的关于ETCD官网文档系列(二)Demo的全部内容,更多相关ETCD官网文档系列(二)Demo内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部