2017-12-03 05:57:02 +00:00
|
|
|
## etcd 集群
|
|
|
|
|
2017-12-03 10:53:35 +00:00
|
|
|
下面我们使用 [Docker Compose](../compose/) 模拟启动一个 3 节点的 `etcd` 集群。
|
2017-12-03 05:57:02 +00:00
|
|
|
|
|
|
|
编辑 `docker-compose.yml` 文件
|
|
|
|
|
|
|
|
```yaml
|
2018-07-10 00:08:34 +00:00
|
|
|
version: "3.6"
|
2017-12-03 05:57:02 +00:00
|
|
|
services:
|
|
|
|
|
|
|
|
node1:
|
2019-08-31 14:57:43 +00:00
|
|
|
image: quay.io/coreos/etcd:v3.4.0
|
2017-12-03 05:57:02 +00:00
|
|
|
volumes:
|
|
|
|
- node1-data:/etcd-data
|
|
|
|
expose:
|
|
|
|
- 2379
|
2019-02-21 02:53:33 +00:00
|
|
|
- 2380
|
2017-12-03 05:57:02 +00:00
|
|
|
networks:
|
|
|
|
cluster_net:
|
|
|
|
ipv4_address: 172.16.238.100
|
|
|
|
environment:
|
|
|
|
- ETCDCTL_API=3
|
|
|
|
command:
|
|
|
|
- /usr/local/bin/etcd
|
|
|
|
- --data-dir=/etcd-data
|
|
|
|
- --name
|
|
|
|
- node1
|
|
|
|
- --initial-advertise-peer-urls
|
|
|
|
- http://172.16.238.100:2380
|
|
|
|
- --listen-peer-urls
|
|
|
|
- http://0.0.0.0:2380
|
|
|
|
- --advertise-client-urls
|
|
|
|
- http://172.16.238.100:2379
|
|
|
|
- --listen-client-urls
|
|
|
|
- http://0.0.0.0:2379
|
|
|
|
- --initial-cluster
|
|
|
|
- node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
|
|
|
|
- --initial-cluster-state
|
|
|
|
- new
|
|
|
|
- --initial-cluster-token
|
|
|
|
- docker-etcd
|
|
|
|
|
|
|
|
node2:
|
2019-08-31 14:57:43 +00:00
|
|
|
image: quay.io/coreos/etcd:v3.4.0
|
2017-12-03 05:57:02 +00:00
|
|
|
volumes:
|
|
|
|
- node2-data:/etcd-data
|
|
|
|
networks:
|
|
|
|
cluster_net:
|
|
|
|
ipv4_address: 172.16.238.101
|
|
|
|
environment:
|
|
|
|
- ETCDCTL_API=3
|
|
|
|
expose:
|
|
|
|
- 2379
|
|
|
|
- 2380
|
|
|
|
command:
|
|
|
|
- /usr/local/bin/etcd
|
|
|
|
- --data-dir=/etcd-data
|
|
|
|
- --name
|
|
|
|
- node2
|
|
|
|
- --initial-advertise-peer-urls
|
|
|
|
- http://172.16.238.101:2380
|
|
|
|
- --listen-peer-urls
|
|
|
|
- http://0.0.0.0:2380
|
|
|
|
- --advertise-client-urls
|
|
|
|
- http://172.16.238.101:2379
|
|
|
|
- --listen-client-urls
|
|
|
|
- http://0.0.0.0:2379
|
|
|
|
- --initial-cluster
|
|
|
|
- node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
|
|
|
|
- --initial-cluster-state
|
|
|
|
- new
|
|
|
|
- --initial-cluster-token
|
|
|
|
- docker-etcd
|
|
|
|
|
|
|
|
node3:
|
2019-08-31 14:57:43 +00:00
|
|
|
image: quay.io/coreos/etcd:v3.4.0
|
2017-12-03 05:57:02 +00:00
|
|
|
volumes:
|
|
|
|
- node3-data:/etcd-data
|
|
|
|
networks:
|
|
|
|
cluster_net:
|
|
|
|
ipv4_address: 172.16.238.102
|
|
|
|
environment:
|
|
|
|
- ETCDCTL_API=3
|
|
|
|
expose:
|
|
|
|
- 2379
|
|
|
|
- 2380
|
|
|
|
command:
|
|
|
|
- /usr/local/bin/etcd
|
|
|
|
- --data-dir=/etcd-data
|
|
|
|
- --name
|
|
|
|
- node3
|
|
|
|
- --initial-advertise-peer-urls
|
|
|
|
- http://172.16.238.102:2380
|
|
|
|
- --listen-peer-urls
|
|
|
|
- http://0.0.0.0:2380
|
|
|
|
- --advertise-client-urls
|
|
|
|
- http://172.16.238.102:2379
|
|
|
|
- --listen-client-urls
|
|
|
|
- http://0.0.0.0:2379
|
|
|
|
- --initial-cluster
|
|
|
|
- node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
|
|
|
|
- --initial-cluster-state
|
|
|
|
- new
|
|
|
|
- --initial-cluster-token
|
|
|
|
- docker-etcd
|
|
|
|
|
|
|
|
volumes:
|
|
|
|
node1-data:
|
|
|
|
node2-data:
|
|
|
|
node3-data:
|
|
|
|
|
|
|
|
networks:
|
|
|
|
cluster_net:
|
|
|
|
driver: bridge
|
|
|
|
ipam:
|
|
|
|
driver: default
|
|
|
|
config:
|
|
|
|
-
|
|
|
|
subnet: 172.16.238.0/24
|
|
|
|
```
|
|
|
|
|
2017-12-03 10:53:35 +00:00
|
|
|
使用 `docker-compose up` 启动集群之后使用 `docker exec` 命令登录到任一节点测试 `etcd` 集群。
|
2017-12-03 05:57:02 +00:00
|
|
|
|
|
|
|
```bash
|
|
|
|
/ # etcdctl member list
|
|
|
|
daf3fd52e3583ff, started, node3, http://172.16.238.102:2380, http://172.16.238.102:2379
|
|
|
|
422a74f03b622fef, started, node1, http://172.16.238.100:2380, http://172.16.238.100:2379
|
|
|
|
ed635d2a2dbef43d, started, node2, http://172.16.238.101:2380, http://172.16.238.101:2379
|
|
|
|
```
|