从前面的文章中,我们已经了解到了etcd是什么以及它的适用场景,也知道它是通过raft算法来保证一致性的。
这一篇主要记录如果快算安装一个etcd并启动一个集群。
安装etcd
我使用的是Mac系统,这里推荐2种方式安装etcd
brew安装
通过mac系统自带的brew安装,只需要执行如下命令即可:1
brew install etcd
下载安装包
在etcd的release页面,找到最新的下载包,比如说:etcd-v3.4.7-darwin-amd64.zip
下载文件到本地即可。
解压文件后,可以看到2个文件: etcd 和 etcdctl
其中etcd文件是启动etcd的脚本,etcdctl是操作etcd的命令行,对这两个文件进行软链。
1 | ln -s ~/Downloads/etcd-v3.4.5-darwin-amd64/etcd /usr/local/bin/etcd |
启动etcd集群
直接执行etcd命令,会在当前目录下生成一个default.etcd的文件夹,这时启动的也是单节点的etcd服务。
那么如何启动一个etcd集群呢?
官方etcd github提供了一个快速在本地启动一个集群的方法。
- 首先安装goreman
在golang环境下,直接执行go get github.com/mattn/goreman 新建一个Procfile文件
1
2
3
4
5
6
7
8
9# Use goreman to run `go get github.com/mattn/goreman`
# Change the path of bin/etcd if etcd is located elsewhere
etcd1: bin/etcd --name infra1 --listen-client-urls http://127.0.0.1:2379 --advertise-client-urls http://127.0.0.1:2379 --listen-peer-urls http://127.0.0.1:12380 --initial-advertise-peer-urls http://127.0.0.1:12380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof --logger=zap --log-outputs=stderr
etcd2: bin/etcd --name infra2 --listen-client-urls http://127.0.0.1:22379 --advertise-client-urls http://127.0.0.1:22379 --listen-peer-urls http://127.0.0.1:22380 --initial-advertise-peer-urls http://127.0.0.1:22380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof --logger=zap --log-outputs=stderr
etcd3: bin/etcd --name infra3 --listen-client-urls http://127.0.0.1:32379 --advertise-client-urls http://127.0.0.1:32379 --listen-peer-urls http://127.0.0.1:32380 --initial-advertise-peer-urls http://127.0.0.1:32380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof --logger=zap --log-outputs=stderr
#proxy: bin/etcd grpc-proxy start --endpoints=127.0.0.1:2379,127.0.0.1:22379,127.0.0.1:32379 --listen-addr=127.0.0.1:23790 --advertise-client-url=127.0.0.1:23790 --enable-pprof
# A learner node can be started using Procfile.learner启动集群
执行goreman start命令,会启动3个etcd成员:infra1,infra2,infra31
goreman start
快速验证
向集群里put一个key并获取它1
ETCDCTL_API=3 etcdctl put mykey "this is awesome"
然后get刚才放入的key1
ETCDCTL_API=3 etcdctl get mykey
下一次详细讲etcd提高了哪些功能。