CentOS下安装etcd集群

一 . 在所有机器上安装etcd

我们准备3台机器 

172.18.0.231 集群节点1
172.18.0.232 集群节点2
172.18.0.233 集群节点3

 

 

 

 

#yum安装etcd
yum install -y etcd

 

二 . 修改配置

#备份原配置文件并编辑
cp /etc/etcd/etcd.conf /etc/etcd/etcd.conf.bk
vim /etc/etcd/etcd.conf

 

参考配置:

Node1 : 172.18.0.231

#[Member]
ETCD_DATA_DIR="/var/lib/etcd/etcd231"
ETCD_WAL_DIR="/var/lib/etcd/etcd231/wal"
ETCD_LISTEN_PEER_URLS="http://172.18.0.231:2380"
ETCD_LISTEN_CLIENT_URLS="http://172.18.0.231:2379,http://127.0.0.1:2379"
ETCD_NAME="etcd231"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://172.18.0.231:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://172.18.0.231:2379,http://127.0.0.1:2379"
ETCD_INITIAL_CLUSTER="etcd231=http://172.18.0.231:2380,etcd232=http://172.18.0.232:2380,etcd233=http://172.18.0.233:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster-wanma"
ETCD_INITIAL_CLUSTER_STATE="new"

 

Node2 : 172.18.0.232

#[Member]
ETCD_DATA_DIR="/var/lib/etcd/etcd232"
ETCD_WAL_DIR="/var/lib/etcd/etcd232/wal"
ETCD_LISTEN_PEER_URLS="http://172.18.0.232:2380"
ETCD_LISTEN_CLIENT_URLS="http://172.18.0.232:2379,http://127.0.0.1:2379"
ETCD_NAME="etcd232"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://172.18.0.232:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://172.18.0.232:2379,http://127.0.0.1:2379"
ETCD_INITIAL_CLUSTER="etcd231=http://172.18.0.231:2380,etcd232=http://172.18.0.232:2380,etcd233=http://172.18.0.233:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster-wanma"
ETCD_INITIAL_CLUSTER_STATE="new"

 

Node3 : 172.18.0.233

#[Member]
ETCD_DATA_DIR="/var/lib/etcd/etcd233"
ETCD_WAL_DIR="/var/lib/etcd/etcd233/wal"
ETCD_LISTEN_PEER_URLS="http://172.18.0.233:2380"
ETCD_LISTEN_CLIENT_URLS="http://172.18.0.233:2379,http://127.0.0.1:2379"
ETCD_NAME="etcd233"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://172.18.0.233:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://172.18.0.233:2379,http://127.0.0.1:2379"
ETCD_INITIAL_CLUSTER="etcd231=http://172.18.0.231:2380,etcd232=http://172.18.0.232:2380,etcd233=http://172.18.0.233:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster-wanma"
ETCD_INITIAL_CLUSTER_STATE="new"

 

三 . 启动集群

#启动etcd服务
systemctl restart etcd

#设定etcd服务为开机自启动
systemctl enable etcd

注意 , 第一台启动的etcd(master节点)(isLeader=true)会等待第二台启动之后 ,才会启动成功

 

四.集群新增额外节点(临时节点)

步骤1 : 加入成员

#增加一个集群节点 , 首先新节点需要配置并启动etcd
etcdctl member add name=etcd234 --peer-urls="http://172.18.0.234:2380"

步骤2 : 删除新节点的data目录

rm -rf /var/lib/etcd/etcd234/*

步骤3 : 新节点增加配置etcd.conf

注意 , 里面的 ETCD_INITIAL_CLUSTER_STATE="existing" 代表已存在节点

步骤4 : 启动新节点的etcd服务

systemctl start etcd

 

PS : 步骤不能错 , 所以如果有可能 , 请新加节点之后 , 集群重启一下 , 比较不容易错

 

 

五 . 常用命令

#查看集群健康状态
etcdctl cluster-health

#查看集群所有节点
etcdctl member list

#把一台设备移除出集群 , 后面是集群节点号 , 使用list可以查看到
#移除之后,该节点的etcd服务自动关闭
etcdctl member remove 1e82894832618580

#更新一个节点
etcdctl member update 1e82894832618580

#设置key=hello , value=world
etcdctl set  hello world

#查看key的值
etcdctl get hello

 

 

posted on 2020-06-18 12:28  太清  阅读(3904)  评论(0编辑  收藏  举报