etcd:开篇

etcd 是一个开源分布式的键值存储系统,它能够提供稳定的方式去跨数据中心存储数据,常用语配置共享和服务发现。它是开源免费的,https://github.com/etcd-io/etcd。etcd 优雅地处理了网络分区期间leader选举问题并且能够容忍节点宕机,即使是leader。

搭建 etcd 集群

单节点 etcd 安装很简单,下载已经编译好的二进制文件直接启动即可。etcd V3 官方文档,今天我利用 docker 快速创建一个 docker 集群,下面是我的 docker-compose.yaml 文件:

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
version: "3.6"
services:

proxy:
image: quay.io/coreos/etcd
networks:
etcd_cluster_net:
environment:
- ETCDCTL_API=3
ports:
- "2379:2379"
command:
- /usr/local/bin/etcd
- grpc-proxy
- start
- --listen-addr
- 0.0.0.0:2379
- --endpoints
- 172.16.238.100:2379,172.16.238.101:2379,172.16.238.102:2379

node1:
image: quay.io/coreos/etcd
volumes:
- node1-data:/etcd-data
expose:
- 2379
- 2380
networks:
etcd_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:
image: quay.io/coreos/etcd
volumes:
- node2-data:/etcd-data
networks:
etcd_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:
image: quay.io/coreos/etcd
volumes:
- node3-data:/etcd-data
networks:
etcd_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:
etcd_cluster_net:
driver: bridge
ipam:
driver: default
config:
-
subnet: 172.16.238.0/24

启动命令:docker-compose up -d,我们利用 etcd 的代理功能单独开启了一个容器做代理服务器,作为我们 API 的接收点。

测试连接:ETCDCTL_API=3 ./bin/etcdctl --endpoints http://127.0.0.1:2379 put "name" "michael"

参考阅读

  1. 基于 DNS 动态发现方式部署 Etcd 集群
  2. 基于已有集群动态发现方式部署 Etcd 集群
  3. 通过静态发现方式部署 Etcd 集群