etcd集群部署

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

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

环境:

nodeIPOSetcd_version
etcd010.1.2.61centos7.0etcd3.0.14
etcd110.1.2.172centos7.0etcd3.0.14
etcd210.1.2.173centos7.0etcd3.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

参数说明:
● –data-dir 指定节点的数据存储目录,若不指定,则默认是当前目录。这些数据包括节点ID,集群ID,集群初始化配置,Snapshot文件,若未指 定–wal-dir,还会存储WAL文件
● –wal-dir 指定节点的was文件存储目录,若指定了该参数,wal文件会和其他数据文件分开存储
● –name 节点名称
● –initial-advertise-peer-urls 告知集群其他节点的URL,tcp2380端口用于集群通信
● –listen-peer-urls 监听URL,用于与其他节点通讯
● –advertise-client-urls 告知客户端的URL, 也就是服务的URL,tcp2379端口用于监听客户端请求
● –initial-cluster-token 集群的ID
● –initial-cluster 集群中所有节点
● –initial-cluster-state 集群状态,new为新创建集群,existing为已存在的集群

在etcd1、etcd2上分别做相似操作,只需将脚本中–advertise-client-urls 和 –initial-advertis-peer-urls 参数修改一下即可。

注意:上面的初始化只是在集群初始化时运行一次,之后节点的服务有重启,必须要去掉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

修改etcd3.sh,脚本中–advertise-client-urls 和 –initial-advertis-peer-urls 参数修改为etcd3的,–initial-cluster-state改为existing

# 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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值