本文主要记录了如何通过docker-compose
来搭建etcd
,包括单节点和集群模式及其web监控
。
原文作者: 意琦行
原文链接: etcd教程(一)—通过docker安装etcd集群 | 指月小筑|意琦行的个人博客
1. 单节点
1.1 目录结构
/root/docker_study/
└── etcd
├── data # 数据存储
└── docker-compose.yml # docker-compose文件
1.2 docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| version: '3'
networks:
myetcd_single:
services:
etcd:
image: quay.io/coreos/etcd # 最好指定架构
container_name: etcd_single
command: etcd -name etcd1 -advertise-client-urls http://0.0.0.0:2379 -listen-client-urls http://0.0.0.0:2379 -listen-peer-urls http://0.0.0.0:2380
ports:
- 12379:2379
- 12380:2380
volumes:
- ./data:/etcd-data
networks:
- myetcd_single
etcdkeeper: # 不支持arm架构 所以会失败
image: deltaprojects/etcdkeeper
container_name: etcdkeeper_single
ports:
- 8088:8080
networks:
- myetcd_single
|
1.3 相关参数
参数 | 作用 |
---|
name | 节点名称 |
data-dir | 指定节点的数据存储目录 |
listen-client-urls | 对外提供服务的地址:比如 http://ip:2379,http://127.0.0.1:2379 ,客户端会连接到这里和 etcd 交互 |
listen-peer-urls | 监听URL,用于与其他节点通讯 |
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;否则会导致多个集群之间的冲突,造成未知的错误 |
1.4 启动
1
2
3
| docker-compose up
#后台启动增加`-d`参数
docker-compose up -d
|
2. 伪集群
2.1 目录结构
/root/docker_study/
└── etcd
├── data
├── docker-compose.yml
├── etcd1
├── etcd2
└── etcd3
2.2. docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
| version: '3'
networks:
myetcd:
services:
etcd1:
image: quay.io/coreos/etcd
container_name: etcd1
command: etcd -name etcd1 -advertise-client-urls http://0.0.0.0:2379 -listen-client-urls http://0.0.0.0:2379 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster "etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380" -initial-cluster-state new
ports:
- 12379:2379
- 12380:2380
volumes:
- ./data/etcd1:/etcd-data
networks:
- myetcd
etcd2:
image: quay.io/coreos/etcd
container_name: etcd2
command: etcd -name etcd2 -advertise-client-urls http://0.0.0.0:2379 -listen-client-urls http://0.0.0.0:2379 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster "etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380" -initial-cluster-state new
ports:
- 22379:2379
- 22380:2380
volumes:
- ./data/etcd2:/etcd-data
networks:
- myetcd
etcd3:
image: quay.io/coreos/etcd
container_name: etcd3
command: etcd -name etcd3 -advertise-client-urls http://0.0.0.0:2379 -listen-client-urls http://0.0.0.0:2379 -listen-peer-urls http://0.0.0.0:2380 -initial-cluster-token etcd-cluster -initial-cluster "etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380" -initial-cluster-state new
ports:
- 32379:2379
- 32380:2380
volumes:
- ./data/etcd3:/etcd-data
networks:
- myetcd
etcdkeeper:
image: deltaprojects/etcdkeeper
container_name: etcdkeeper
ports:
- 8088:8080
networks:
- myetcd
|
2.3. 启动
1
2
3
| docker-compose up
#后台启动增加`-d`参数
docker-compose up -d
|
2.4. 查看集群信息
1
2
3
| [root@localhost etcd3]# curl -L http://127.0.0.1:32379/v2/members
{"members":[{"id":"ade526d28b1f92f7","name":"etcd1","peerURLs":["http://etcd1:2380"],"clientURLs":["http://0.0.0.0:2379"]},{"id":"bd388e7810915853","name":"etcd3","peerURLs":["http://etcd3:2380"],"clientURLs":["http://0.0.0.0:2379"]},{"id":"d282ac2ce600c1ce","name":"etcd2","peerURLs":["http://etcd2:2380"],"clientURLs":["http://0.0.0.0:2379"]}]}
|
3. web监控
启动etcd的同时也会启动web监控etcdkeeper
访问路径