一、目标
在centos7.6上安装配置etcd集群
二、架构
ETCD集群3个节点的架构
ip | hostname | os & module | Cluster Name |
10.1.1.5 | etcd5 | centos 7.6 etcd-v3.3.2-linux-amd64 | etcd-1 |
10.1.1.6 | etcd6 | centos 7.6 etcd-v3.3.2-linux-amd64 | etcd-2 |
10.1.1.7 | etcd7 | centos 7.6 etcd-v3.3.2-linux-amd64 | etcd-3 |
三、初始化环境
以下在所有节点上都要操作
1.关闭防火墙和selinux
systemctl restart ntpd
systemctl enable ntpd
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
2.下载etcd软件包
wget -P /usr/local/src/ https://github.com/coreos/etcd/releases/download/v3.3.2/etcd-v3.3.2-linux-amd64.tar.gz
cd /usr/local/src
tar -zxf etcd-v3.3.2-linux-amd64.tar.gz
mv etcd-v3.3.2-linux-amd64 /usr/local/etcd-v3.3.2
3.修改系统环境变量
vim ~/.bashrc
在~/.bashrc文件的末尾追加以下两行内容
export PATH=$PATH:/usr/local/etcd-v3.3.2/
export ETCDCTL_API=3
让配置生效
source ~/.bashrc
四、配置etcd
以下需要再三个节点上操作,配置文件略有不同,注意看
1.创建etcd启动所需的配置文件
说明:配置文件基本相同,唯独advertise-client-urls和initial-advertise-peer-urls要写自己的ip地址。
● 在节点1(10.1.1.5)创建配置文件etcd.conf
vim /usr/local/etcd-v3.3.2/etcd.conf
name: etcd-1
data-dir: /usr/local/etcd-v3.3.2/data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://10.1.1.5:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://10.1.1.5:2380
initial-cluster: etcd-1=http://10.1.1.5:2380,etcd-2=http://10.1.1.6:2380,etcd-3=http://10.1.1.7:2380
initial-cluster-token: etcd-cluster-my
initial-cluster-state: new
● 在节点2(10.1.1.6)创建配置文件etcd.conf
vim /usr/local/etcd-v3.3.2/etcd.conf
name: etcd-2
data-dir: /usr/local/etcd-v3.3.2/data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://10.1.1.6:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://10.1.1.6:2380
initial-cluster: etcd-1=http://10.1.1.5:2380,etcd-2=http://10.1.1.6:2380,etcd-3=http://10.1.1.7:2380
initial-cluster-token: etcd-cluster-my
initial-cluster-state: new
● 在节点3(10.1.1.7)创建配置文件etcd.conf
vim /usr/local/etcd-v3.3.2/etcd.conf
name: etcd-3
data-dir: /usr/local/etcd-v3.3.2/data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://10.1.1.7:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://10.1.1.7:2380
initial-cluster: etcd-1=http://10.1.1.5:2380,etcd-2=http://10.1.1.6:2380,etcd-3=http://10.1.1.7:2380
initial-cluster-token: etcd-cluster-my
initial-cluster-state: new
注释:
• name: etcd-1 --->etcd集群节点名称(每台主机不能重复)
• data-dir: /usr/local/etcd-v3.3.2/data --->存放数据的主目录
• listen-client-urls: http://0.0.0.0:2379 --->监听客户端的url,0.0.0.0表示监听本机所有的ip地址
• advertise-client-urls: http://10.1.1.5:2379 --->向etcd集群宣告自己的ip和端口,写本机ip
• listen-peer-urls: http://0.0.0.0:2380 --->监听集群中其他成员的ip,0.0.0.0表示所有ip都能成为成员
• initial-advertise-peer-urls: http://10.1.1.5:2380 --->宣告给集群中的其他成员自己的ip和端口,写本机ip
• initial-cluster: etcd-1=http://10.1.1.5:2380,etcd-2=http://10.1.1.6:2380,etcd-3=http://10.1.1.7:2380 --->将集群成员的所有集群名(etcd.conf中的name值)和其ip+端口都写在这里,每个成员用逗号分隔。
• initial-cluster-token: etcd-cluster-my --->集群的token,只有相同的token才能有资格成为etcd成员
• initial-cluster-state: new --->集群初始化状态。new表示新建集群。当谁想加入本已存在的集群时后,那它的状态应写成existing
2.在所有节点上都创建数据存储目录
mkdir /usr/local/etcd-v3.3.2/data
3.在所有节点上都创建系统启动菜单
vim /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=/usr/local/etcd-v3.3.2
# User=etcd
ExecStart=/usr/local/etcd-v3.3.2/etcd --config-file /usr/local/etcd-v3.3.2/etcd.conf
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
4.首次启动etcd集群节点
启动大概原则:先启动任意一个节点,待第一个节点启动成功后,再启动剩余的两个
● 启动节点1的etcd,并设置为开机自启
systemctl daemon-reload
systemctl restart etcd && systemctl enable etcd
● 启动节点2和节点3的etcd,并设置为开机自启
systemctl daemon-reload
systemctl restart etcd && systemctl enable etcd
5.检验ETCD集群
● 在任意节点上执行,观察2379和2380端口是否启动
[root@etcd5 ~]# netstat -ntlup |grep etcd
tcp6 0 0 :::2379 :::* LISTEN 6417/etcd
tcp6 0 0 :::2380 :::* LISTEN 6417/etcd
● 在任意接上上执行,查看ETCD集群成员列表
[root@etcd5 ~]# etcdctl member list
71b9fac8482ec139, started, etcd-1, http://10.1.1.5:2380, http://10.1.1.5:2379
89d4b75907acdf9a, started, etcd-3, http://10.1.1.7:2380, http://10.1.1.7:2379
b22a0934e0760a75, started, etcd-2, http://10.1.1.6:2380, http://10.1.1.6:2379
注释:查到3行,并且是已启动状态。说明我们本次的3个节点上的etcd都启动成功了,而且成功的组件成群了。
● 在任意接上上执行,查看节点健康状况
[root@etcd5 ~]# etcdctl endpoint health --endpoints=10.1.1.5:2379,10.1.1.6:2379,10.1.1.7:2379
10.1.1.5:2379 is healthy: successfully committed proposal: took = 1.79041ms
10.1.1.7:2379 is healthy: successfully committed proposal: took = 3.425099ms
10.1.1.6:2379 is healthy: successfully committed proposal: took = 3.544992ms
6.小结
无论你如何重启任意节点,集群依然监控。
无论你如何重启某个节点服务器,重启后,它依然能顺利加入到ETCD集群。
五、ETCD的基本操作命令
1.创建键值对
创建一个键name值为serena
etcdctl put name serena
2.根据键查询值
查询键为name的信息
[root@etcd5 ~]# etcdctl get name
name
serena
3.根据键的前缀模糊查询键值对信息
查询键以n开头的所有键值
[root@etcd5 ~]# etcdctl get n --prefix
name
serena
name2
kahn.xiao
4.删除指定的键
#当键存在时,成功删除返回1
[root@etcd5 ~]# etcdctl del name4
1
#当键不存在时,删除返回0
[root@etcd5 ~]# etcdctl del name666
0
5.使用URL的方式存储键值
[root@etcd5 ~]# curl http://10.1.1.5:2379/v2/keys/name5 -XPUT -d value="kahn.xiao"
{"action":"set","node":{"key":"/name5","value":"kahn.xiao","modifiedIndex":19,"createdIndex":19}}
6.使用URL的方式查询键值
[root@etcd5 ~]# curl -s http://10.1.1.5:2379/v2/keys/name5
{"action":"get","node":{"key":"/name5","value":"kahn.xiao","modifiedIndex":19,"createdIndex":19}}
7.使用URL的方式查询ETCD的版本
[root@etcd5 ~]# curl -s http://10.1.1.5:2379/version
{"etcdserver":"3.3.2","etcdcluster":"3.3.0"}
8.使用URL的方式删除键值
[root@etcd5 ~]# curl -s http://10.1.1.5:2379/v2/keys/name5 -XDELETE
{"action":"delete","node":{"key":"/name5","modifiedIndex":20,"createdIndex":19},"prevNode":{"key":"/name5","value":"kahn.xiao","modifiedIndex":19,"createdIndex":19}}
9.使用URL的方式创建目录
创建一个叫kahnDIR的目录
[root@etcd5 ~]# curl -s http://10.1.1.5:2379/v2/keys/kahnDIR -XPUT -d dir=true
{"action":"set","node":{"key":"/kahnDIR","dir":true,"modifiedIndex":21,"createdIndex":21}}
10.使用URL的方式查询所有目录
[root@etcd5 ~]# curl -s http://10.1.1.5:2379/v2/keys/?recursive=true
{"action":"get","node":{"dir":true,"nodes":[{"key":"/kahnDIR","dir":true,"modifiedIndex":21,"createdIndex":21},
{"key":"/kahnDIR2","dir":true,"modifiedIndex":22,"createdIndex":22},
{"key":"/kahnDIR3","dir":true,"modifiedIndex":23,"createdIndex":23}]}}
注释:这里查到有3个目录,目录名分别叫kahnDIR,kahnDIR2,kahnDIR3
11.使用URL的方式删除一个目录
删除一个叫kahnDIR3的目录
[root@etcd5 ~]# curl -s 'http://10.1.1.5:2379/v2/keys/kahnDIR3?dir=true' -XDELETE
{"action":"delete","node":{"key":"/kahnDIR3","dir":true,"modifiedIndex":24,"createdIndex":23},"prevNode":{"key":"/kahnDIR3","dir":true,"modifiedIndex":23,"createdIndex":23}}
-------------END-------------------2020年11月3日22:59:06-------------------------