https://github.com/etcd-io/etcd/releases
https://hub.docker.com/r/bitnami/etcd
https://etcd.io/docs/current/
# docker
docker run -d --name etcd --restart=always --p 2379:2379 --p 2380:2380 --env ALLOW_NONE_AUTHENTICATION=yes --env ETCD_ADVERTISE_CLIENT_URLS=http://etcd-server:2379 bitnami/etcd:latest
docker run -d --name etcdkeeper --restart=always -p 8080:8080 deltaprojects/etcdkeeper:latest
-http://ip:8080/etcdkeeper
-输入etcd的地址
-v /path/to/etcd.conf.yml:/opt/bitnami/etcd/conf/etcd.conf.yml
# 编译版
curl -L https://github.com/etcd-io/etcd/releases/download/v3.4.14/etcd-v3.4.14-linux-amd64.tar.gz -o /opt/app/etcd-v3.4.14-linux-amd64.tar.gz
mkdir -p /opt/app/etcd
tar xzvf /opt/app/etcd-v3.4.14-linux-amd64.tar.gz -C /opt/app/etcd --strip-components=1
chmod -R 777 /opt/app/etcd
cd /opt/app/etcd/
cp etcd /usr/local/bin
cp etcdctl /usr/local/bin
rm -f /opt/app/etcd-v3.4.14-linux-amd64.tar.gz
/opt/app/etcd/etcd --version
/opt/app/etcd/etcdctl version
# start a local etcd server
/opt/app/etcd/etcd
# write,read to etcd
/opt/app/etcd/etcdctl --endpoints=localhost:2379 put foo bar
/opt/app/etcd/etcdctl --endpoints=localhost:2379 get foo
# 安装 etcd 单机版,直接github下载二进制版本即可运行。
nohup /opt/app/etcd/etcd --listen-client-urls http://0.0.0.0:2379 --advertise-client-urls http://0.0.0.0:2379 > etcd.log &
# 多节点集群化部署
一. 静态配置
三台机器信息:
name ip
etcd1 192.168.159.128
etcd2 192.168.159.129
etcd3 192.168.159.130
etcd1配置文件conf.yml
name: etcd1
data-dir: /project/etcd_cluster/etcd1/data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://192.168.159.128:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://192.168.159.128:2380
initial-cluster: etcd1=http://192.168.159.128:2380,etcd2=http://192.168.159.129:2380,etcd3=http://192.168.159.130:2380
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new
etcd2配置文件conf.yml如下:
name: etcd2
data-dir: /project/etcd_cluster/etcd2/data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://192.168.159.129:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://192.168.159.129:2380
initial-cluster: etcd1=http://192.168.159.128:2380,etcd2=http://192.168.159.129:2380,etcd3=http://192.168.159.130:2380
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new
etcd3配置文件conf.yml如下:
name: etcd3
data-dir: /project/etcd_cluster/etcd3/data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://192.168.159.130:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://192.168.159.130:2380
initial-cluster: etcd1=http://192.168.159.128:2380,etcd2=http://192.168.159.129:2380,etcd3=http://192.168.159.130:2380
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new
注意,上面配置文件中listen-client-urls和listen-peer-urls参数监听的的是所有的地址。
接下来通过etcd(此时etcd压缩包已经拷贝至另外两台机器)来启动各个节点:
192.168.159.128:
./etcd --config-file=/project/etcd_cluster/etcd1/conf.yml
192.168.159.129:
./etcd --config-file=/project/etcd_cluster/etcd2/conf.yml
192.168.159.130:
./etcd --config-file=/project/etcd_cluster/etcd3/conf.yml
# 启动完毕后对集群的状态进行查看
---查看成员
./etcdctl member list
---查看集群健康状态
./etcdctl cluster-health
#测试
ETCDCTL_API=3 ./etcdctl put foo bar
ETCDCTL_API=3 ./etcdctl get foo
二. 服务发现
实际应用中,可能无法知道各个节点ip地址的情况,所以就不能使用上述的静态配置,而是需要使用服务发现,实质上就是利用现有的etcd集群来启动一个新的etcd集群。我们可以使用之前搭建好的集群来搭建。
1、创建发现url(集群标识)
---使用现有的集群创建发现url:
curl -X PUT http://192.168.159.128:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83/_config/size -d value=3
{"action":"set","node":{"key":"/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83/_config/size","value":"3","modifiedIndex":9,"createdIndex":9}}
---当然,如果没有自己的已有集群,也可以通过etcd提供的公共服务获取发现url:
-size代表要创建的集群大小
[root@localhost etcd-v3.3.10-linux-amd64]# curl -w "\n" 'https://discovery.etcd.io/new?size=3'
https://discovery.etcd.io/8c66ef7d7bdccfa2e83f277b2407bd44
2、启动新的集群
服务发现的url是http://192.168.159.128:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83,需要将其作为--discovery参数来启动etcd,新的etcd实例会自动使用服务发现url的目录来启动注册。
三个节点的配置文件如下:
infra0
name: infra0
listen-client-urls: http://0.0.0.0:12379
advertise-client-urls: http://192.168.159.128:12379
listen-peer-urls: http://0.0.0.0:12380
initial-advertise-peer-urls: http://192.168.159.128:12380
discovery: http://192.168.159.128:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83
infra1
name: infra1
listen-client-urls: http://0.0.0.0:12379
advertise-client-urls: http://192.168.159.129:12379
listen-peer-urls: http://0.0.0.0:12380
initial-advertise-peer-urls: http://192.168.159.129:12380
discovery: http://192.168.159.128:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83
infra2
name: infra2
listen-client-urls: http://0.0.0.0:12379
advertise-client-urls: http://192.168.159.130:12379
listen-peer-urls: http://0.0.0.0:12380
initial-advertise-peer-urls: http://192.168.159.130:12380
discovery: http://192.168.159.128:2379/v2/keys/discovery/6c007a14875d53d9bf0ef5a6fc0257c817f0fb83
-可以看到这与之前静态配置的区别是不需要initial-cluster、initial-cluster-token、initial-cluster-state三个参数,但是需要discovery参数用来获取集群标识。上面的配置没有加上data-dir,这会在当前目录下创建一个数据目录infra0.etcd(或者infra1.etcd、infra2.etcd)
然后,可以进行启动集群,与静态配置方式启动一样,不过配置文件不同
# infra0
./etcd --config-file=/project/etcd_cluster/infra0/conf.yml
# infra1
./etcd --config-file=/project/etcd_cluster/infra1/conf.yml
# infra2
./etcd --config-file=/project/etcd_cluster/infra2/conf.yml
获取集群信息:
./etcdctl -endpoints=http://192.168.159.128:12379 member list