etcd集群部署

匿名 (未验证) 提交于 2019-12-03 00:27:02

转载地址:https://blog.csdn.net/yjk13703623757/article/details/54142242

简介
Etcd是一个高可用的 Key/Value 存储系统,主要用于分享配置和服务发现。
● 简单:支持 curl 方式的用户 API (HTTP+JSON)
● 安全:可选 SSL 客户端证书认证
● 快速:单实例可达每秒1000次写操作
● 可靠:使用 Raft 实现分布式

环境:

node IP OS etcd_version
etcd0 10.1.2.61 centos7.0 etcd3.0.14
etcd1 10.1.2.172 centos7.0 etcd3.0.14
etcd2 10.1.2.173 centos7.0 etcd3.0.14

一、在各节点(etcd0、etcd1、etcd2)上安装etcd
下载etcd源码包:https://github.com/coreos/etcd/releases,这里用的是etcd-v3.0.14-linux-amd64.tar.gz,解压并添加etcd命令至环境变量。

# cd  /path/to/etcd-v3.0.14-linux-amd64.tar.gz  # tar xf  etcd-v3.0.14-linux-amd64.tar.gz  # mv etcd-v3.0.14-linux-amd64   etcd  # vim /etc/profile export  PATH=/path/to/etcd  # source  /etc/profile
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

二、配置etcd集群
目前支持三种发现方式:Static,etcd Discovery,DNS Discovery。
● Static适用于有固定IP的主机节点
● etcd Discovery适用于DHCP环境
● DNS Discovery依赖DNS SRV记录
这里我们采用Static方式,创建etcd0脚本,方便配置etcd启动参数。

# cd  /path/to/etcd # mkdir  log  data       //data为节点数据存储文件夹,log为日志文件夹  # vim  etcd0.sh          //写入以下脚本内容 #!/bin/bash   etcd  --name etcd0 --data-dir  /data/etcd/data --advertise-client-urls http://10.1.2.61:2379,http://10.1.2.61:4001 --listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 --initial-advertise-peer-urls http://10.1.2.61:2380 --listen-peer-urls http://0.0.0.0:2380 --initial-cluster-token etcd-cluster-1 --initial-cluster etcd0=http://10.1.2.61:2380,etcd1=http://10.1.2.172:2380,etcd2=http://10.1.2.173:2380 --initial-cluster-state new > ./log/etcd.log 2>&1   # nohup  ./etcd0.sh  &  # ps  -ef|grep etcd  # netstat  -lnpt|grep etcd
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

参数说明:








注意:上面的初始化只是在集群初始化时运行一次,之后节点的服务有重启,必须要去掉initial参数,否则报错。

# etcd --name  etcd0  --data-dir  /data/etcd/data  --listen-peer-urls  http://10.1.2.61:2380  --listen-client-urls  http://10.1.2.61:2379,http://127.0.0.1:2379  --advertise-client-urls  http://10.1.2.61:2379
  • 1

三、管理etcd集群
1.查看集群版本

# etcdctl  --version # etcdctl  --help
  • 1
  • 2

2.查看集群健康状态

# etcdctl cluster-health
  • 1

3.查看集群成员

# etcdctl  member  list
  • 1

在任一节点上执行,可以看到集群的节点情况,并能看出哪个是leader节点

4.更新一个节点
如果你想更新一个节点的IP(peerURLS),首先你需要知道那个节点的ID

# etcdctl member list # etcdctl member update memberID http://ip:2380
  • 1
  • 2

5.删除一个节点(Etcd集群成员的缩)

#  etcdctl  member  list #  etcdctl  member  remove  memberID #  etcdctl  member  list #  ps -ef|grep etcd          //在相关节点上kill掉etcd进程
  • 1
  • 2
  • 3
  • 4

6.增加一个新节点(Etcd集群成员的伸)
注意:步骤很重要,不然会报集群ID不匹配

# etcdctl member add --help
  • 1

a. 将目标节点添加到集群

# etcdctl member add etcd3 http://10.1.2.174:2380  Addedmember named etcd3 with ID 28e0d98e7ec15cd4 to cluster ETCD_NAME="etcd3" ETCD_INITIAL_CLUSTER="etcd0=http://10.1.2.61:2380,etcd1=http://10.1.2.172:2380,etcd2=http://10.1.2.173:2380,etcd3=http://10.1.2.174:2380" ETCD_INITIAL_CLUSTER_STATE="existing"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

b. 查看新增成员列表,etcd3状态为unstarted

# etcdctl member list d4f257d2b5f99b64[unstarted]:peerURLs=http://10.1.2.174:2380
  • 1
  • 2

c. 清空目标节点etcd3的data-dir
节点删除后,集群中的成员信息会更新,新节点是作为一个全新的节点加入集群,如果data-dir有数据,etcd启动时会读取己经存在的数据,仍然用老的memberID会造成无法加入集群,所以一定要清空新节点的data-dir。

# rm  -rf  /path/to/etcd/data
  • 1

d. 在目标节点上启动新增加的成员
这里的initial标记一定要指定为existing,如果为new,则会自动生成一个新的memberID,这和前面添加节点时生成的ID不一致,故日志中会报节点ID不匹配的错。

# vim  etcd3.sh
  • 1

# nohup   ./etcd3.sh  &  # etcdctl  member  list
  • 1
  • 2
  • 3

五、增删改查

1.在etcd1上设置一个key/value对,这时就可以在集群任意节点上获取key/value

etcd1# etcdctl set api_server  http://192.168.5.44:8080  http://192.168.5.44:8080   etcd0# etcdctl get api_server  http://192.168.5.44:8080
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2.

# etcdctl  set  foo  "bar" # etcdctl  get  foo  # etcdctl  mkdir  hello # etcdctl  ls  # etcdctl  --output  extended  get  foo # etcdctl  --output  json  get  foo  # etcdctl  update  foo  "etcd cluster is ok" # etcdctl  get foo  # etcdctl  import  --snap  /data/etcd/member/snap/db
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3.REST API

# curl  http://10.1.2.61:2379/v2/members 
  • 1

查看集群成员,其中,id是集群成员的全局唯一的身份标识,name是成员的名字,peerURLs是成员之间通信的入口,clientURLs是成员跟用户通信的访问入口

# curl   http://10.1.2.61:2379/v2/keys  # curl   -fs  -X  PUT   http://10.1.2.61:2379/v2/keys/_test  # curl   -X  GET   http://10.1.2.61:2379/v2/keys/_test
  • 1
  • 2
  • 3
  • 4
  • 5

六、常见问题
1.dial tcp 10.1.2.172:2380: getsockopt: no route to host
此为防火墙没有关闭,要关闭防火墙

# service iptables  stop # service firewalld  stop # systemctl disable firewalld
  • 1
  • 2
  • 3

2.nohup――真正的Shell后台运行

# nohup /path/to/start_etcd.sh &
  • 1
文章来源: etcd集群部署
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!