对于etcd集群,如果在配置文件内配置了其他节点信息,而其他节点没有启动就会报错,进入容器执行命令也执行不了,因为他们需要严格同步,如果想只使用单节点,在docker中映射出配置文件,修改为单节点,把其他节点信息去掉用时再加上。
由于集群中的单节点可以启动,但无法和其他节点同步,所以执行命令如下错误,gui客户端也会报连接超时,就练习还是单节点。
{"level":"warn","ts":"2024-11-16T19:26:41.679994+0800","logger":"etcd-client","caller":"[email protected]/retry_interceptor.go:63","msg":"retrying of unary invoker failed","target":"etcd-endpoints://0xc0001461e0/127.0.0.1:2379","attempt":0,"error":"rpc error: code = DeadlineExceeded desc = context deadline exceeded"}
Error: context deadline exceeded
在网上各种各样的构建版本导致来源多样,直接从网上使用报错,以bitnami/etcd:latest为例,镜像的构建执行文件按配置文件可能与其他版本不同,官方推荐从网络构建镜像,但有人构建后挪动位置构建出来的不易使用。
先创建网络
docker network create app-tier --subnet 172.25.0.0/16
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
简单配置:
version: '2'
networks:
app-tier:
driver: bridge
services:
Etcd:
image: 'bitnami/etcd:latest'
environment:
- ALLOW_NONE_AUTHENTICATION=yes
- ETCD_ADVERTISE_CLIENT_URLS=http://etcd:2379
ports:
- 2379:2379
- 2380:2380
networks:
- app-tier
myapp:
image: 'YOUR_APPLICATION_IMAGE'
networks:
- app-tier
关于配置文件映射:后面的路径就是要确认的
docker run --name Etcd -v /path/to/Etcd.conf.yml:/opt/bitnami/Etcd/conf/etcd.conf.yml bitnami/etcd:latest
文件构建
Etcd:
...
volumes:
- /path/to/Etcd.conf.yml:/opt/bitnami/etcd/conf/etcd.conf.yml
...
其次是数据映射
version: '3'
services:
etcd:
container_name: etcd
image: bitnami/etcd:latest
environment:
- ALLOW_NONE_AUTHENTICATION=yes
entrypoint: [ "/opt/bitnami/etcd/bin/etcd", "--config-file", "/opt/bitnami/etcd/conf/etcd.conf.yml" ]
volumes:
- /Data/etcd/cluster/etcd1/data:/bitnami/etcd
- /Data/etcd/cluster/etcd1/conf/etcd.conf.yml:/opt/bitnami/etcd/conf/etcd.conf.yml
- "/etc/localtime:/etc/localtime:ro"
ports:
- 2379:2379
- 2380:2380
networks:
default:
name: etcd-tier
driver: bridge
重点就是容器启动后的配置文件和数据文件的路径问题和可执行文件的位置,如何不对,容器启动失败。
完整配置过程:
1 创建配置文件目录,设置权限,每个文件下配置如下:etcd.conf.yaml
# 其他node 分别将 `etcd1`改为 `etcd2`/ `etcd3`
name: etcd1
data-dir: /bitnami/etcd
# 其他node 分别下面的将`172.25.0.101`改为`172.25.0.102`/ `172.25.0.103`
listen-client-urls: http://172.25.0.101:2379, http://127.0.0.1:2379
advertise-client-urls: http://172.25.0.101:2379
listen-peer-urls: http://172.25.0.101:2380
initial-advertise-peer-urls: http://172.25.0.101:2380
# 这里之后不需要改
initial-cluster: etcd1=http://172.25.0.101:2380,etcd2=http://172.25.0.102:2380,etcd3=http://172.25.0.103:2380
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new
2 三个容器一起启动:如果启动1个由于这个节点配置了其他节点信息,会导致该节点不能正常工作如put和get甚至不能被外界连接
version: '3'
services:
etcd-1:
image: bitnami/etcd:latest
entrypoint: [ "/opt/bitnami/etcd/bin/etcd", "--config-file", "/opt/bitnami/etcd/conf/etcd.conf.yml" ]
ports:
- "23791:2379"
environment:
ETCDCTL_API: 3
volumes:
- /Data/etcd/cluster/etcd1/data:/bitnami/etcd
- /Data/etcd/cluster/etcd1/conf/etcd.conf.yml:/opt/bitnami/etcd/conf/etcd.conf.yml
- "/etc/localtime:/etc/localtime:ro"
networks:
etcd-tier:
ipv4_address: 172.25.0.101
etcd-2:
image: bitnami/etcd:latest
entrypoint: [ "/opt/bitnami/etcd/bin/etcd", "--config-file", "/opt/bitnami/etcd/conf/etcd.conf.yml" ]
ports:
- "23792:2379"
environment:
ETCDCTL_API: 3
volumes:
- /Data/etcd/cluster/etcd2/data:/bitnami/etcd
- /Data/etcd/cluster/etcd2/conf/etcd.conf.yml:/opt/bitnami/etcd/conf/etcd.conf.yml
- "/etc/localtime:/etc/localtime:ro"
networks:
etcd-tier:
ipv4_address: 172.25.0.102
etcd-3:
image: bitnami/etcd:latest
entrypoint: [ "/opt/bitnami/etcd/bin/etcd", "--config-file", "/opt/bitnami/etcd/conf/etcd.conf.yml" ]
ports:
- "23793:2379"
environment:
ETCDCTL_API: 3
volumes:
- /Data/etcd/cluster/etcd3/data:/bitnami/etcd
- /Data/etcd/cluster/etcd3/conf/etcd.conf.yml:/opt/bitnami/etcd/conf/etcd.conf.yml
- "/etc/localtime:/etc/localtime:ro"
networks:
etcd-tier:
ipv4_address: 172.25.0.103
networks:
etcd-tier:
driver: bridge
ipam:
config:
- subnet: 172.25.0.0/16
gateway: 172.25.0.1