version: "3.0"
services:
etcd:
image: bitnami/etcd:latest
container_name: etcd1
restart: always
ports:
- "2379:2379"
- "2380:2380"
environment:
- ALLOW_NONE_AUTHENTICATION=yes #示例中我们不设置密码
- ETCD_NAME=etcd1 #节点自己的名字
- ETCD_ADVERTISE_CLIENT_URLS=http://10.10.239.31:2379 #告知集群自己的客户端地址
- ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379 #设置监听客户端通讯的URL列表
- ETCD_INITIAL_ADVERTISE_PEER_URLS=http://10.10.239.31:2380 #告知集群自己集群通讯地址
- ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380 #用于监听伙伴通讯的URL列表
- ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster #etcd 集群的初始化集群记号
- ETCD_INITIAL_CLUSTER=etcd1=http://10.10.239.31:2380,etcd2=http://10.10.239.32:2380,etcd3=http://10.10.239.33:2380 #集群成员
- ETCD_INITIAL_CLUSTER_STATE=new #初始化集群状态
volumes:
- ./data:/bitnami/etc
如果此时启动,会在当前目录下创建 ./data 目录,挂载到容器的 /bitnami/etcd ,但我们要注意的是,容器中启动etcd的用户并不是root用户,很可能没有在目录下创建文件的权限。我们有两个方案:
[root@centos-01 etcd1]# docker-compose up -d
[root@centos-01 etcd1]# docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------------
etcd1 /opt/bitnami/scripts/etcd/ ... Up 0.0.0.0:2379->2379/tcp, 0.0.0.0:2380->2380/tcp
version: "3.0"
services:
etcd:
image: bitnami/etcd:latest
container_name: etcd2
restart: always
ports:
- "2379:2379"
- "2380:2380"
environment:
- ALLOW_NONE_AUTHENTICATION=yes
- ETCD_NAME=etcd2
- ETCD_ADVERTISE_CLIENT_URLS=http://10.10.239.32:2379
- ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
- ETCD_INITIAL_ADVERTISE_PEER_URLS=http://10.10.239.32:2380
- ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
- ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
- ETCD_INITIAL_CLUSTER=etcd1=http://10.10.239.31:2380,etcd2=http://10.10.239.32:2380,etcd3=http://10.10.239.33:2380
- ETCD_INITIAL_CLUSTER_STATE=new
volumes:
- ./data:/bitnami/etcd
version: "3.0"
services:
etcd:
image: bitnami/etcd:latest
container_name: etcd3
restart: always
ports:
- "2379:2379"
- "2380:2380"
environment:
- ALLOW_NONE_AUTHENTICATION=yes
- ETCD_NAME=etcd3
- ETCD_ADVERTISE_CLIENT_URLS=http://10.10.239.33:2379
- ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
- ETCD_INITIAL_ADVERTISE_PEER_URLS=http://10.10.239.33:2380
- ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
- ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
- ETCD_INITIAL_CLUSTER=etcd1=http://10.10.239.31:2380,etcd2=http://10.10.239.32:2380,etcd3=http://10.10.239.33:2380
- ETCD_INITIAL_CLUSTER_STATE=new
volumes:
- ./data:/bitnami/etcd
I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl --write-out=table member list
+------------------+---------+-------+--------------------------+--------------------------+------------+
| ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS | IS LEARNER |
+------------------+---------+-------+--------------------------+--------------------------+------------+
| 57d6f74aaa8d5d3e | started | etcd1 | http://10.10.239.31:2380 | http://10.10.239.31:2379 | false |
| 9107e491ab549324 | started | etcd3 | http://10.10.239.33:2380 | http://10.10.239.33:2379 | false |
| e7b147006e212ca5 | started | etcd2 | http://10.10.239.32:2380 | http://10.10.239.32:2379 | false |
+------------------+---------+-------+--------------------------+--------------------------+------------+
I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl --write-out=table member list
+------------------+---------+-------+--------------------------+--------------------------+------------+
| ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS | IS LEARNER |
+------------------+---------+-------+--------------------------+--------------------------+------------+
| 57d6f74aaa8d5d3e | started | etcd1 | http://10.10.239.31:2380 | http://10.10.239.31:2379 | false |
| 9107e491ab549324 | started | etcd3 | http://10.10.239.33:2380 | http://10.10.239.33:2379 | false |
| e7b147006e212ca5 | started | etcd2 | http://10.10.239.32:2380 | http://10.10.239.32:2379 | false |
+------------------+---------+-------+--------------------------+--------------------------+------------+
[root@centos-01 etcd3]# docker exec -it etcd1 bash
I have no name!@788dd01aaa79:/opt/bitnami/etcd$ etcdctl put name "kkk"
OK
[root@centos-01 etcd3]# docker exec -it etcd3 bash
I have no name!@788dd01aaa79:/opt/bitnami/etcd$ etcdctl get name
name
kkk