etcd使用

Posted 北漂-boy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了etcd使用相关的知识,希望对你有一定的参考价值。

一、概述

ETCD是一个开源的、分布式的键值对数据存储系统,由Go语言实现,用于存储key-value键值对,同时不仅仅是存储,主要用途是提供共享配置及服务发现,使用Raft一致性算法来管理高度可用的复制日志。有下面特点

简单:定义明确,面向用户的API(gRPC)
安全:具有可选客户端证书身份验证的自动TLS
快速:基准测试10,000次/秒
可靠:使用Raft正确分布

二、安装

2.1、二进制安装

  1. 下载地址:
wget https://github.com/etcd-io/etcd/releases/download/v3.4.9/etcd-v3.4.9-linux-amd64.tar.gz
  1. 解压即可使用
tar zxvf etcd-v3.4.9-linux-amd64.tar.gz
  1. 解压以后文件信息如下
    解压以后的文件信息

2.2 docker化安装

# docker run -itd -p 2379:2379 -p 2380:2380 --name etcd elcolio/etcd

进入docker容器就可以正常使用了

# docker exec -it etcd bash


不过版本有点老了

三、基本使用

1、启动单节点服务

(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcd
[WARNING] Deprecated \'--logger=capnslog\' flag is set; use \'--logger=zap\' flag instead
2020-06-23 16:55:10.974638 I | etcdmain: etcd Version: 3.4.9
2020-06-23 16:55:10.974744 I | etcdmain: Git SHA: 54ba95891
2020-06-23 16:55:10.974781 I | etcdmain: Go Version: go1.12.17
2020-06-23 16:55:10.974815 I | etcdmain: Go OS/Arch: linux/amd64
2020-06-23 16:55:10.974851 I | etcdmain: setting maximum number of CPUs to 40, total number of available CPUs is 40
2020-06-23 16:55:10.974896 W | etcdmain: no data-dir provided, using default data-dir ./default.etcd
[WARNING] Deprecated \'--logger=capnslog\' flag is set; use \'--logger=zap\' flag instead
2020-06-23 16:55:10.975986 I | embed: name = default
2020-06-23 16:55:10.976024 I | embed: data dir = default.etcd
2020-06-23 16:55:10.976047 I | embed: member dir = default.etcd/member
2020-06-23 16:55:10.976078 I | embed: heartbeat = 100ms
2020-06-23 16:55:10.976102 I | embed: election = 1000ms
2020-06-23 16:55:10.976125 I | embed: snapshot count = 100000
2020-06-23 16:55:10.976156 I | embed: advertise client URLs = http://localhost:2379
2020-06-23 16:55:10.980673 I | etcdserver: starting member 8e9e05c52164694d in cluster cdf818194e3a8c32
raft2020/06/23 16:55:10 INFO: 8e9e05c52164694d switched to configuration voters=()
raft2020/06/23 16:55:10 INFO: 8e9e05c52164694d became follower at term 0
raft2020/06/23 16:55:10 INFO: newRaft 8e9e05c52164694d [peers: [], term: 0, commit: 0, applied: 0, lastindex: 0, lastterm: 0]
raft2020/06/23 16:55:10 INFO: 8e9e05c52164694d became follower at term 1
raft2020/06/23 16:55:10 INFO: 8e9e05c52164694d switched to configuration voters=(10276657743932975437)
2020-06-23 16:55:10.982217 W | auth: simple token is not cryptographically signed
2020-06-23 16:55:10.984728 I | etcdserver: starting server... [version: 3.4.9, cluster version: to_be_decided]
2020-06-23 16:55:10.985016 I | etcdserver: 8e9e05c52164694d as single-node; fast-forwarding 9 ticks (election ticks 10)
raft2020/06/23 16:55:10 INFO: 8e9e05c52164694d switched to configuration voters=(10276657743932975437)
2020-06-23 16:55:10.985788 I | etcdserver/membership: added member 8e9e05c52164694d [http://localhost:2380] to cluster cdf818194e3a8c32
2020-06-23 16:55:10.989122 I | embed: listening for peers on 127.0.0.1:2380
raft2020/06/23 16:55:11 INFO: 8e9e05c52164694d is starting a new election at term 1
raft2020/06/23 16:55:11 INFO: 8e9e05c52164694d became candidate at term 2
raft2020/06/23 16:55:11 INFO: 8e9e05c52164694d received MsgVoteResp from 8e9e05c52164694d at term 2
raft2020/06/23 16:55:11 INFO: 8e9e05c52164694d became leader at term 2
raft2020/06/23 16:55:11 INFO: raft.node: 8e9e05c52164694d elected leader 8e9e05c52164694d at term 2
2020-06-23 16:55:11.482246 I | etcdserver: setting up the initial cluster version to 3.4
2020-06-23 16:55:11.482468 I | etcdserver: published {Name:default ClientURLs:[http://localhost:2379]} to cluster cdf818194e3a8c32
2020-06-23 16:55:11.482523 I | embed: ready to serve client requests
2020-06-23 16:55:11.482594 N | etcdserver/membership: set the initial cluster version to 3.4
2020-06-23 16:55:11.482737 I | etcdserver/api: enabled capabilities for version 3.4
2020-06-23 16:55:11.483780 N | embed: serving insecure client requests on 127.0.0.1:2379, this is strongly discouraged!

启动信息说明:

name表示节点名称,默认为default。
data-dir 保存日志和快照的目录,默认为当前工作目录default.etcd/目录下。
在http://localhost:2380和集群中其他节点通信。
在http://localhost:2379提供HTTP API服务,供客户端交互。
heartbeat为100ms,该参数的作用是leader多久发送一次心跳到followers,默认值是100ms。
election为1000ms,该参数的作用是重新投票的超时时间,如果follow在该超时时间间隔没有收到心跳包,会触发重新投票,默认为1000ms。
snapshot count为100000,该参数的作用是指定有多少事务被提交时,触发截取快照保存到磁盘。
集群和每个节点都会生成一个uuid。
启动的时候会运行raft,选举出leader。

如果是测试,可以直接使用 ./etcd 启动,当然还可以配置其他参
列如:
启动时指定节点名称和数据存放目录

./etcd --name \'test\' --data-dir test.dir
./etcd --data-dir ./data --listen-client-urls \'http://192.168.1.15:2379\' --advertise-client-urls  \'http://192.168.1.15:2379\'

查看数据目录信息

(base) [root@localhost etcd-v3.4.9-linux-amd64]# tree test.dir/
test.dir/
└── member
    ├── snap
    │   └── db
    └── wal
        ├── 0000000000000000-0000000000000000.wal
        └── 0.tmp

3 directories, 3 files

① snap:存放快照信息
② wal: 存放数据库的操作日志信息,wal就是(wal-ahead log)预写日志。类似于hbase的wal,主要是保存数据的可用性。可以使用 --wal-dir 指定其他的存储位置。
./etcd -h

Member:
  --name \'default\'
    Human-readable name for this member.
  --data-dir \'${name}.etcd\'
    Path to the data directory.
  --wal-dir \'\'
    Path to the dedicated wal directory.
  --snapshot-count \'100000\'
    Number of committed transactions to trigger a snapshot to disk.
  --heartbeat-interval \'100\'
    Time (in milliseconds) of a heartbeat interval.
  --election-timeout \'1000\'
    Time (in milliseconds) for an election to timeout. See tuning documentation for details.
  --initial-election-tick-advance \'true\'
    Whether to fast-forward initial election ticks on boot for faster election.
  --listen-peer-urls \'http://localhost:2380\'
    List of URLs to listen on for peer traffic.
  --listen-client-urls \'http://localhost:2379\'
    List of URLs to listen on for client traffic.
  --max-snapshots \'5\'
    Maximum number of snapshot files to retain (0 is unlimited).
  --max-wals \'5\'
    Maximum number of wal files to retain (0 is unlimited).
  --quota-backend-bytes \'0\'
    Raise alarms when backend size exceeds the given quota (0 defaults to low space quota).
  --backend-batch-interval \'\'
    BackendBatchInterval is the maximum time before commit the backend transaction.
  --backend-batch-limit \'0\'
    BackendBatchLimit is the maximum operations before commit the backend transaction.
  --max-txn-ops \'128\'
    Maximum number of operations permitted in a transaction.
  --max-request-bytes \'1572864\'
    Maximum client request size in bytes the server will accept.
  --grpc-keepalive-min-time \'5s\'
    Minimum duration interval that a client should wait before pinging server.
  --grpc-keepalive-interval \'2h\'
    Frequency duration of server-to-client ping to check if a connection is alive (0 to disable).
  --grpc-keepalive-timeout \'20s\'
    Additional duration of wait before closing a non-responsive connection (0 to disable).
.........

检查服务状态

(base) [root@localhost etcd-v3.4.9-linux-amd64]# curl -L localhost:2379/health
{"health":"true"}

2、put :设置键值对

# ./etcdctl put key value  #老版本的是set命令,目前的版本是第三版,老版本对应v2版。
OK

或者:

./etcdctl  --endpoints=192.168.1.15:2379  put key value

3、get :获取指定key的信息,当键不存在的时候返回会空,不会报错。

① 获取指定key
# ./etcdctl get key
key
value
② 只获取值
(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcdctl get key --print-value-only  #获取数据的时候不获取key,仅仅只获取值
value
③ 前缀匹配key或者说过滤key
(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcdctl get  --prefix k --print-value-only
v1
v4
v7
value

3、grant :授权租期(TTL)

# ./etcdctl lease grant 20     #设置TTL为20秒
lease 694d72e06369850f granted with TTL(20s)

# ./etcdctl put --lease=694d72e06369850f  k2 v2   #插入数据的时候指定TTL
OK
# ./etcdctl get  k2  
k2
v2
# ./etcdctl get  k2 #20秒过后数据将会被删除
# 

4、revoke : 回收租期

(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcdctl lease grant 60
lease 694d72e063698515 granted with TTL(60s)
(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcdctl put --lease=694d72e063698515  k3 v3
OK
(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcdctl lease revoke 694d72e063698515   #回收租期
lease 694d72e063698515 revoked
(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcdctl get k3 #租期回收以后,数据将会删除
(base) [root@localhost etcd-v3.4.9-linux-amd64]#

5、keep-alive :让租期保持一致存活

./etcdctl lease keep-alive 694d72e06369851a
lease 694d72e06369851a keepalived with TTL(10)
lease 694d72e06369851a keepalived with TTL(10)
lease 694d72e06369851a keepalived with TTL(10)
lease 694d72e06369851a keepalived with TTL(10)
lease 694d72e06369851a keepalived with TTL(10)
lease 694d72e06369851a keepalived with TTL(10)
lease 694d72e06369851a keepalived with TTL(10)

(base) [root@localhost etcd-v3.4.9-linux-amd64]# date;./etcdctl put --lease=694d72e06369851a k6 v6   #插入数据
Tue Jun 23 17:27:02 CST 2020
OK
(base) [root@localhost etcd-v3.4.9-linux-amd64]# date;./etcdctl  get k6  #获取数据,10秒钟以后数据还是存活的
Tue Jun 23 17:27:13 CST 2020
k6
v6
(base) [root@localhost etcd-v3.4.9-linux-amd64]# date;./etcdctl  get k6  #crtl + c停止存活,可以发现数据就消失了
Tue Jun 23 17:28:17 CST 2020
(base) [root@localhost etcd-v3.4.9-linux-amd64]# 

6、member list :查看集群成员信息

(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcdctl member list
8e9e05c52164694d, started, default, http://localhost:2380, http://localhost:2379, false

7、创建用户

(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcdctl user list
(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcdctl user add test1
Password of test1: #输入密码
Type password of test1 again for confirmation: #再次确实密码
User test1 created
(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcdctl user get test1
User: test1
Roles:  #显示用户属于哪个角色
(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcdctl user list
test1
(base) [root@localhost etcd-v3.4.9-linux-amd64]# 

8、创建角色

(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcdctl role add role1
Role role1 created

9、给用户授权角色

(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcdctl user grant-role test1 role1 #grant-role 用户名  角色名
Role role1 is granted to user test1
(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcdctl user get test1
User: test1
Roles: role1
(base) [root@localhost etcd-v3.4.9-linux-amd64]# 

10、开启认证

(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcdctl auth enable --user=\'test1\'
Password: 
{"level":"warn","ts":"2020-06-23T17:43:15.804+0800","caller":"clientv3/retry_interceptor.go:62","msg":"retrying of unary invoker failed","target":"endpoint://client-aa4e66e9-684d-4966-867a-03dcdf79cb0c/127.0.0.1:2379","attempt":0,"error":"rpc error: code = FailedPrecondition desc = etcdserver: authentication is not enabled"}
{"level":"warn","ts":"2020-06-23T17:43:15.806+0800","caller":"clientv3/retry_interceptor.go:62","msg":"retrying of unary invoker failed","target":"endpoint://client-aa4e66e9-684d-4966-867a-03dcdf79cb0c/127.0.0.1:2379","attempt":0,"error":"rpc error: code = FailedPrecondition desc = etcdserver: root user does not exist"}
Error: etcdserver: root user does not exist   #需要创建一个root用户
(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcdctl user add root
Password of root: 
Type password of root again for confirmation: 
User root created
(base) [root@localhost etcd-v3.4.9-linux-amd64]# ./etcdctl auth enable --user=\'test1\'
Password: 
{"level":"warn","ts":"2020-06-23T17:43:55.778+0800","caller":"clientv3/retry_interceptor.go:62","msg":"retrying of unary invoker failed","target":"endpoint://client-261be6df-24f4-40ea-99c4-918878caedf1/127.0.0.1:2379","attempt":0,"error":"rpc error: code = FailedPrecondition desc = etcdserver: authentication is not enabled"}
{"level":"warn","ts":"2020-06-23T17:43:55.780+0800","caller":"clientv3/retry_interceptor.go:62","msg":"retrying of unary invoker failed","target":"endpoint://client-261be6df-24f4-40ea-99c4-918878caedf1/127.0.0.1:2379","attempt":0,"error":"rpc error: code = FailedPrecondition desc = etcdserver: root user does not have root role"}
Authentication Enabled
(base) [root@localhost etcd-v3.4.9-linux-amd64]# 

以上是关于etcd使用的主要内容,如果未能解决你的问题,请参考以下文章

ETCD:词汇表

etcd Raft 源码剖析

etcd Raft 源码剖析

etcd Raft 源码剖析

etcd Raft 源码剖析

开发一个简单的 etcd operator