欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker Etcd cluster(docker搭建Etcd集群)

程序员文章站 2022-07-13 22:37:57
...

docker netword

docker network create etcd_cluster

docker network create --subnet 172.16.3.0/16 etcd_cluster

docker network inspect etcd_cluster

#docker network rm etcd_cluster

docker run -itd --rm --network etcd_cluster --ip 172.16.3.0 --name test busybox

etcd cluster

# etcd1
docker run -itd --rm \
    --network etcd_cluster \
    --ip 172.16.3.31 \
    --hostname etcd1 \
    --name etcd1 \
    -e ETCDCTL_API=3 \
    -p 2379:2379 \
    -p 2380:2380 \
    quay.io/coreos/etcd:v3.3.9 \
    etcd --name etcd1 \
    --initial-advertise-peer-urls http://172.16.3.31:2380 \
    --listen-peer-urls http://172.16.3.31:2380 \
    --listen-client-urls http://172.16.3.31:2379,http://127.0.0.1:2379 \
    --advertise-client-urls http://172.16.3.31:2379 \
    --initial-cluster-token etcd-cluster-1 \
    --initial-cluster etcd1=http://172.16.3.31:2380,etcd2=http://172.16.3.32:2380,etcd3=http://172.16.3.33:2380 \
    --initial-cluster-state new
    
  # etcd2  
    docker run -itd --rm \
    --network etcd_cluster \
    --ip 172.16.3.32 \
    --hostname etcd2 \
    --name etcd2 \
    -e ETCDCTL_API=3 \
    -p 22379:2379 \
    -p 22380:2380 \
    quay.io/coreos/etcd:v3.3.9 \
    etcd --name etcd2 \
    --initial-advertise-peer-urls http://172.16.3.32:2380 \
    --listen-peer-urls http://172.16.3.32:2380 \
    --listen-client-urls http://172.16.3.32:2379,http://127.0.0.1:2379 \
    --advertise-client-urls http://172.16.3.32:2379 \
    --initial-cluster-token etcd-cluster-1 \
    --initial-cluster etcd1=http://172.16.3.31:2380,etcd2=http://172.16.3.32:2380,etcd3=http://172.16.3.33:2380 \
    --initial-cluster-state new
  
  # etcd3
    docker run -itd --rm \
    --network etcd_cluster \
    --ip 172.16.3.33 \
    --hostname etcd3 \
    --name etcd3 \
    -e ETCDCTL_API=3 \
    -p 32379:2379 \
    -p 32380:2380 \
    quay.io/coreos/etcd:v3.3.9 \
    etcd --name etcd3 \
    --initial-advertise-peer-urls http://172.16.3.33:2380 \
    --listen-peer-urls http://172.16.3.33:2380 \
    --listen-client-urls http://172.16.3.33:2379,http://127.0.0.1:2379 \
    --advertise-client-urls http://172.16.3.33:2379 \
    --initial-cluster-token etcd-cluster-1 \
    --initial-cluster etcd1=http://172.16.3.31:2380,etcd2=http://172.16.3.32:2380,etcd3=http://172.16.3.33:2380 \
    --initial-cluster-state new
    
 # client
 #进入容器 
docker exec -it etcd1 bin/sh
#客户端测试
etcdctl --write-out=table --endpoints=http://127.0.0.1:2379 member list
etcdctl --endpoints=http://127.0.0.1:2379 put foo bar
etcdctl --endpoints=http://127.0.0.1:2379 get foo

#测试结果如下
[[email protected] ~]# docker exec -it etcd1 bin/sh
/ # etcdctl --write-out=table --endpoints=http://127.0.0.1:2379 member list
+------------------+---------+-------+-------------------------+-------------------------+
|        ID        | STATUS  | NAME  |       PEER ADDRS        |      CLIENT ADDRS       |
+------------------+---------+-------+-------------------------+-------------------------+
|  c26d6ba798c079c | started | etcd3 | http://172.16.3.33:2380 | http://172.16.3.33:2379 |
| 4631df2115e1ef72 | started | etcd2 | http://172.16.3.32:2380 | http://172.16.3.32:2379 |
| a92fe5422902bc40 | started | etcd1 | http://172.16.3.31:2380 | http://172.16.3.31:2379 |
+------------------+---------+-------+-------------------------+-------------------------+
/ # etcdctl --endpoints=http://127.0.0.1:2379 put foo bar
OK
/ # etcdctl --endpoints=http://127.0.0.1:2379 get foo
foo
bar
#docker ps
[[email protected] docker]# docker ps
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                                              NAMES
5988c4443eb5        quay.io/coreos/etcd:v3.3.9   "etcd --name etcd3..."   37 seconds ago      Up 36 seconds       0.0.0.0:32379->2379/tcp, 0.0.0.0:32380->2380/tcp   etcd3
d4fdda6400cc        quay.io/coreos/etcd:v3.3.9   "etcd --name etcd2..."   44 seconds ago      Up 44 seconds       0.0.0.0:22379->2379/tcp, 0.0.0.0:22380->2380/tcp   etcd2
af86e5fd2ae5        quay.io/coreos/etcd:v3.3.9   "etcd --name etcd1..."   53 seconds ago      Up 52 seconds       0.0.0.0:2379-2380->2379-2380/tcp                   etcd1