0%

Docker部署单节点ETCD

准备

镜像选择

quay.io/coreos/etcd:3.2.7
bitname/etcd

创建ETCD数据目录

1
2
3
# data 存储容器持久化数据
# config 存储容器使用的配置文件
mkdir -p /usr/local/docker/etcd/{data,config}

创建ETCD配置文件

配置文件路径为 /usr/local/docker/etcd/config/etcd.config.yml

1
2
3
4
5
6
7
8
9
10
11
12
name: etcd # etcd member 名称,可根据实际情况修改
data-dir: /var/etcd #etcd 数据目录,可根据实际情况修改
listen-client-urls: http://0.0.0.0:2379 #client 流量监听地址,没特殊需求按文档填写即可
advertise-client-urls: http://0.0.0.0:2379 # 该 member 向外部通告的客户端 url 列表,单节点部署时不需要修改,集群部署模式需修改为容器所在节点对外提供服务的 IP
listen-peer-urls: http://0.0.0.0:2380 # peer 流量监听地址,没特殊需求按文档填写即可
initial-advertise-peer-urls: http://0.0.0.0:2380 # 该 member 向同一集群内其他 member 通告的 peer url 列表,单节点部署时不需要修改,集群部署模式需修改为容器所在节点对外提供服务的 IP
initial-cluster: etcd=http://0.0.0.0:2380 # 初始化集群节点信息,单节点部署时不需要修改,集群部署模式需要填写集群中所有 member 的信息
initial-cluster-token: etcd-cluster # 初始化集群时使用的 token,随便写
initial-cluster-state: new # 初始化集群状态,可选的值为 **new** 或者 **existing**,通常采用 **new**
logger: zap
log-level: info
# log-outputs: stderr

创建并启动ETCD服务

1
docker run -d --name etcd -p 2379:2379 -p 2380:2380 -v /usr/local/docker/etcd/data:/var/etcd -v /usr/local/docker/etcd/config:/var/lib/etcd/config quay.io/coreos/etcd: 3.5.12 

使用docker-compose部署

创建Docker-compose

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
version: '3'

services:
etcd:
container_name: etcd
image: quay.io/coreos/etcd:v3.5.12
command: /usr/local/bin/etcd --config-file=/var/lib/etcd/config/etcd.conf.yml
volumes:
- ${DOCKER_VOLUME_DIRECTORY:-.}/data:/var/lib/etcd
- ${DOCKER_VOLUME_DIRECTORY:-.}/config/etcd.config.yml:/var/lib/etcd/conf/etcd.conf.yml
ports:
- 2379:2379
- 2380:2380
restart: always

networks: # 船舰一个新的bridge模式网络,名称 etcd-tier,名称可以根据需求自定义
default:
name: etcd-tier
driver: bridge

基于环境变量配置, 配置参考配置文件方式

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
version: '3'

services:
etcd:
container_name: etcd
image: quay.io/coreos/etcd:v3.5.12
environment:
- ETCD_NAME=etcd
- ETCD_DATA_DIR=
- ETCD_LISTEN_CLIENT_URLS=
- ETCD_ADVERTISE_CLIENT_URLS=
- ETCD_LISTEN_PEER_URLS=
- ETCD_INSTALL_ADVERTISE_PEER_URLS=
- ETCD_INSTALL_CLUSTER_TOKEN=
- ETCD_INSTALL_CLUSTER
- ETCD_INSTALL_CLUSTER_STATE=new
- ETCD_LOGGER=zap
- ETCD_LOG_LEVEL=info
- ALLOW_NONE_AUTHENTICATION="yes" # 允许无身份验证访问
- TZ="Asia/Shanghai"
volumes:
- ${DOCKER_VOLUME_DIRECTORY:=.}/data:/var/etcd
- /etc/localtime:/etc/localtime:rw
ports:
- 2379:2379
- 2380:2380
restart: always
networks:
default:
name: etcd-tier
driver: bridge

创建并启动etcd

1
2
cd {docker-compose文件所在目录}
docker compose up -d

测试命令

1
2
3
4
5
6
7
8
9
10
11
12
etcdctl --endpoints=192.168.1.2:2379 --write-out=table endpoint health

etcdctl --endpoints=192.168.1.2:2379 --write-out=table endpoint status

# 查看member状态
etcdctl --endpoints=192.168.1.2:2379 --write-out=table member list

# 写入数据
etcdctl --endpoints=192.168.1.2:2379 put foo bar

# 读取数据
etcdctl --endpoints=192.168.1.2:2379 get foo