etcd集群搭建
环境配置
etcd-v3.5.0
192.168.108.128 节点1
192.168.108.129 节点2
192.168.108.130 节点3
ETCD下载
https://github.com/etcd-io/etcd/releases/download/v3.5.0/etcd-v3.5.0-linux-amd64.tar.gz
ETCD安装配置
- 解压安装:
$ tar -zxvf etcd-v3.5.0-linux-amd64.tar.gz -C /opt/
$ cd /opt
$ mv etcd-v3.5.0-linux-amd64 etcd-v3.5.0
$ mkdir /etc/etcd # 创建etcd配置文件目录
- 创建etcd配置文件:
$ vi /etc/etcd/conf.yml
节点1,添加如下内容:
name: etcd-1
data-dir: /opt/etcd-v3.5.0/data
listen-client-urls: http://192.168.108.128:2379,http://127.0.0.1:2379
advertise-client-urls: http://192.168.108.128:2379,http://127.0.0.1:2379
listen-peer-urls: http://192.168.108.128:2380
initial-advertise-peer-urls: http://192.168.108.128:2380
initial-cluster: etcd-1=http://192.168.108.128:2380,etcd-2=http://192.168.108.129:2380,etcd-3=http://192.168.108.130:2380
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new
节点2,添加如下内容:
name: etcd-2
data-dir: /opt/etcd-v3.5.0/data
listen-client-urls: http://192.168.108.129:2379,http://127.0.0.1:2379
advertise-client-urls: http://192.168.108.129:2379,http://127.0.0.1:2379
listen-peer-urls: http://192.168.108.129:2380
initial-advertise-peer-urls: http://192.168.108.129:2380
initial-cluster: etcd-1=http://192.168.108.128:2380,etcd-2=http://192.168.108.129:2380,etcd-3=http://192.168.108.130:2380
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new
节点3,添加如下内容:
name: etcd-3
data-dir: /opt/etcd-v3.5.0/data
listen-client-urls: http://192.168.108.130:2379,http://127.0.0.1:2379
advertise-client-urls: http://192.168.108.130:2379,http://127.0.0.1:2379
listen-peer-urls: http://192.168.108.130:2380
initial-advertise-peer-urls: http://192.168.108.130:2380
initial-cluster: etcd-1=http://192.168.108.128:2380,etcd-2=http://192.168.108.129:2380,etcd-3=http://192.168.108.130:2380
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new
- 更新etcd系统默认配置:
当前使用的是etcd v3版本,系统默认的是v2,通过下面命令修改配置。
$ vi /etc/profile
在末尾追加
export ETCDCTL_API=3
$ source /etc/profile
ETCD命令
$ cd /opt/etcd-v3.5.0
- 创建etcd配置文件:
$ ./etcdctl version
etcdctl version: 3.5.0
API version: 3.5
- 启动命令:
$ ./etcd --config-file=/etc/etcd/conf.yml
- 查看集群成员信息:
$ ./etcdctl member list
2618ce5cd761aa8e: name=etcd-3 peerURLs=http://192.168.108.130:2380 clientURLs=http://127.0.0.1:2379,http://192.168.108.130:2379 isLeader=false
9c359d48a2f34938: name=etcd-1 peerURLs=http://192.168.108.128:2380 clientURLs=http://127.0.0.1:2379,http://192.168.108.128:2379 isLeader=false
f3c45714407d68f3: name=etcd-2 peerURLs=http://192.168.108.129:2380 clientURLs=http://127.0.0.1:2379,http://192.168.108.129:2379 isLeader=true
- 查看集群状态(Leader节点):
$ ./etcdctl endpoint health --endpoints=192.168.108.130:2380
192.168.108.130:2380 is healthy: successfully committed proposal: took = 4.780312ms cluster is healthy
ECTD读写操作
基于HTTP协议的API使用起来比较简单,这里主要通过etcdctl和curl两种方式来做简单介绍。
下面通过给message key设置Hello值示例:
$ ./etcdctl put /message Hello
OK
读取message的值:
$ ./etcdctl get /message
/message
Hello
删除message key:
$ ./etcdctl del /message
1
说明:因为是集群,所以message在其中一个节点创建后,在集群中的任何节点都可以查询到。
配置ETCD为启动服务
编辑/usr/lib/systemd/system/etcd.service,添加下面内容:
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
WorkingDirectory=/home/bea2/etcd/etcd-v3.5.0/
# User=etcd
ExecStart=/home/bea2/etcd/etcd-v3.5.0/etcd --config-file=/home/bea2/etcd/etcd-v3.5.0/conf/conf.yml
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
更新启动:
systemctl daemon-reload
systemctl enable etcd
systemctl start etcd
systemctl restart etcd
systemctl status etcd.service -l

浙公网安备 33010602011771号