etcd 是一个高可用的 Key/Value 存储系统,主要用于分享配置和服务发现。etcd 的灵感来自于 ZooKeeper 和 Doozer,侧重于:

  • 简单:支持 curl 方式的用户 API (HTTP+JSON)

  • 安全:可选 SSL 客户端证书认证

  • 快速:单实例可达每秒 10000 次写操作

  • 可靠:使用 Raft 实现分布式

ETCD下载

https://github.com/etcd-io/etcd/releases 选择最新版本:etcd-v3.3.13-darwin-amd64.zip上传到服务器

etcd部署安装 

部署架构

  • 192.168.10.12 节点1

  • 192.168.10.13 节点2

  • 192.168.10.14 节点3

安装解压

$tar -zxvf etcd-v3.3.13-linux-amd64.tar.gz -C /workspace

创建etcd配置文件

$cd /workspace/etcd-v3.3.13/

$vim conf.yml

节点1,添加如下内容:

name: etcd-1

data-dir: /data/etcd

listen-client-urls: http://192.168.10.12:2379,http://127.0.0.1:2379

advertise-client-urls: http://192.168.10.12:2379,http://127.0.0.1:2379

listen-peer-urls: http://192.168.10.12:2380

initial-advertise-peer-urls: http://192.168.10.12:2380

initial-cluster: etcd-1=http://192.168.10.12:2380,etcd-2=http://192.168.10.13:2380,etcd-3=http://192.168.10.14:2380

initial-cluster-token: etcd-cluster-token

initial-cluster-state: new

节点2,添加如下内容:

name: etcd-2

data-dir: /data/etcd

listen-client-urls: http://192.168.10.13:2379,http://127.0.0.1:2379

advertise-client-urls: http://192.168.10.13:2379,http://127.0.0.1:2379

listen-peer-urls: http://192.168.10.13:2380

initial-advertise-peer-urls: http://192.168.10.13:2380

initial-cluster: etcd-1=http://192.168.10.12:2380,etcd-2=http://192.168.10.13:2380,etcd-3=http://192.168.10.14:2380

initial-cluster-token: etcd-cluster-token

initial-cluster-state: new

节点3,添加如下内容:

name: etcd-3

data-dir: /data/etcd

listen-client-urls: http://192.168.10.14:2379,http://127.0.0.1:2379

advertise-client-urls: http://192.168.10.14:2379,http://127.0.0.1:2379

listen-peer-urls: http://192.168.10.14:2380

initial-advertise-peer-urls: http://192.168.10.14:2380

initial-cluster: etcd-1=http://192.168.10.12:2380,etcd-2=http://192.168.10.13:2380,etcd-3=http://192.168.10.14:2380

initial-cluster-token: etcd-cluster-token

initial-cluster-state: new

更新etcd系统默认配置

当前使用的是etcd v3版本,系统默认的是v2,通过下面命令修改配置。

$vim /etc/profile

在文件末尾追加:

export ETCDCTL_API=3

 使文件生效

$ source /etc/profile

ETCD命令

查看版本信息:

./etcdctl version

image.png

启动命令

$nohup ./etcd --config-file=/workspace/etcd-v3.3.13/conf.yml &


查看集群成员信息

$ ./etcdctl member list

image.png

ECTD读写操作

基于HTTP协议的API使用起来比较简单,这里主要通过etcdctl和curl两种方式来做简单介绍

下面通过给message key设置Hello值示例

./etcdctl  put /message howareyou


image.png

$ curl -X PUT http://127.0.0.1:2379/v2/keys/message -d value="youyou"

image.png

读取message的值

$ ./etcdctl  get /message

image.png

$  curl http://127.0.0.1:2379/v2/keys/message

image.png

删除message key

$ ./etcdctl del /message

image.png

再查看消息

image.png

注意:因为是集群,所以message在其中一个节点创建后,在集群中的任何节点都可以查询到