基于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. 参考

相关推荐
是枚小菜鸡儿吖14 小时前
Home Assistant跨品牌智能家居教程:Docker部署、米家接入、vivo桥接与远程访问
docker·容器·智能家居
螺蛳粉 螺蛳粉17 小时前
第 02 篇:镜像分层与 containerd 2.x 实战:SystemdCgroup 与 pause 镜像配置踩坑(底座认知 02)
docker·云原生·容器·kubernetes·etcd·containerd·kube-apiserver
wefg117 小时前
【Docker】镜像相关指令
运维·docker·容器
java_logo18 小时前
Docker 部署 DPanel:轻松搭建轻量容器管理平台
运维·docker·容器·podman·容器管理·dpanel·轩辕镜像
死磕音视频19 小时前
Open WebUI 入门:用 Docker 装上本地对话界面并连接 Ollama
docker·ollama·本地大模型·open webui
分布式存储与RustFS19 小时前
CI 老被 Docker Hub 的 429 卡住,自建 Registry 把镜像搬进内网
运维·docker·开源·镜像仓库·对象存储·ci·rustfs
NiceZack19 小时前
Hadoop 部署全流程教程:从新装 CentOS 到伪分布式与完全分布式
hadoop·分布式·centos
傻啦嘿哟19 小时前
分布式爬虫架构:Redis+Scrapy打造企业级爬虫系统
分布式·爬虫·架构
n1121219 小时前
Docker 容器时区错 8 小时:3 个坑
运维·docker·容器·故障排查·服务器运维·容器时区
慈云数据20 小时前
用 DSH 辅助部署一个网站监控平台:Uptime Kuma + Docker 实战
运维·docker·容器