本文介绍如何在etcd v3添加权限和用户,是防裸奔的神器。
启动服务
nohup etcd --name s1 --data-dir /export/Data/etcd --listen-client-urls http://0.0.0.0:2379 --advertise-client-urls http://0.0.0.0:2379 --listen-peer-urls http://0.0.0.0:2380 --initial-advertise-peer-urls http://0.0.0.0:2380 --initial-cluster s1=http://0.0.0.0:2380 &
创建root用户
etcdctl --endpoints=http://127.0.0.1:2379 user add root
# etcd 3.3.18 使用之前的语句授权报错:
{"level":"warn","ts":"2020-01-19T15:58:16.731+0800","caller":"clientv3/retry_interceptor.go:61","msg":"retrying of unary invoker failed","target":"endpoint://client-8eabadf1-ffce-4fb6-b190-2e435d3efeab/127.0.0.1:2379","attempt":0,"error":"rpc error: code = FailedPrecondition desc = etcdserver: authentication is not enabled"}
# 使用下面语句授权之后正常
etcdctl --user root user add root
etcdctl --user root user grant-role root root
添加root用户的时候会要求指定密码,这里我们指定123456
创建普通用户
etcdctl --endpoints=http://127.0.0.1:2379 --user=root:123456 user add yunzhonghe
添加角色
etcdctl --endpoints http://127.0.0.1:2379 --user=root:123456 role add normal
角色授权
etcdctl --endpoints http://127.0.0.1:2379 --user=root:123456 role grant-permission --prefix=true normal readwrite /path_name
用户绑定角色
etcdctl --endpoints http://127.0.0.1:2379 --user=root:123456 user grant-role yunzhonghe normal
验证
$ etcdctl --endpoints=http://127.0.0.1:2379 --user=yunzhonghe:xuzhimo put /ha 'haha'
OK
$ etcdctl --endpoints=http://127.0.0.1:2379 --user=yunzhonghe:xuzhimo get /ha
/ha
haha
至此,用户添加完毕,可以使用带权限的用户访问啦!