摘要
本文内容取自 官方演示demo。
在此基础上添加了部分个人理解的内容。
etcd安装部署请移步 etcd-cluster 集群部署
本文内容目前为api3的操作
配置客户端环境变量
1 | export ETCDCTL_API=3 |
Access etcd
put command to write:
1 | etcdctl --endpoints=$ENDPOINTS put foo "Hello World!" |
get to read from etcd:
1 | etcdctl --endpoints=$ENDPOINTS get foo |
Get by prefix
1 | etcdctl --endpoints=$ENDPOINTS put web1 value1 |
Delete
1 | etcdctl --endpoints=$ENDPOINTS put key myvalue |
Transactional write
txn to wrap multiple requests into one transaction:
1 | etcdctl --endpoints=$ENDPOINTS put user1 bad |
Watch
watch to get notified of future changes:
1 | etcdctl --endpoints=$ENDPOINTS watch stock1 |
Lease
lease to write with TTL:
1 | etcdctl --endpoints=$ENDPOINTS lease grant 300 |
Distributed locks
lock for distributed lock:
1 | etcdctl --endpoints=$ENDPOINTS lock mutex1 |
Elections
elect for leader election:
1 | etcdctl --endpoints=$ENDPOINTS elect one p1 |
Cluster status
Specify the initial cluster configuration for each machine:
1 | etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status |
1 | etcdctl --endpoints=$ENDPOINTS endpoint health |
Snapshot
snapshot to save point-in-time snapshot of etcd database:
1 | etcdctl --endpoints=$ENDPOINTS snapshot save my.db |
1 | etcdctl --write-out=table --endpoints=$ENDPOINTS snapshot status my.db |
Migrate
migrate to transform etcd v2 to v3 data:
1 | # write key in etcd version 2 store |
Member
member to add,remove,update membership:
1 | # For each machine |
Then replace a member with member remove and member add commands:
1 | # get member ID |
Next, start the new member with –initial-cluster-state existing flag:
1 | # [WARNING] If the new member starts from the same disk space, |
Auth
auth,user,role for authentication:
1 | export ETCDCTL_API=3 |