准备
镜像选择
quay.io/coreos/etcd:3.2.7
bitname/etcd
创建ETCD数据目录
1 2 3
|
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 data-dir: /var/etcd listen-client-urls: http://0.0.0.0:2379 advertise-client-urls: http://0.0.0.0:2379 listen-peer-urls: http://0.0.0.0:2380 initial-advertise-peer-urls: http://0.0.0.0:2380 initial-cluster: etcd=http://0.0.0.0:2380 initial-cluster-token: etcd-cluster initial-cluster-state: new logger: zap log-level: info
|
创建并启动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: 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
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
|