image.png

etcd

etcd(读作et-see-dee)是一种开源的分布式统一键值存储,用于分布式系统或计算机集群的共享配置、服务发现和的调度协调etcd 有助于促进更加安全的自动更新,协调向主机调度的工作,并帮助设置容器的覆盖网络。 etcd 是许多其他项目的核心组件。

安装

  • 1、第一种方式就是通过 github 安装,可自行在一下网站下载

Releases · etcd-io/etcd (github.com)

  • 2、 通过命令安装

mac brew install etcd

添加环境变量

安装完成后需要将 etcd 的目录添加到环境变量中,这样就可以调用 etcd 命令操作。

add path

执行以下命令查看是否生效: etcd --version

输出:

etcd --version
etcd Version: 3.5.10
Git SHA: 0223ca52b
Go Version: go1.20.10
Go OS/Arch: darwin/amd64

启动 etcd 服务

./etcd

输出:

image-20231031151027987

基础使用

设置值 etcdctl put

etcdctl put greeting "hello world"

获取值 etcdctl get

etcdctl get greeting

授权 auth

  • 设置环境变量 ETCDCTL_API=3 ENDPOINTS=localhost:2379
export ETCDCTL_API=3
ENDPOINTS=localhost:2379
  • 添加角色: etcd 可以通过添加用户,添加角色,角色授权,将用户添加到角色进行权限管控:
# 添加 root 角色
etcdctl --endpoints=${ENDPOINTS} role add root
  • 获取角色信息
etcdctl --endpoints=${ENDPOINTS} role get root
Role root
KV Read:
KV Write:
  • 添加 root 成员 / 获取成员信息

# 添加成员 root
etcdctl --endpoints=${ENDPOINTS} user add root

# 设置密码
Password of root:
Type password of root again for confirmation:
User root created

# 查看用户信息
etcdctl --endpoints=${ENDPOINTS} user get root
User: root
Roles:
  • 成员授权角色
# 将 root 用户授权给 root 角色
etcdctl --endpoints=${ENDPOINTS} user grant-role root root

# 用户 root 授权 root 成功
Role root is granted to user root
  • 添加新的角色,授权角色的权限
etcdctl --endpoints=${ENDPOINTS} role add role0
etcdctl --endpoints=${ENDPOINTS} role grant-permission role0 readwrite foo
etcdctl --endpoints=${ENDPOINTS} user add user0
etcdctl --endpoints=${ENDPOINTS} user grant-role user0 role0
  • 启用角色授权

# 最后需要启用 etcd 的auth 功能
etcdctl --endpoints=${ENDPOINTS} auth enable
Authentication Enabled
  • 使用新添加的成员user0来操作etcd
etcdctl --endpoints=${ENDPOINTS} --user=user0:123 put foo bar
etcdctl --endpoints=${ENDPOINTS} get foo
#权限被拒绝,用户名为空,因为请求未发出身份验证请求
etcdctl --endpoints=${ENDPOINTS} --user=user0:123 get foo
#user0可以读取关键字foo
etcdctl --endpoints=${ENDPOINTS} --user=user0:123 get foo1

这个系列的帖子

类似的帖子