Docker搭建etcd集群(亲测)

笔者测试环境为Centos7.5 64bit

1、容器安装etcd

#yum install etcd -y    //centos7的镜像直接用yum安装即可

#etcd -version           //进入容器查看etcd版本,是否安装成功

 

2、Etcd部署

2.1、开放etcd通信使用的接口

#firewall-cmd --zone=public --add-port=2379/tcp --permanent

#firewall-cmd --zone=public --add-port=2380/tcp --permanent

#firewall-cmd --reload

#firewall-cmd --list-ports        //查看已经打开的所有端口

#mkdir -p /var/data/etcd

 

扫描二维码关注公众号,回复: 10119744 查看本文章

Etcd启动参数说明及注意事项:

--name etcd0 //节点名称,默认为 default

--data-dir //服务运行数据保存的路径,默认为${name}.etcd

--snapshot-count //指定有多少事务(transaction)被提交时,触发截取快照保存到磁盘

--heartbeat-interval //leader 多久发送一次心跳到 followers。默认值是 100ms

--eletion-timeout //重新投票的超时时间,如果 follow 在该时间间隔没有收到心跳包,会触发重新投票,默认为 1000 ms。

--listen-peer-urls  //和同伴通信的地址,比如http://ip:2380,如果有多个,使用逗号分隔。需要所有节点都能够访问,所以不要使用 localhost

--listen-client-urls //对外提供服务的地址:比如http://ip:2379,http://127.0.0.1:2379,客户端会连接到这里和 etcd 交互

--advertise-client-urls:对外公告的该节点客户端监听地址,这个值会告诉集群中其他节点

--initial-advertise-peer-urls http://192.168.2.55:2380 //其他member使用,其他member通过该地址与本member交互信息。一定要保证从其他member能可访问该地址。静态配置方式下,该参数的value一定要同时在--initial-cluster参数中存在。

memberID的生成受--initial-cluster-token和--initial-advertise-peer-urls影响。该节点同伴监听地址,这个值会告诉集群中其他节点

--initial-cluster //集群中所有节点的信息,格式为node1=http://ip1:2380,node2=http://ip2:2380,…,注意:这里的 node1 是节点的 --name 指定的名字;后面的 ip1:2380 是 --initial-advertise-peer-urls 指定的值。

--initial-cluster-state //新建集群的时候,这个值为 new;假如已经存在的集群,这个值为 existing。

--initial-cluster-token//创建集群的 token,这个值每个集群保持唯一。这样的话,如果你要重新创建集群,即使配置和之前一样,也会再次生成新的集群和节点 uuid;否则会导致多个集群之间的冲突,造成未知的错误。

--initial-cluster-token etcd-cluster-2    //用于区分不同集群。本地如有多个集群要设为不同。

--initial-cluster-state new    //用于指示本次是否为新建集群。有两个取值new和existing。如果填为existing,则该member启动时会尝试与其他member交互。集群初次建立时,要填为new,经尝试最后一个节点填existing也正常,其他节点不能填为existing。集群运行过程中,一个member故障后恢复时填为existing,经尝试填为new也正常。

 

上述配置也可以设置配置文件,默认为/etc/etcd/etcd.conf

 

#docker run -ti --net mynetwork --ip 192.168.0.10 --name etcd1 -p 2379:2379 -p 2380:2380 d5d21b79319a /bin/bash

 

#docker run -ti --net mynetwork --ip 192.168.0.10 --name etcd1 -v /root/jiezisoft:/root/jiezisoft --privileged=true -p 2379:2379 -p 2380:2380 -p 9901:9901 -p 9902:9902 -p 9903:9903 -p 9904:9904 d5d21b79319a /bin/bash

 

#docker create -it --net mynetwork --ip 192.168.0.10 --name etcd1 -v /root/jiezisoft:/root/jiezisoft/ --privileged=true -p 2379:2379 -p 2380:2380 -p 9901:9901 -p 9902:9902 -p 9903:9903 -p 9904:9904 d5d21b79319a bash

 

#etcd --name node1 --initial-advertise-peer-urls http://192.168.0.10:2380 --listen-peer-urls http://192.168.0.10:2380 --listen-client-urls http://192.168.0.10:2379,http://127.0.0.1:2379 --advertise-client-urls http://192.168.0.10:2379 --initial-cluster-token etcd-cluster-1 --initial-cluster node1=http://192.168.0.10:2380,node2=http://192.168.0.20:2480,node3=http://192.168.2.10:2380 --initial-cluster-state new

 

#etcd --name node1 --initial-advertise-peer-urls http://192.168.0.10:2380 --listen-peer-urls http://192.168.0.10:2380 --listen-client-urls http://192.168.0.10:2379,http://127.0.0.1:2379 --advertise-client-urls http://192.168.0.10:2379 --initial-cluster-token etcd-cluster-1 --initial-cluster node1=http://192.168.0.10:2380,node2=http://192.168.0.20:2480,node3=http://192.168.2.10:2380,node4=http://192.168.2.20:2480 --initial-cluster-state new

 

#etcd --name node2 --initial-advertise-peer-urls http://192.168.0.20:2480 --listen-peer-urls http://192.168.0.20:2480 --listen-client-urls http://192.168.0.20:2479,http://127.0.0.1:2479 --advertise-client-urls http://192.168.0.20:2479 --initial-cluster-token etcd-cluster-1 --initial-cluster node1=http://192.168.0.10:2380,node2=http://192.168.0.20:2480,node3=http://192.168.2.10:2380,node4=http://192.168.2.20:2480 --initial-cluster-state new

 

#etcd --name node3 --initial-advertise-peer-urls http://192.168.2.10:2380 --listen-peer-urls http://192.168.2.10:2380 --listen-client-urls http://192.168.2.10:2379,http://127.0.0.1:2379 --advertise-client-urls http://192.168.2.10:2379 --initial-cluster-token etcd-cluster-1 --initial-cluster node1=http://192.168.0.10:2380,node2=http://192.168.0.20:2480,node3=http://192.168.2.10:2380,node4=http://192.168.2.20:2480 --initial-cluster-state new

 

#etcd --name node4 --initial-advertise-peer-urls http://192.168.2.20:2480 --listen-peer-urls http://192.168.2.20:2480 --listen-client-urls http://192.168.2.20:2479,http://127.0.0.1:2479 --advertise-client-urls http://192.168.2.20:2479 --initial-cluster-token etcd-cluster-1 --initial-cluster node1=http://192.168.0.10:2380,node2=http://192.168.0.20:2480,node3=http://192.168.2.10:2380,node4=http://192.168.2.20:2480 --initial-cluster-state new

 

 

#etcd --name etcd0 --initial-advertise-peer-urls http://192.168.0.10:2380 --listen-peer-urls http://0.0.0.0:2380 --listen-client-urls http://0.0.0.0:2379 --advertise-client-urls http://192.168.0.10:2379 --initial-cluster-token etcd-cluster-2 --initial-cluster etcd0=http://192.168.0.10:2380,etcd1=http://192.168.0.20:2480,etcd2=http://192.168.2.10:2380 --initial-cluster-state new

 

#etcdctl member list   //查看集群成员

#vi /etc/etcd/etcd.conf     //修改etcd的配置文件

#etcdctl member add node4 http://192.168.2.20:2480   //添加节点,然后再启动节点

 

 

静态ip:

A主机虚拟机172.16.1.117:

#docker network create --subnet=192.168.0.0/24 mynetwork  //A主机,一定不能和宿主机同网段

#docker network ls

#docker run -it --name CONTAINERNAME --net mynetwork --ip 192.168.0.10 IMAGEID /bin/bash

#route add -net 192.168.2.0/24 gw 172.16.1.118   //添加路由

 

B主机虚拟机172.16.1.118

#docker network create --subnet=192.168.2.0/24 mynetwork  //B主机,一定不能和宿主机同网段

#docker network ls

#docker run -it --name CONTAINERNAME --net mynetwork --ip 192.168.2.10 IMAGEID /bin/bash   

#route add -net 192.168.0.0/24 gw 172.16.1.117    //添加路由

 

 

打开端口

#systemctl start firewalld

#firewall-cmd --query-port=2379/tcp   //查询2379端口是否开启

#firewall-cmd --zone=public --add-port=2379/tcp --permanent  //永久开启2379端口
#firewall-cmd --reload   //重启防火墙生效

 

自动启动etcd脚本:

注意:用host网络就是使用宿主机网络+端口,在主备机模式测试时,需要改一下IP="10.0.0.10",和initial-cluster-token etcd-cluster-1,保证主备机IP不一样和集群ID不一样。

#!/bin/bash

 

IP="10.0.0.10"

CLUSTER="s11=http://${IP}:2380,s12=http://${IP}:2480,s13=http://${IP}:2580"

 

nohup /usr/bin/etcd --name s11 --initial-advertise-peer-urls http://${IP}:2380 \

  --listen-peer-urls http://${IP}:2380 \

  --listen-client-urls http://${IP}:2379,http://127.0.0.1:2379 \

  --advertise-client-urls http://${IP}:2379,http://127.0.0.1:2379 \

  --initial-cluster-token etcd-cluster-1 \

  --initial-cluster ${CLUSTER} \

  --initial-cluster-state new > /dev/null 2>&1 &

 

 

nohup /usr/bin/etcd --name s12 --initial-advertise-peer-urls http://${IP}:2480 \

  --listen-peer-urls http://${IP}:2480 \

  --listen-client-urls http://${IP}:2479,http://127.0.0.1:2479 \

  --advertise-client-urls http://${IP}:2479,http://127.0.0.1:2479 \

  --initial-cluster-token etcd-cluster-1 \

  --initial-cluster ${CLUSTER} \

  --initial-cluster-state new > /dev/null 2>&1 &

 

 

nohup /usr/bin/etcd --name s13 --initial-advertise-peer-urls http://${IP}:2580 \

  --listen-peer-urls http://${IP}:2580 \

  --listen-client-urls http://${IP}:2579,http://127.0.0.1:2579 \

  --advertise-client-urls http://${IP}:2579,http://127.0.0.1:2579 \

  --initial-cluster-token etcd-cluster-1 \

  --initial-cluster ${CLUSTER} \

  --initial-cluster-state new > /dev/null 2>&1 &

 

 

3、Centos6安装etcd

#yum -y update nss -y

# curl -L  https://github.com/coreos/etcd/releases/download/v3.2.22/etcd-v3.2.22-linux-amd64.tar.gz -o etcd-v3.2.22-linux-amd64.tar.gz

#tar xzvf etcd-v3.2.22-linux-amd64.tar.gz

#cd etcd-v3.2.22-linux-amd64

#cp etcd /bin

#cp etcdctl /bin

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

发布了32 篇原创文章 · 获赞 5 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_28903377/article/details/88896673
今日推荐