(十一)etcd项目
etcd 是 CoreOS 团队发起的一个管理配置信息和服务发现(Service Discovery ) 的项目。
A distributed, reliable key-value store for the most critical data of a distributed system.
受到 Apache ZooKeeper 项目和 doozer 项目的启发, etcd 在设计的时候重点考虑了下面四个要素:
- 简单:具有定义良好、面向用户的 API (gRPC)
- 安全:支持 HTTPS 方式的访问
- 快速:支持并发 10 k/s 的写操作
- 可靠:支持分布式结构,基于 Raft 的一致性算法
1. 下载安装
默认 2379 端口处理客户端的请求, 2380 端口用于集群各成员间的通信。
1.1 二进制文件
下载地址 https://github.com/etcd-io/etcd/releases
curl -L https://github.com/coreos/etcd/releases/download/v3.3.9/etcd-v3.3.9-linux-amd64.tar.gz -o etcd-v3.3.9-linux-amd64.tar.gz
只需要解压即可运行 etcd 和 etcdctl
1.2 Docker 镜像
也可通过Docker镜像运行。
2. 使用
2.1 启动etcd服务
[root@Thor etcd]# etcd
2018-08-07 17:44:31.025901 I | etcdmain: etcd Version: 3.3.9
2018-08-07 17:44:31.026016 I | etcdmain: Git SHA: fca8add78
2018-08-07 17:44:31.026034 I | etcdmain: Go Version: go1.10.3
2018-08-07 17:44:31.026084 I | etcdmain: Go OS/Arch: linux/amd64
2018-08-07 17:44:31.026101 I | etcdmain: setting maximum number of CPUs to 56, total number of available CPUs is 56
2018-08-07 17:44:31.026132 W | etcdmain: no data-dir provided, using default data-dir ./default.etcd
2018-08-07 17:44:31.026788 I | embed: listening for peers on http://localhost:2380
2018-08-07 17:44:31.027006 I | embed: listening for client requests on localhost:2379
2018-08-07 17:44:31.028463 I | etcdserver: name = default
2018-08-07 17:44:31.028498 I | etcdserver: data dir = default.etcd
2018-08-07 17:44:31.028515 I | etcdserver: member dir = default.etcd/member
2018-08-07 17:44:31.028531 I | etcdserver: heartbeat = 100ms
2018-08-07 17:44:31.028571 I | etcdserver: election = 1000ms
2018-08-07 17:44:31.028588 I | etcdserver: snapshot count = 100000
2018-08-07 17:44:31.028641 I | etcdserver: advertise client URLs = http://localhost:2379
2018-08-07 17:44:31.028662 I | etcdserver: initial advertise peer URLs = http://localhost:2380
2018-08-07 17:44:31.028714 I | etcdserver: initial cluster = default=http://localhost:2380
2018-08-07 17:44:31.033183 I | etcdserver: starting member 8e9e05c52164694d in cluster cdf818194e3a8c32
2018-08-07 17:44:31.033268 I | raft: 8e9e05c52164694d became follower at term 0
2018-08-07 17:44:31.033306 I | raft: newRaft 8e9e05c52164694d [peers: [], term: 0, commit: 0, applied: 0, lastindex: 0, lastterm: 0]
2018-08-07 17:44:31.033353 I | raft: 8e9e05c52164694d became follower at term 1
2018-08-07 17:44:31.035573 W | auth: simple token is not cryptographically signed
2018-08-07 17:44:31.036705 I | etcdserver: starting server... [version: 3.3.9, cluster version: to_be_decided]
2018-08-07 17:44:31.038176 I | etcdserver: 8e9e05c52164694d as single-node; fast-forwarding 9 ticks (election ticks 10)
2018-08-07 17:44:31.039206 I | etcdserver/membership: added member 8e9e05c52164694d [http://localhost:2380] to cluster cdf818194e3a8c32
2018-08-07 17:44:32.033906 I | raft: 8e9e05c52164694d is starting a new election at term 1
2018-08-07 17:44:32.033972 I | raft: 8e9e05c52164694d became candidate at term 2
2018-08-07 17:44:32.034034 I | raft: 8e9e05c52164694d received MsgVoteResp from 8e9e05c52164694d at term 2
2018-08-07 17:44:32.034068 I | raft: 8e9e05c52164694d became leader at term 2
2018-08-07 17:44:32.034120 I | raft: raft.node: 8e9e05c52164694d elected leader 8e9e05c52164694d at term 2
2018-08-07 17:44:32.034589 I | embed: ready to serve client requests
2018-08-07 17:44:32.034768 E | etcdmain: forgot to set Type=notify in systemd service file?
2018-08-07 17:44:32.034819 I | etcdserver: published {Name:default ClientURLs:[http://localhost:2379]} to cluster cdf818194e3a8c32
2018-08-07 17:44:32.034850 I | etcdserver: setting up the initial cluster version to 3.3
2018-08-07 17:44:32.035123 N | etcdserver/membership: set the initial cluster version to 3.3
2018-08-07 17:44:32.035285 I | etcdserver/api: enabled capabilities for version 3.3
2018-08-07 17:44:32.035631 N | embed: serving insecure client requests on 127.0.0.1:2379, this is strongly discouraged!
2.2 etcdctl命令行客户端
etcdctl 支持如下的命令,大体上分为数据库操作和非数据库操作两类:
- 数据库操作:set/get/rm...
- 非数据库操作:watch/member...
[root@Thor ~]#etcdctl -h
NAME:
etcdctl - A simple command line client for etcd.
WARNING:
Environment variable ETCDCTL_API is not set; defaults to etcdctl v2.
Set environment variable ETCDCTL_API=3 to use v3 API or ETCDCTL_API=2 to use v2 API.
USAGE:
etcdctl [global options] command [command options] [arguments...]
VERSION:
3.3.9
COMMANDS:
backup backup an etcd directory
cluster-health check the health of the etcd cluster
mk make a new key with a given value
mkdir make a new directory
rm remove a key or a directory
rmdir removes the key if it is an empty directory or a key-value pair
get retrieve the value of a key
ls retrieve a directory
set set the value of a key
setdir create a new directory or update an existing directory TTL
update update an existing key with a given value
updatedir update an existing directory
watch watch a key for changes
exec-watch watch a key for changes and exec an executable
member member add, remove and list subcommands
user user add, grant and revoke subcommands
role role add, grant and revoke subcommands
auth overall auth controls
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--debug output cURL commands which can be used to reproduce the request
--no-sync don't synchronize cluster information before sending request
--output simple, -o simple output response in the given format (simple, `extended` or `json`) (default: "simple")
--discovery-srv value, -D value domain name to query for SRV records describing cluster endpoints
--insecure-discovery accept insecure SRV records describing cluster endpoints
--peers value, -C value DEPRECATED - "--endpoints" should be used instead
--endpoint value DEPRECATED - "--endpoints" should be used instead
--endpoints value a comma-delimited list of machine addresses in the cluster (default: "http://127.0.0.1:2379,http://127.0.0.1:4001")
--cert-file value identify HTTPS client using this SSL certificate file
--key-file value identify HTTPS client using this SSL key file
--ca-file value verify certificates of HTTPS-enabled servers using this CA bundle
--username value, -u value provide username[:password] and prompt if password is not supplied.
--timeout value connection timeout per request (default: 2s)
--total-timeout value timeout for the command execution (except watch) (default: 5s)
--help, -h show help
--version, -v print the version
3. 集群
4. HTTP JSON
[root@Thor ~]#curl -L http://127.0.0.1:2379/version
{"etcdserver":"3.3.9","etcdcluster":"3.3.0"}
[root@Thor ~]#curl -X PUT http://localhost:2379/v2/keys/age -d value=27
{"action":"set","node":{"key":"/age","value":"27","modifiedIndex":18,"createdIndex":18},"prevNode":{"key":"/age","value":"26","modifiedIndex":17,"createdIndex":17}}
[root@Thor ~]#curl -X GET http://localhost:2379/v2/keys/age
{"action":"get","node":{"key":"/age","value":"27","modifiedIndex":18,"createdIndex":18}}