目录

Etcd

Etcd

1 基本操作

API 版本: export ETCDCTL_API=3

1.1 查询

etcdctl get foo

1.2 写入

$ etcdctl put foo bar
OK

1.3 删除

$ etcdctl del foo
1 # 删除了一个key

1.4 更新

put

1.5 查看集群信息

etcdctl member list -w table

2 用户权限

2.1 创建root用户

etcdctl --endpoints=http://127.0.0.1:2379 user add root
Password of root:
Type password of root again for confirmation:
User root created

2.2 创建普通用户

etcdctl --endpoints=http://127.0.0.1:2379 --user=root:123456 user add asura
Password of asura:
Type password of asura again for confirmation:
User asura created

2.3 创建role

[root@~]# etcdctl --endpoints http://127.0.0.1:2379 --user=root:123456 role add normal
Role normal created

2.4 role 受权

[root@~]#etcdctl --endpoints http://127.0.0.1:2379 --user=root:123456 role grant-permission --prefix=true normal readwrite /_test
Role normal updated

2.5 用户绑定角色

[root@~]# etcdctl --endpoints http://127.0.0.1:2379 --user=root:123456 user grant-role asura normal
Role normal is granted to user asura

2.6 开启验证

[root@~]# etcdctl auth enable
Authentication Enabled
etcdctl get /_test
Error: etcdserver: user name is empty

etcdctl --endpoints=http://127.0.0.1:2379 --user=asura:123456 get /_test

3 历史版本

3.1 压缩修订版本

保留 1 个小时的历史版本:
etcd --auto-compaction-retention=1 
保留五个版本
$ etcdctl compact 5

4 租约

4.1 授予租约

# 授予租约,TTL为10秒
$ etcdctl lease grant 10
lease 32695410dcc0ca06 granted with TTL(10s)

# 附加key foo到租约32695410dcc0ca06
$ etcdctl put --lease=32695410dcc0ca06 foo bar
OK

4.2 撤销租约

$ etcdctl lease revoke 32695410dcc0ca06
lease 32695410dcc0ca06 revoked

$ etcdctl get foo
# 空应答,因为租约撤销导致foo被删除

5 碎片

使用 etcdctl 命令行的 defrag 子命令可以清理 etcd 节点的存储碎片,示例代码如下:
etcdctl defrag 

6 备份

增量同步/异地备份
etcdctl make-mirror http://IP:2379 

snapshot备份
 etcdctl --endpoints localhost:2379 snapshot save snapshot.db

snapshot恢复
etcdctl snapshot restore snapshot.db --name m3 --data-dir=/home/etcd_data

7 etcd web

7.1 etcdkeeper

https://github.com/evildecay/etcdkeeper/releases
etcdkeeper-v0.7.6-linux_x86_64.zip


vim start.sh
HOST="0.0.0.0"
PORT="9000"
./etcdkeeper -h $HOST -p $PORT -auth=true &> /dev/null&

sh start.sh
打开网页: http://x.x.x.x:9000/etcdkeeper/
输入用户名密码

-w361