docker部署etcd集群

2023-11-29 18:52:12 浏览数 (1)

docker部署etcd集群

etcd版本:v3.5.5

创建etcd数据目录

代码语言:shell复制
mkdir -p etcd-node{1,2,3}

创建docker网络

代码语言:shell复制
docker network create --driver bridge --subnet 172.20.0.0/16 --gateway 172.20.0.1 etcd-cluster

docker-compose.yml

代码语言:yaml复制
version: '3'

networks:
  etcd-cluster:
    external: true

services:
  etcd-node1:
    image: quay.io/coreos/etcd:v3.5.5
    container_name: etcd-node1
    ports:
      - "12379:2379"
      - "12380:2380"
    restart: always
    volumes:
      - ./etcd-node1:/data/app/etcd
    command: etcd --name etcd-node1 --data-dir /data/app/etcd/ --advertise-client-urls http://172.20.0.10:2379 --initial-advertise-peer-urls http://172.20.0.10:2380 --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 "etcd-node1=http://172.20.0.10:2380,etcd-node2=http://172.20.0.11:2380,etcd-node3=http://172.20.0.12:2380" --initial-cluster-state new
    networks:
      etcd-cluster:
        ipv4_address: 172.20.0.10

  etcd-node2:
    image: quay.io/coreos/etcd:v3.5.5
    container_name: etcd-node2
    ports:
      - "22379:2379"
      - "22380:2380"
    restart: always
    volumes:
      - ./etcd-node2:/data/app/etcd
    command: etcd --name etcd-node2 --data-dir /data/app/etcd/ --advertise-client-urls http://172.20.0.11:2379 --initial-advertise-peer-urls http://172.20.0.11:2380 --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 "etcd-node1=http://172.20.0.10:2380,etcd-node2=http://172.20.0.11:2380,etcd-node3=http://172.20.0.12:2380" --initial-cluster-state new
    networks:
      etcd-cluster:
        ipv4_address: 172.20.0.11

  etcd-node3:
    image: quay.io/coreos/etcd:v3.5.5
    container_name: etcd-node3
    ports:
      - "32379:2379"
      - "32380:2380"
    restart: always
    volumes:
      - ./etcd-node3:/data/app/etcd
    command: etcd --name etcd-node3 --data-dir /data/app/etcd/ --advertise-client-urls http://172.20.0.12:2379 --initial-advertise-peer-urls http://172.20.0.12:2380 --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 "etcd-node1=http://172.20.0.10:2380,etcd-node2=http://172.20.0.11:2380,etcd-node3=http://172.20.0.12:2380" --initial-cluster-state new
    networks:
      etcd-cluster:
        ipv4_address: 172.20.0.12

启动集群

代码语言:shell复制
docker-compose up -d

验证集群

代码语言:shell复制
docker exec -it etcd-node1 bash
etcdctl member list -w table
代码语言:shell复制
etcdctl --endpoints=172.20.0.10:2379,172.20.0.11:2379,172.20.0.12:2379 endpoint status -w table

可以看出etcd-node2为leader。

读写数据

代码语言:shell复制
etcdctl put /foo bar
etcdctl get /foo

任意节点写入,任意节点都可以读取。

节点故障测试1

停掉etcd-node1节点(非leader)。进行读写测试。

恢复etcd-node1节点。进行读写测试。

节点故障测试2

停掉etcd-node2节点(leader)。观察新leader。进行读写测试。

恢复etcd-node2节点。进行读写测试。

添加节点

etcd4.yml

创建数据目录

代码语言:shell复制
mkdir etcd-node4
代码语言:yaml复制
version: '3'

networks:
  etcd-cluster:
    external: true

services:
  etcd-node4:
    image: quay.io/coreos/etcd:v3.5.5
    container_name: etcd-node4
    ports:
      - "42379:2379"
      - "42380:2380"
    restart: always
    volumes:
      - ./etcd-node4:/data/app/etcd
    command: etcd --name etcd-node4 --data-dir /data/app/etcd/ --advertise-client-urls http://172.20.0.13:2379 --initial-advertise-peer-urls http://172.20.0.13:2380 --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 "etcd-node1=http://172.20.0.10:2380,etcd-node2=http://172.20.0.11:2380,etcd-node3=http://172.20.0.12:2380,etcd-node4=http://172.20.0.13:2380" --initial-cluster-state existing
    networks:
      etcd-cluster:
        ipv4_address: 172.20.0.13

启动:

代码语言:shell复制
docker-compose -f etcd4.yml up -d

进入任意容器执行:

代码语言:txt复制
etcdctl member add etcd-node4 --peer-urls=http://172.20.0.13:2380

重启容器:

代码语言:txt复制
docker-compose -f etcd4.yml stop
docker-compose -f etcd4.yml start

验证集群。

删除节点

代码语言:shell复制
etcdctl member remove b7476e957868d2df
Member b7476e957868d2df removed from cluster fcb94ddca70b9db2

etcdctl member list -w table

etcdctl --endpoints=172.20.0.10:2379,172.20.0.11:2379,172.20.0.12:2379 endpoint status -w table

0 人点赞