Docker 部署 Etcd 集群

拉取镜像

选择一个镜像拉取

1
2
3
4
5
6
7
docker pull gcr.io/etcd-development/etcd:v3.5.0

docker pull quay.io/coreos/etcd:v3.5.0

docker pull bitnami/etcd:latest

docker pull k8s.gcr.io/etcd:3.4.13-0

运行容器

使用gcr.io/etcd-development/etcd:v3.5.0镜像启动容器

 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
rm -rf /tmp/etcd-data.tmp && mkdir -p /tmp/etcd-data.tmp && \
  docker rmi gcr.io/etcd-development/etcd:v3.5.0 || true && \
  docker run \
  -p 2379:2379 \
  -p 2380:2380 \
  --mount type=bind,source=/tmp/etcd-data.tmp,destination=/etcd-data \
  --name etcd-gcr-v3.5.0 \
  gcr.io/etcd-development/etcd:v3.5.0 \
  /usr/local/bin/etcd \
  --name s1 \
  --data-dir /etcd-data \
  --listen-client-urls http://0.0.0.0:2379 \
  --advertise-client-urls http://0.0.0.0:2379 \
  --listen-peer-urls http://0.0.0.0:2380 \
  --initial-advertise-peer-urls http://0.0.0.0:2380 \
  --initial-cluster s1=http://0.0.0.0:2380 \
  --initial-cluster-token tkn \
  --initial-cluster-state new \
  --log-level info \
  --logger zap \
  --log-outputs stderr

# 进入容器执行指令
docker exec etcd-gcr-v3.5.0 /bin/sh -c "/usr/local/bin/etcd --version"
docker exec etcd-gcr-v3.5.0 /bin/sh -c "/usr/local/bin/etcdctl version"
docker exec etcd-gcr-v3.5.0 /bin/sh -c "/usr/local/bin/etcdctl endpoint health"
docker exec etcd-gcr-v3.5.0 /bin/sh -c "/usr/local/bin/etcdctl put foo bar"
docker exec etcd-gcr-v3.5.0 /bin/sh -c "/usr/local/bin/etcdctl get foo"
docker exec etcd-gcr-v3.5.0 /bin/sh -c "/usr/local/bin/etcdutl version"

使用bitnami/etcd:latest镜像启动容器

 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
# Create a network
$ docker network create app-tier --driver bridge

# Launch the etcd server instance
$ docker run -d --name etcd-server \
    --network app-tier \
    --publish 2379:2379 \
    --publish 2380:2380 \
    --env ALLOW_NONE_AUTHENTICATION=yes \
    --env ETCD_ADVERTISE_CLIENT_URLS=http://etcd-server:2379 \
    bitnami/etcd:latest


# Launch your etcd client instance
$ docker run -it --rm \
    --network app-tier \
    --env ALLOW_NONE_AUTHENTICATION=yes \
    bitnami/etcd:latest etcdctl --endpoints http://etcd-server:2379 put /message Hello
etcd 07:54:56.33
etcd 07:54:56.33 Welcome to the Bitnami etcd container
etcd 07:54:56.34 Subscribe to project updates by watching https://github.com/bitnami/bitnami-docker-etcd
etcd 07:54:56.34 Submit issues and feature requests at https://github.com/bitnami/bitnami-docker-etcd/issues
etcd 07:54:56.34

OK

$ docker run -it --rm \
    --network app-tier \
    --env ALLOW_NONE_AUTHENTICATION=yes \
    bitnami/etcd:latest etcdctl --endpoints http://etcd-server:2379 get /message
etcd 07:55:08.47
etcd 07:55:08.47 Welcome to the Bitnami etcd container
etcd 07:55:08.48 Subscribe to project updates by watching https://github.com/bitnami/bitnami-docker-etcd
etcd 07:55:08.48 Submit issues and feature requests at https://github.com/bitnami/bitnami-docker-etcd/issues
etcd 07:55:08.48

/message
Hello

# stop the etcd server
$ docker stop etcd-server
$ docker rm etcd-server
$ docker network rm app-tier

创建etcd集群

创建myapp/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
version: '2'

services:
  etcd1:
    image: bitnami/etcd:latest
    environment:
      - ALLOW_NONE_AUTHENTICATION=yes
      - ETCD_NAME=etcd1
      - ETCD_INITIAL_ADVERTISE_PEER_URLS=http://etcd1:2380
      - ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
      - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
      - ETCD_ADVERTISE_CLIENT_URLS=http://etcd1:2379
      - ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
      - ETCD_INITIAL_CLUSTER=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
      - ETCD_INITIAL_CLUSTER_STATE=new
  etcd2:
    image: bitnami/etcd:latest
    environment:
      - ALLOW_NONE_AUTHENTICATION=yes
      - ETCD_NAME=etcd2
      - ETCD_INITIAL_ADVERTISE_PEER_URLS=http://etcd2:2380
      - ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
      - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
      - ETCD_ADVERTISE_CLIENT_URLS=http://etcd2:2379
      - ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
      - ETCD_INITIAL_CLUSTER=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
      - ETCD_INITIAL_CLUSTER_STATE=new
  etcd3:
    image: bitnami/etcd:latest
    environment:
      - ALLOW_NONE_AUTHENTICATION=yes
      - ETCD_NAME=etcd3
      - ETCD_INITIAL_ADVERTISE_PEER_URLS=http://etcd3:2380
      - ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
      - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
      - ETCD_ADVERTISE_CLIENT_URLS=http://etcd3:2379
      - ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
      - ETCD_INITIAL_CLUSTER=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
      - ETCD_INITIAL_CLUSTER_STATE=new

启动etcd集群

docker-compose.yaml目录下使用docker-compose up命令创建集群

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
> docker-compose -f docker-compose.yaml up -d
Creating network "myapp_default" with the default driver
Creating myapp_etcd1_1 ... done
Creating myapp_etcd3_1 ... done
Creating myapp_etcd2_1 ... done

> docker-compose ps
    Name                   Command               State         Ports
---------------------------------------------------------------------------
myapp_etcd1_1   /opt/bitnami/scripts/etcd/ ...   Up      2379/tcp, 2380/tcp
myapp_etcd2_1   /opt/bitnami/scripts/etcd/ ...   Up      2379/tcp, 2380/tcp
myapp_etcd3_1   /opt/bitnami/scripts/etcd/ ...   Up      2379/tcp, 2380/tcp

停止etcd集群

1
2
3
4
5
6
7
8
> docker-compose down
Stopping myapp_etcd2_1 ... done
Stopping myapp_etcd3_1 ... done
Stopping myapp_etcd1_1 ... done
Removing myapp_etcd2_1 ... done
Removing myapp_etcd3_1 ... done
Removing myapp_etcd1_1 ... done
Removing network myapp_default

使用quay.io/coreos/etcd:v3.5.0镜像创建

  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
version: "3"
services:

  node1:
    image: quay.io/coreos/etcd
    volumes:
      - /var/local/etcd-data/node1-data:/etcd-data
    expose:
      - 2379
      - 2380
    ports:
      - "22379:2379"
      - "22380:2380"     
    networks:
      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
    privileged: true

  node2:
    image: quay.io/coreos/etcd
    volumes:
      - /var/local/etcd-data/node2-data:/etcd-data
    networks:
      cluster_net:
        ipv4_address: 172.16.238.101
    environment:
      - ETCDCTL_API=3
    expose:
      - 2379
      - 2380
    ports:
      - "22381:2379"
      - "22382: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
    privileged: true

  node3:
    image: quay.io/coreos/etcd
    volumes:
      - /var/local/etcd-data/node3-data:/etcd-data
    networks:
      cluster_net:
        ipv4_address: 172.16.238.102
    environment:
      - ETCDCTL_API=3
    expose:
      - 2379
      - 2380
    ports:
      - "22383:2379"
      - "22384: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
    privileged: true

volumes:
  node1-data: 
  node2-data:
  node3-data:

networks:
  cluster_net:
    driver: bridge
    ipam:
      driver: default
      config:
      -
        subnet: 172.16.238.0/24

启动etcd集群

1
> docker-compose -f docker-compose.yml -p etcd up -d

参考