基于Docker的ETCD分布式集群

目录

[1. 说明](#1. 说明)

[2. 配置表](#2. 配置表)

[3. 步骤](#3. 步骤)

[3.1 放行端口](#3.1 放行端口)

[3.2 docker-compose 文件](#3.2 docker-compose 文件)

[3.3 部署到3台服务器](#3.3 部署到3台服务器)

[3.4 相关命令](#3.4 相关命令)

[4. 参考](#4. 参考)


1. 说明

  • 以docker容器方式实现ETCD分布式集群,为其他项目提供支持,经过反复试验成功部署**(网上资料大都过期或部署失败)**。

  • 三个ETCD服务点供访问

2. 配置表

服务器 etcd name ip 备注
center01.dev.sb etcd0 172.16.20.20 硬件配置:16核32G 软件配置:ubuntu22.04 + 宝塔面板
host004.dev.sb etcd1 172.16.20.63 ...
host004.dev.sb etcd2 172.16.20.64 ...

3. 步骤

3.1 放行端口

分别放行 4001, 2380, 2379

3.2 docker-compose 文件

docker-compose.etcd0.yml

复制代码
x-variables:
  flag_initial_cluster_token: &flag_initial_cluster_token '--initial-cluster-token=etcd-cluster'
  common_settings: &common_settings
      image: quay.io/coreos/etcd:v3.5.16
      entrypoint: /usr/local/bin/etcd
      ports:
        - 4001:4001
        - 2380:2380
        - 2379:2379
          
services:
  etcd0:
    container_name: etcd0
    restart: always
    <<: *common_settings
    command:
      - '--name=etcd0'
      - '--advertise-client-urls=http://172.16.20.20:2379,http://172.16.20.20:4001'
      - '--listen-client-urls=http://0.0.0.0:2379,http://0.0.0.0:4001'
      - '--initial-advertise-peer-urls=http://172.16.20.20:2380'
      - '--listen-peer-urls=http://0.0.0.0:2380'
      - '--initial-cluster=etcd0=http://172.16.20.20:2380,etcd1=http://172.16.20.63:2380,etcd2=http://172.16.20.64:2380'
      - '--initial-cluster-state=new'
    logging:
      driver: "json-file"
      options:
        max-size: "1m"
        max-file: "5"

docker-compose.etcd1.yml

复制代码
x-variables:
  flag_initial_cluster_token: &flag_initial_cluster_token '--initial-cluster-token=etcd-cluster'
  common_settings: &common_settings
      image: quay.io/coreos/etcd:v3.5.16
      entrypoint: /usr/local/bin/etcd
      ports:
        - 4001:4001
        - 2380:2380
        - 2379:2379
          
services:
  etcd1:
    container_name: etcd1
    restart: always
    <<: *common_settings
    command:
      - '--name=etcd1'
      - '--advertise-client-urls=http://172.16.20.63:2379,http://172.16.20.63:4001'
      - '--listen-client-urls=http://0.0.0.0:2379,http://0.0.0.0:4001'
      - '--initial-advertise-peer-urls=http://172.16.20.63:2380'
      - '--listen-peer-urls=http://0.0.0.0:2380'
      - '--initial-cluster=etcd0=http://172.16.20.20:2380,etcd1=http://172.16.20.63:2380,etcd2=http://172.16.20.64:2380'
      - '--initial-cluster-state=new'
    logging:
      driver: "json-file"
      options:
        max-size: "1m"
        max-file: "5"

docker-compose.etcd2.yml

复制代码
x-variables:
  flag_initial_cluster_token: &flag_initial_cluster_token '--initial-cluster-token=etcd-cluster'
  common_settings: &common_settings
      image: quay.io/coreos/etcd:v3.5.16
      entrypoint: /usr/local/bin/etcd
      ports:
        - 4001:4001
        - 2380:2380
        - 2379:2379
          
services:
  etcd2:
    container_name: etcd2
    restart: always
    <<: *common_settings
    command:
      - '--name=etcd2'
      - '--advertise-client-urls=http://172.16.20.64:2379,http://172.16.20.64:4001'
      - '--listen-client-urls=http://0.0.0.0:2379,http://0.0.0.0:4001'
      - '--initial-advertise-peer-urls=http://172.16.20.64:2380'
      - '--listen-peer-urls=http://0.0.0.0:2380'
      - '--initial-cluster=etcd0=http://172.16.20.20:2380,etcd1=http://172.16.20.63:2380,etcd2=http://172.16.20.64:2380'
      - '--initial-cluster-state=new'
    logging:
      driver: "json-file"
      options:
        max-size: "1m"
        max-file: "5"
3.3 部署到3台服务器

服务拉起来:

3.4 相关命令
复制代码
# 列出节点成员
docker exec etcd0 etcdctl --write-out=table member list

# 查看节点状态
docker exec etcd0 etcdctl --write-out=table endpoint status

# 查看节点健康
docker exec etcd0 etcdctl --write-out=table endpoint health

# 在etcd0中放入KV
docker exec etcd0 etcdctl put name ben

# 在etcd1中获取
docker exec etcd1 etcdctl get name

4. 参考

相关推荐
阿里云云原生41 分钟前
Agent 不再是“玩具”!AgentScope Java 2.0 GA 发布:构建企业级分布式智能体底座
java·开发语言·分布式·agentscope
BaiduPHP1 小时前
docker异常断电,出现docker服务,启动失败
docker·容器·eureka
时代的狂2 小时前
RabbitMQ 面试问答
分布式·面试·rabbitmq
hkNaruto3 小时前
【大数据】《传统Hadoop大数据技术入门:补充与进阶——从数据格式到智能决策的问答实录》
大数据·hadoop·分布式
ThanksGive3 小时前
TurboLock:从 v1 Redis 分布式锁到 v2 Go 并发控制中间件
分布式·后端
寒冰碧海3 小时前
大模型部署从0到1(三):单机多卡 vLLM 部署Qwen3.6实战|Docker Compose 一键启动
docker·容器·vllm
chexus5 小时前
Docker 镜像瘦身:使用 Maven 拆分 Jar 包
docker·maven·jar
梦梦代码精5 小时前
智能体应用开发平台技术选型:一个自带商业模块的开源方案调研
人工智能·docker·开源
潘正翔15 小时前
docker核心概念
linux·运维·服务器·docker·容器·centos·运维开发
韩楚风15 小时前
【参天引擎】事务生命周期 / MVCC / Undo / ACID / 分布式事务 功能域整体解析
数据库·分布式·mysql·架构·cantian