etcd 集群

下面我们使用 Docker Compose 模拟启动一个 3 节点的 etcd 集群。

编辑 docker-compose.yml 文件

  1. version: "3.6"
  2. services:
  3. node1:
  4. image: quay.io/coreos/etcd:v3.4.0
  5. volumes:
  6. - node1-data:/etcd-data
  7. expose:
  8. - 2379
  9. - 2380
  10. networks:
  11. cluster_net:
  12. ipv4_address: 172.16.238.100
  13. environment:
  14. - ETCDCTL_API=3
  15. command:
  16. - /usr/local/bin/etcd
  17. - --data-dir=/etcd-data
  18. - --name
  19. - node1
  20. - --initial-advertise-peer-urls
  21. - http://172.16.238.100:2380
  22. - --listen-peer-urls
  23. - http://0.0.0.0:2380
  24. - --advertise-client-urls
  25. - http://172.16.238.100:2379
  26. - --listen-client-urls
  27. - http://0.0.0.0:2379
  28. - --initial-cluster
  29. - node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
  30. - --initial-cluster-state
  31. - new
  32. - --initial-cluster-token
  33. - docker-etcd
  34. node2:
  35. image: quay.io/coreos/etcd:v3.4.0
  36. volumes:
  37. - node2-data:/etcd-data
  38. networks:
  39. cluster_net:
  40. ipv4_address: 172.16.238.101
  41. environment:
  42. - ETCDCTL_API=3
  43. expose:
  44. - 2379
  45. - 2380
  46. command:
  47. - /usr/local/bin/etcd
  48. - --data-dir=/etcd-data
  49. - --name
  50. - node2
  51. - --initial-advertise-peer-urls
  52. - http://172.16.238.101:2380
  53. - --listen-peer-urls
  54. - http://0.0.0.0:2380
  55. - --advertise-client-urls
  56. - http://172.16.238.101:2379
  57. - --listen-client-urls
  58. - http://0.0.0.0:2379
  59. - --initial-cluster
  60. - node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
  61. - --initial-cluster-state
  62. - new
  63. - --initial-cluster-token
  64. - docker-etcd
  65. node3:
  66. image: quay.io/coreos/etcd:v3.4.0
  67. volumes:
  68. - node3-data:/etcd-data
  69. networks:
  70. cluster_net:
  71. ipv4_address: 172.16.238.102
  72. environment:
  73. - ETCDCTL_API=3
  74. expose:
  75. - 2379
  76. - 2380
  77. command:
  78. - /usr/local/bin/etcd
  79. - --data-dir=/etcd-data
  80. - --name
  81. - node3
  82. - --initial-advertise-peer-urls
  83. - http://172.16.238.102:2380
  84. - --listen-peer-urls
  85. - http://0.0.0.0:2380
  86. - --advertise-client-urls
  87. - http://172.16.238.102:2379
  88. - --listen-client-urls
  89. - http://0.0.0.0:2379
  90. - --initial-cluster
  91. - node1=http://172.16.238.100:2380,node2=http://172.16.238.101:2380,node3=http://172.16.238.102:2380
  92. - --initial-cluster-state
  93. - new
  94. - --initial-cluster-token
  95. - docker-etcd
  96. volumes:
  97. node1-data:
  98. node2-data:
  99. node3-data:
  100. networks:
  101. cluster_net:
  102. driver: bridge
  103. ipam:
  104. driver: default
  105. config:
  106. -
  107. subnet: 172.16.238.0/24

使用 docker-compose up 启动集群之后使用 docker exec 命令登录到任一节点测试 etcd 集群。

  1. / # etcdctl member list
  2. daf3fd52e3583ff, started, node3, http://172.16.238.102:2380, http://172.16.238.102:2379
  3. 422a74f03b622fef, started, node1, http://172.16.238.100:2380, http://172.16.238.100:2379
  4. ed635d2a2dbef43d, started, node2, http://172.16.238.101:2380, http://172.16.238.101:2379