Etcd 简介
etcd 是一个高可用的键值存储系统,主要用于共享配置和服务发现。etcd是由CoreOS开发并维护的,灵感来自于 ZooKeeper 和 Doozer,它使用Go语言编写,并通过Raft一致性算法处理日志复制以保证强一致性。Raft是一个来自Stanford的新的一致性算法,适用于分布式系统的日志复制,Raft通过选举的方式来实现一致性,在Raft中,任何一个节点都可能成为Leader。Google的容器集群管理系统Kubernetes、开源PaaS平台Cloud Foundry和CoreOS的Fleet都广泛使用了etcd。
etcd 集群的工作原理基于 raft 共识算法 (The Raft Consensus Algorithm)。
etcd 在 0.5.0 版本中重新实现了 raft 算法,而非像之前那样依赖于第三方库 go-raft 。
raft 共识算法的优点在于可以在高效的解决分布式系统中各个节点日志内容一致性问题的同时,也使得集群具备一定的容错能力。即使集群中出现部分节点故障、网络故障等问题,仍可保证其余大多数节点正确的步进。甚至当更多的节点(一般来说超过集群节点总数的一半)出现故障而导致集群不可用时,依然可以保证节点中的数据不会出现错误的结果。
1. Getting etcd
从 Release 页面(https://github.com/coreos/etcd/releases/ )下载二进制,或者拉最新 master 代码进行编译。
$ git clone https://github.com/coreos/etcd.git
$ cd etcd
$ ./build
还有一种办法,直接获取一个 vendor 编译好的 etcd,如下:
$ go get github.com/coreos/etcd/cmd/etcd
2. Starting etcd
直接执行
$ etcd
或者指定 data-dir
$ etcd -data-dir=/var/edata
更加详细的配置选项,请查看:https://coreos.com/etcd/docs/latest/op-guide/configuration.html
一个简单例子 human readable:
# This config is meant to be consumed by the config transpiler, which will
# generate the corresponding Ignition config. Do not pass this config directly
# to instances of Container Linux.
etcd:
name: my-etcd-1
listen_client_urls: https://10.240.0.1:2379
advertise_client_urls: https://10.240.0.1:2379
listen_peer_urls: https://10.240.0.1:2380
initial_advertise_peer_urls: https://10.240.0.1:2380
initial_cluster: my-1=https://10.240.0.1:2380,my-2=https://10.240.0.2:2380,my-3=https://10.240.0.3:2380
initial_cluster_token: my-etcd-token
initial_cluster_state: new
参考:
https://coreos.com/etcd/docs/latest/getting-started-with-etcd.html
https://docs.portworx.com/run-etcd.html
3. etcd cluster
etcd --name infra0 --initial-advertise-peer-urls http://10.0.1.10:2380 \
--listen-peer-urls http://10.0.1.10:2380 \
--listen-client-urls http://10.0.1.10:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://10.0.1.10:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster infra0=http://10.0.1.10:2380,infra1=http://10.0.1.11:2380,infra2=http://10.0.1.12:2380 \
--initial-cluster-state new
参考:
https://coreos.com/etcd/docs/latest/v2/docker_guide.html#running-etcd-in-standalone-mode
https://coreos.com/etcd/docs/latest/op-guide/clustering.html
4. etcdctl 查看帮助
上述启动的 etcd 是服务器,官方还提供了一个 commandline 客户端,即 etcdctl,所有的命令都通过这个客户端去请求。
执行下列命令来查看帮助信息:
$ ETCDCTL_API=3 etcdctl help
想要查看某个具体 command 的帮助,比如查看 get 命令的帮助:
$ ETCDCTL_API=3 etcdctl help get
NAME:
get - Gets the key or a range of keys
USAGE:
etcdctl get [options] <key> [range_end]
OPTIONS:
--consistency="l" Linearizable(l) or Serializable(s)
--from-key[=false] Get keys that are greater than or equal to the given key using byte compare
--keys-only[=false] Get only the keys
--limit=0 Maximum number of results
--order="" Order of results; ASCEND or DESCEND (ASCEND by default)
--prefix[=false] Get keys with matching prefix
--print-value-only[=false] Only write values when using the "simple" output format
--rev=0 Specify the kv revision
--sort-by="" Sort target; CREATE, KEY, MODIFY, VALUE, or VERSION
GLOBAL OPTIONS:
--cacert="" verify certificates of TLS-enabled secure servers using this CA bundle
--cert="" identify secure client using this TLS certificate file
--command-timeout=5s timeout for short running command (excluding dial timeout)
--debug[=false] enable client-side debug logging
--dial-timeout=2s dial timeout for client connections
--endpoints=[127.0.0.1:2379] gRPC endpoints
--hex[=false] print byte strings as hex encoded strings
--insecure-skip-tls-verify[=false] skip server certificate verification
--insecure-transport[=true] disable transport security for client connections
--key="" identify secure client using this TLS key file
--user="" username[:password] for authentication (prompt if password is not supplied)
-w, --write-out="simple" set the output format (fields, json, protobuf, simple, table)
5. Command examples
假设已有如下 keys
foo = bar
foo1 = bar1
foo2 = bar2
foo3 = bar3
默认 etcdctl 会key 和 value 都打印,想要只打印 value 的值:
$ etcdctl get foo --print-value-only
bar
获取所有 prefix 为 foo 的 key 所对应的 value
$ etcdctl get --prefix foo
foo
bar
foo1
bar1
foo2
bar2
foo3
bar3
更多命令:
https://coreos.com/etcd/docs/latest/dev-guide/interacting_v3.html