1.docker单机部署ETCD
undefined
docker pull quay.io/coreos/etcd
docker pull quay.io/coreos/etcd
- 设置环境变量和监听地址
undefined
-e ETCDCTL_API=3
-p 2379:2379 -p 2380:2380
-e ETCDCTL_API=3
-p 2379:2379 -p 2380:2380
- 运行
cpp
docker run -d -it --rm \
--name etcd_test \
-e ETCDCTL_API=3 \
-p 2379:2379 \
-p 2380:2380 \
quay.io/coreos/etcd \
etcd \
--advertise-client-urls http://0.0.0.0:2379 \
--listen-client-urls http://0.0.0.0:2379
docker run -d -it --rm \
--name etcd_test \
-e ETCDCTL_API=3 \
-p 2379:2379 \
-p 2380:2380 \
quay.io/coreos/etcd \
etcd \
--advertise-client-urls http://0.0.0.0:2379 \
--listen-client-urls http://0.0.0.0:2379
容器退出自动删除rm
cpp
[root@bogon etcd]# docker run -d -it --rm --name etcd_test -e ETCDCTL_API=3 -p 2379:2379 -p 2380:2380 quay.io/coreos/etcd:v3.3.9 etcd --advertise-client-urls http://0.0.0.0:2379 --listen-client-urls http://0.0.0.0:2379
ca444c094b6bf00b47726b0dee600620d3962cd0fb78d224db9fae12da94dc13
[root@bogon etcd]# docker run -d -it --rm --name etcd_test -e ETCDCTL_API=3 -p 2379:2379 -p 2380:2380 quay.io/coreos/etcd:v3.3.9 etcd --advertise-client-urls http://0.0.0.0:2379 --listen-client-urls http://0.0.0.0:2379
ca444c094b6bf00b47726b0dee600620d3962cd0fb78d224db9fae12da94dc13
容器停止总是重启
cpp
[root@ansible-server ~]# docker run -d -it --restart=always --name etcd1 -e ETCDCTL_API=3 -p 2379:2379 -p 2380:2380 quay.io/coreos/etcd:v3.3.9 etcd --advertise-client-urls http://0.0.0.0:2379 --listen-client-urls http://0.0.0.0:2379
199b521dcdd1a742701350c8024dd76c179db2ded0d05a40dd44adfd14f2fb27
[root@ansible-server ~]# docker run -d -it --restart=always --name etcd1 -e ETCDCTL_API=3 -p 2379:2379 -p 2380:2380 quay.io/coreos/etcd:v3.3.9 etcd --advertise-client-urls http://0.0.0.0:2379 --listen-client-urls http://0.0.0.0:2379
199b521dcdd1a742701350c8024dd76c179db2ded0d05a40dd44adfd14f2fb27
其他状态:
- no,默认策略,在容器退出时不重启容器
- on-failure,在容器非正常退出时(退出状态非0),才会重启容器
- on-failure:3,在容器非正常退出时重启容器,最多重启3次
- always,在容器退出时总是重启容器
- unless-stopped,在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器
2.docker集群部署
1.etcd集群
undefined
docker pull busybox
docker pull busybox
2.network
bash
#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
#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
3.cluster
csharp
# etcd1
docker run -itd --restart=always \
--network etcd_cluster \
--ip 172.16.3.31 \
--hostname etcd1 \
--name etcd1 \
-e ETCDCTL_API=3 \
-p 12379:2379 \
-p 12380: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 --restart=always \
--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 --restart=always \
--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
#测试结果如下
[root@bogon ~]# 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
[root@bogon 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
# etcd1
docker run -itd --restart=always \
--network etcd_cluster \
--ip 172.16.3.31 \
--hostname etcd1 \
--name etcd1 \
-e ETCDCTL_API=3 \
-p 12379:2379 \
-p 12380: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 --restart=always \
--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 --restart=always \
--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
#测试结果如下
[root@bogon ~]# 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
[root@bogon 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