2. 搭建 Etcd 单节点和集群

203 阅读2分钟

docker命令就不解释了,从官网上copy下来的,都是一些内部的配置信息

需要挂载和端口暴露的docker基础

单机单实例

安装命令

docker run \
  -d \
  --name etcd \
  -p 2379:2379 \
  -p 2380:2380 \
  --volume=/var/etcd:/etcd-data \
  quay.io/coreos/etcd:v3.3.13 \
  /usr/local/bin/etcd \
  --name my-etcd-1 \
  --data-dir /etcd-data \
  --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 my-etcd-1=http://0.0.0.0:2380 \
  --initial-cluster-token my-etcd-token \
  --initial-cluster-state new \
  --auto-compaction-retention 1 \
  --auto-compaction-mode periodic

etcdctl 工具 先进入容器

~ # etcdctl set name cfd
cfd
~ # etcdctl get name
cfd

~ # etcdctl --endpoints http://localhost:2379 set name cfd
cfd
~ # etcdctl --endpoints http://localhost:2379 get name
cfd

单机多实例

默认使用:

  • 2379 - 服务接口
  • 2380 - 通信监听端口

多实例配置端口如下,按照上面的 docker 安装命令修改 将192.168.159.128配置为本机ip,0.0.0.0不变 如果出错了需要重新配置,需要停止容器并删除,再把挂载的目录也删掉,运行命令

1237912380
2237922380
3237932380
docker run \
  -d \
  --name my-etcd-2 \
  -p 12379:12379 \
  -p 12380:12380 \
  --volume=/home/cfd/env/etcd/2:/etcd-data \
  quay.io/coreos/etcd:v3.3.13 \
  /usr/local/bin/etcd \
  --name my-etcd-2 \
  --data-dir /etcd-data \
  --listen-client-urls http://0.0.0.0:12379 \
  --advertise-client-urls http://192.168.159.128:12379 \
  --listen-peer-urls http://0.0.0.0:12380 \
  --initial-advertise-peer-urls http://192.168.159.128:12380 \
  --initial-cluster 'my-etcd-2=http://192.168.159.128:12380,my-etcd-3=http://192.168.159.128:22380,my-etcd-4=http://192.168.159.128:32380' \
  --initial-cluster-token my-etcd-token \
  --initial-cluster-state new \
  --auto-compaction-retention 1 \
  --auto-compaction-mode periodic

docker run \
  -d \
  --name my-etcd-3 \
  -p 22379:22379 \
  -p 22380:22380 \
  --volume=/home/cfd/env/etcd/3:/etcd-data \
  quay.io/coreos/etcd:v3.3.13 \
  /usr/local/bin/etcd \
  --name my-etcd-3 \
  --data-dir /etcd-data \
  --listen-client-urls http://0.0.0.0:22379 \
  --advertise-client-urls http://192.168.159.128:22379 \
  --listen-peer-urls http://0.0.0.0:22380 \
  --initial-advertise-peer-urls http://192.168.159.128:22380 \
  --initial-cluster 'my-etcd-2=http://192.168.159.128:12380,my-etcd-3=http://192.168.159.128:22380,my-etcd-4=http://192.168.159.128:32380' \
  --initial-cluster-token my-etcd-token \
  --initial-cluster-state new \
  --auto-compaction-retention 1 \
  --auto-compaction-mode periodic

docker run \
  -d \
  --name my-etcd-4 \
  -p 32379:32379 \
  -p 32380:32380 \
  --volume=/home/cfd/env/etcd/4:/etcd-data \
  quay.io/coreos/etcd:v3.3.13 \
  /usr/local/bin/etcd \
  --name my-etcd-4 \
  --data-dir /etcd-data \
  --listen-client-urls http://0.0.0.0:32379 \
  --advertise-client-urls http://192.168.159.128:32379 \
  --listen-peer-urls http://0.0.0.0:32380 \
  --initial-advertise-peer-urls http://192.168.159.128:32380 \
  --initial-cluster 'my-etcd-2=http://192.168.159.128:12380,my-etcd-3=http://192.168.159.128:22380,my-etcd-4=http://192.168.159.128:32380' \
  --initial-cluster-token my-etcd-token \
  --initial-cluster-state new \
  --auto-compaction-retention 1 \
  --auto-compaction-mode periodic
  

etcd关键参数说明

进入容器后查看连接列表

# docker exec -it my-etcd-2 /bin/sh

# etcdctl --endpoints=192.168.159.128:12379 member list
parse 192.168.159.128:12379: first path segment in URL cannot contain colon
# etcdctl --endpoints=http://192.168.159.128:12379 member list
731ef0275af38ce7: name=my-etcd-3 peerURLs=http://192.168.159.128:22380 clientURLs=http://192.168.159.128:22379 isLeader=false
7f396b01e384c118: name=my-etcd-4 peerURLs=http://192.168.159.128:32380 clientURLs=http://192.168.159.128:32379 isLeader=false
fccae47c7ce36cc6: name=my-etcd-2 peerURLs=http://192.168.159.128:12380 clientURLs=http://192.168.159.128:12379 isLeader=true
# 三号节点是leader

简单使用etcdctl

[root@iZ2vcd6bealyr76cgiidyvZ var]# docker exec -it my-etcd-2 /bin/sh
/ # etcdctl --endpoints=http://172.27.3.40:12379 set name cfd
cfd
/ # etcdctl --endpoints=http://172.27.3.40:12379 get name
cfd