etcd 基于 Go 语言实现,因此,用户可以从github下载源代码自行编译,也可以下载编译好的二进制文件,甚至直接使用制作好的 Docker 镜像文件来体验。
二进制文件方式
安装
编译好的二进制文件都在 github.com/coreos/etcd/releases
页面,用户可以选择需要的版本,或通过下载工具下载。
例如,使用 curl 工具下载压缩包,并解压。
$ curl -L https://github.com/coreos/etcd/releases/download/v3.2.10/etcd-v3.2.10-linux-amd64.tar.gz -o etcd-v3.2.10-linux-amd64.tar.gz
$ tar xzvf etcd-v3.2.10-linux-amd64.tar.gz
$ cd etcd-v3.2.10-linux-amd64
解压后,可以看到文件包括
$ ls
Documentation README-etcdctl.md README.md READMEv2-etcdctl.md etcd etcdctl
其中 etcd
是服务主文件,etcdctl
是提供给用户的命令客户端,其他文件是支持文档。
下面将 etcd
etcdctl
文件放到系统可执行目录(例如 /usr/local/bin/)。
$ sudo cp etcd* /usr/local/bin/
端口说明:
- 默认
2379
端口处理客户端的请求
2380
端口用于集群各成员间的通信
启动
启动 etcd 显示类似如下的信息:
# etcd
2017-12-03 11:18:34.406082 I | etcdmain: etcd Version: 3.2.10
2017-12-03 11:18:34.406226 I | etcdmain: Git SHA: GitNotFound
2017-12-03 11:18:34.406235 I | etcdmain: Go Version: go1.9.2
2017-12-03 11:18:34.406242 I | etcdmain: Go OS/Arch: darwin/amd64
2017-12-03 11:18:34.406250 I | etcdmain: setting maximum number of CPUs to 4, total number of available CPUs is 4
2017-12-03 11:18:34.406265 N | etcdmain: failed to detect default host (default host not supported on darwin_amd64)
2017-12-03 11:18:34.406279 W | etcdmain: no data-dir provided, using default data-dir ./default.etcd
2017-12-03 11:18:34.406457 N | etcdmain: the server is already initialized as member before, starting as etcd member...
2017-12-03 11:18:34.411579 I | embed: listening for peers on http://localhost:2380
2017-12-03 11:18:34.411938 I | embed: listening for client requests on localhost:2379
此时,可以使用 etcdctl
命令进行测试,设置和获取键值 testkey: "hello world"
,检查 etcd
服务是否启动成功:
# export ETCDCTL_API=3
$ etcdctl member list
8e9e05c52164694d, started, default, http://localhost:2380, http://localhost:2379
$ etcdctl put testkey "hello world"
OK
$ etcdctl get testkey
testkey
hello world
说明 etcd 服务已经成功启动了。
参数说明
常用配置的参数和它们的解释:
- –name:方便理解的节点名称,默认为 default,在集群中应该保持唯一,可以使用 hostname
- –data-dir:服务运行数据保存的路径,默认为 ${name}.etcd
- –snapshot-count:指定有多少事务(transaction)被提交时,触发截取快照保存到磁盘
- –heartbeat-interval:leader 多久发送一次心跳到 followers。默认值是 100ms
- –eletion-timeout:重新投票的超时时间,如果 follow 在该时间间隔没有收到心跳包,会触发重新投票,默认为 1000 ms
- –listen-peer-urls:和同伴通信的地址,比如 http://ip:2380,如果有多个,使用逗号分隔。需要所有节点都能够访问,所以不要使用 localhost!
- –listen-client-urls:对外提供服务的地址:比如 http://ip:2379,http://127.0.0.1:2379,客户端会连接到这里和 etcd 交互
- –advertise-client-urls:对外公告的该节点客户端监听地址,这个值会告诉集群中其他节点
- –initial-advertise-peer-urls:该节点同伴监听地址,这个值会告诉集群中其他节点
- –initial-cluster:集群中所有节点的信息,格式为 node1=http://ip1:2380,node2=http://ip2:2380,…。注意:这里的 node1 是节点的 –name 指定的名字;后面的 ip1:2380 是 –initial-advertise-peer-urls 指定的值
- –initial-cluster-state:新建集群的时候,这个值为 new;假如已经存在的集群,这个值为 existing
- –initial-cluster-token:创建集群的 token,这个值每个集群保持唯一。这样的话,如果你要重新创建集群,即使配置和之前一样,也会再次生成新的集群和节点 uuid;否则会导致多个集群之间的冲突,造成未知的错误
所有以 --init
开头的配置都是在 bootstrap
集群的时候才会用到,后续节点的重启会被忽略。
systemctl 启动
/etc/systemd/system/etcd.service
[Unit]
Description=etcd
Documentation=https://github.com/coreos/etcd
[Service]
Type=notify
Restart=always
RestartSec=5s
LimitNOFILE=40000
TimeoutStartSec=0
EnvironmentFile=-/etc/sysconfig/etcd-ssl
ExecStart=/usr/local/bin/etcd \
--name etcd1 \
--data-dir /data/bkee/public/etcd \
--initial-advertise-peer-urls http://<ip1>:2380 \
--listen-peer-urls http://<ip1>:2380 \
--advertise-client-urls http://<ip1>:2379 \
--listen-client-urls http://<ip1>:2379,http://127.0.0.1:2379 \
--initial-cluster etcd0=http://<ip0>:2380 \
--initial-cluster-state new \
--initial-cluster-token etcd-cluster-token \
--auto-compaction-retention 1
[Install]
WantedBy=multi-user.target
操作命令:
- systemctl enable etcd
- systemctl start etcd
Docker 镜像方式
可以通过下面的命令启动 etcd 服务监听到 2379 和 2380 端口。
export ETCD1=10.10.10.10
docker run --name etcd \
-p 2379:2379 \
-p 2380:2380 \
--volume=etcd-data:/etcd-data \
gcr.io/etcd-development/etcd:latest \
/usr/local/bin/etcd \
--name etcd1 \
--data-dir=/etcd-data \
--initial-advertise-peer-urls http://${ETCD1}:2380 \
--listen-peer-urls http://0.0.0.0:2380 \
--advertise-client-urls http://${ETCD1}:2379 \
--listen-client-urls http://0.0.0.0:2379 \
--initial-cluster etcd1=http://${ETCD1}:2380
打开新的终端按照上一步的方法测试 etcd 是否成功启动。