安装etcd
etcd的安装还是比较简单的
yum install wget –y
wget https://github.com/etcd-io/etcd/releases/download/v3.4.13/etcd-v3.4.13-linux-amd64.tar.gz
tar -xvf etcd-v3.4.13-linux-amd64.tar.gz
rm -rf etcd-v3.4.13-linux-amd64.tar.gz && cd etcd-v3.4.13-linux-amd64
单机启动就直接在etcd根目录下./etcd
就行了
创建三节点集群
容器启动
因为没有多台物理机,直接用多个容器,不同端口太替代吧
# 以centos7为基础镜像,内部暴露2379和2380端口,2379端口提供增删改查,2380端口节点间通信
docker run –name etcd1 –p 2379:2379 –p 2380:2380 –it centos:7 /bin/bash
docker run –name etcd1 –p 2479:2379 –p 2480:2380 –it centos:7 /bin/bash
docker run –name etcd1 –p 2579:2379 –p 2580:2380 –it centos:7 /bin/bash
启动etcd1
# initial-cluster-state必须为new,initial-cluster必须包含新建集群的所有节点
# etcd1启动时会提示etcd2和etcd3连接超时,后面这些节点启动起来就不显示了
# initial-advertise-peer-urls和initial-cluster要写宿主机ip,其他都是0.0.0.0
./etcd -name etcd1 \
-advertise-client-urls http://0.0.0.0:2379 \
-listen-client-urls http://0.0.0.0:2379 \
-listen-peer-urls http://0.0.0.0:2380 \
-initial-advertise-peer-urls http://159.75.26.246:2380 \
-initial-cluster-token etcd-cluster \
-initial-cluster "etcd1=http://159.75.26.246:2380,etcd2=http://159.75.26.246:2480,etcd3=http://159.75.26.246:2580" \
-initial-cluster-state new
启动etcd2
./etcd -name etcd2 \
-advertise-client-urls http://0.0.0.0:2379 \
-listen-client-urls http://0.0.0.0:2379 \
-listen-peer-urls http://0.0.0.0:2380 \
-initial-advertise-peer-urls http://159.75.26.246:2480 \
-initial-cluster-token etcd-cluster \
-initial-cluster "etcd1=http://159.75.26.246:2380,etcd2=http://159.75.26.246:2480,etcd3=http://159.75.26.246:2580" \
-initial-cluster-state new
启动etcd3
./etcd -name etcd3 \
-advertise-client-urls http://0.0.0.0:2379 \
-listen-client-urls http://0.0.0.0:2379 \
-listen-peer-urls http://0.0.0.0:2380 \
-initial-advertise-peer-urls http://159.75.26.246:2580 \
-initial-cluster-token etcd-cluster \
-initial-cluster "etcd1=http://159.75.26.246:2380,etcd2=http://159.75.26.246:2480,etcd3=http://159.75.26.246:2580" \
-initial-cluster-state new
查看集群状态
# 要制定集群内所有节点
./etcdctl --endpoints=159.75.26.246:2380,159.75.26.246:2480,159.75.26.246:2580, member list
2节点集群添加新的2个节点
首先说明下,2节点一般不能组成集群,至少要3个节点。但是新建集群可以任意节点数,但是集群如果有节点掉线,存活节点数少于3个则集群会失效。
etcd1和etcd2是原节点,我们新增加的节点是etcd3和etcd4,这四个节点的容器启动和上述差不多。四个节点端口映射如下:
etcd端口 | 2379 | 2380 |
---|---|---|
etcd1映射至宿主机端口 | 2379 | 2380 |
etcd2映射至宿主机端口 | 2479 | 2480 |
etcd3映射至宿主机端口 | 2579 | 2580 |
etcd4映射至宿主机端口 | 2679 | 2680 |
启动原节点
# 启动etcd1(注意initial-cluster就两个节点,新建集群就两个节点
./etcd -name etcd1 \
-advertise-client-urls http://0.0.0.0:2379 \
-listen-client-urls http://0.0.0.0:2379 \
-listen-peer-urls http://0.0.0.0:2380 \
-initial-advertise-peer-urls http://159.75.26.246:2380 \
-initial-cluster-token etcd-cluster \
-initial-cluster "etcd1=http://159.75.26.246:2380,etcd2=http://159.75.26.246:2480" \
-initial-cluster-state new
# 动etcd2
./etcd -name etcd2 \
-advertise-client-urls http://0.0.0.0:2379 \
-listen-client-urls http://0.0.0.0:2379 \
-listen-peer-urls http://0.0.0.0:2380 \
-initial-advertise-peer-urls http://159.75.26.246:2480 \
-initial-cluster-token etcd-cluster \
-initial-cluster "etcd1=http://159.75.26.246:2380,etcd2=http://159.75.26.246:2480" \
-initial-cluster-state new
增加新节点
首先要在任意原节点下,添加新增节点的基本信息,我们这里选择在etcd2下进行,也就是endpoints参数。etcd2会告诉整个集群将要加入的新节点的基本信息。
# 只能一个一个添加,这边add member后,对应新节点启动完(下面的命令),才能继续add下一新节点
./etcdctl --endpoints=159.75.26.246:2480 member add etcd3 http://159.75.26.246:2580
./etcdctl --endpoints=159.75.26.246:2480 member add etcd4 http://159.75.26.246:2680
我们进入etcd3新节点的容器,执行命令(initial-cluster是三个节点),并且initial-cluster-state是existing
./etcd -name etcd3 \
-advertise-client-urls http://0.0.0.0:2379 \
-listen-client-urls http://0.0.0.0:2379 \
-listen-peer-urls http://0.0.0.0:2380 \
-initial-advertise-peer-urls http://159.75.26.246:2580 \
-initial-cluster-token etcd-cluster \
-initial-cluster "etcd1=http://159.75.26.246:2380,etcd2=http://159.75.26.246:2480,etcd3=http://159.75.26.246:2580" \
-initial-cluster-state existing
再进入etcd4容器执行命令行,etcd3已经加进去了,所以initial-cluster是四个节点
./etcd -name etcd4 \
-advertise-client-urls http://0.0.0.0:2379 \
-listen-client-urls http://0.0.0.0:2379 \
-listen-peer-urls http://0.0.0.0:2380 \
-initial-advertise-peer-urls http://159.75.26.246:2680 \
-initial-cluster-token etcd-cluster \
-initial-cluster "etcd1=http://159.75.26.246:2380,etcd2=http://159.75.26.246:2480,etcd3=http://159.75.26.246:2580,etcd4=http://159.75.26.246:2680" \
-initial-cluster-state existing
至此2+2集群搭建完毕
评论区