参考: https://www.cnblogs.com/51wansheng/p/10234036.html
分别在master和node1机器上安装etcd
# step1:下载某版本etcd的release包
$ wget https://github.com/etcd-io/etcd/releases/download/v2.3.7/etcd-v2.3.7-linux-amd64.tar.gz
# step2:解压安装到 /data/ 目录下(安装到那个目录可以自己定)
$ tar -C /data -zxvf etcd-v2.3.7-linux-amd64.tar.gz
# step3:检查安装是否成功
$ /data/etcd-v2.3.7-linux-amd64/etcdctl -v
etcdctl version 2.3.7
etcd重要配置参数的说明,不通版本的etcd可能参数选项以及选项的默认值会有些许出入,可以通过 etcd --help
查看
--name # etcd实例名称
--data-dir # etcd数据存储的目录
--listen-client-urls # 供外部客户端使用的url
--advertise-client-urls # 广播给外部客户端使用的url
--listen-peer-urls # 集群内部通信使用的url
--initial-advertise-peer-urls # 广播给集群内其他成员访问的url
--initial-cluster # 初始集群成员列表
--initial-cluster-token # 集群的名称
--initial-cluster-state # 初始集群状态,new为新建集群(master为new,node为existing)
我们通过systemd来管理etcd服务,所以在/etc/systemd/system
目录下新建etcd.service
文件,不了解systemd的可以查看阮一峰老师的博客
# vim /etcsystemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
WorkingDirectory=/data/etcd-v2.3.7-linux-amd64
User=root
ExecStart=/data/etcd-v2.3.7-linux-amd64/etcd --name etcd0 --data-dir /data/etcd_data/etcd1 \
--listen-client-urls http://172.18.0.30:2379,http://localhost:2379 --advertise-client-urls http://172.18.0.30:2379,http://localhost:2379 \
--listen-peer-urls http://172.18.0.30:2380 --initial-advertise-peer-urls http://172.18.0.30:2380 \
--initial-cluster etcd0=http://172.18.0.30:2380,etcd1=http://172.18.0.26:2480 --initial-cluster-token etcd-cluster \
--initial-cluster-state new
Restart=always
[Install]
WantedBy=multi-user.target
注:如果默认的2379
、2380
端口已被占用,可以配置etcd监听其他未被占用的端口
# vim /etc/systemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
WorkingDirectory=/data/etcd-v2.3.7-linux-amd64
User=root
ExecStart=/data/etcd-v2.3.7-linux-amd64/etcd --name etcd1 --data-dir /data/etcd_data/etcd0 \
--listen-client-urls http://172.18.0.26:2479,http://localhost:2479 --advertise-client-urls http://172.18.0.26:2479,http://localhost:2479 \
--listen-peer-urls http://172.18.0.26:2480 --initial-advertise-peer-urls http://172.18.0.26:2480 \
--initial-cluster etcd0=http://172.18.0.30:2380,etcd1=http://172.18.0.26:2480 --initial-cluster-token etcd-cluster \
--initial-cluster-state existing
Restart=always
[Install]
WantedBy=multi-user.target
配置好etcd的服务后,分别启动master和node1节点的etcd服务
# step1:重新加载systemd配置文件(修改systemd配置文件后必须重新加载)
$ systemctl daemon-reload
# step2:启动etcd服务
$ systemctl restart etcd
# 停止etcd服务
$ systmectl stop etcd
# 查看etcd状态
$ systemctl status etcd
# 查看etcd日志
$ journalctl -u etcd
# 设置etcd开机启动
$ systemctl enable etcd
# 查看etcd集群成员(可以在集群成员的任一台机器上操作)
$ /data/etcd-v2.3.7-linux-amd64/etcdctl member list
bd3b67d271b1097d: name=etcd0 peerURLs=http://172.18.0.30:2380 clientURLs=http://172.18.0.30:2379,http://localhost:2379 isLeader=true
e5ad2a6d82697b13: name=etcd1 peerURLs=http://172.18.0.26:2480 clientURLs=http://172.18.0.26:2479,http://localhost:2479 isLeader=false
# 查看集群成员健康情况
$ /data/etcd-v2.3.7-linux-amd64/etcdctl cluster-health
member bd3b67d271b1097d is healthy: got healthy result from http://172.18.0.30:2379
member e5ad2a6d82697b13 is healthy: got healthy result from http://172.18.0.26:2479
cluster is healthy
# 在master上set一个key-value
$ /data/etcd-v2.3.7-linux-amd64/etcdctl set name tab609
tab609
# 在node1上get这个key的值(注:如果监听的不是默认端口2380则需要指定 --endpoints)
$ /data/etcd-v2.3.7-linux-amd64/etcdctl --endpoints http://localhost:2479 get name
tab609
# 更多etcdctl操作可以查看帮助
$ /data/etcd-v2.3.7-linux-amd64/etcdctl help
同上安装步骤
node2节点
# vim /etc/systemd/sytem/etcd.service
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
WorkingDirectory=/data/etcd-v2.3.7-linux-amd64
User=root
ExecStart=/data/etcd-v2.3.7-linux-amd64/etcd --name etcd2 --data-dir /data/etcd_data/etcd2 \
--listen-client-urls http://172.18.0.17:2479,http://localhost:2479 --advertise-client-urls http://172.18.0.17:2479,http://localhost:2479 \
--listen-peer-urls http://172.18.0.17:2480 --initial-advertise-peer-urls http://172.18.0.17:2480 \
--initial-cluster etcd0=http://172.18.0.30:2380,etcd1=http://172.18.0.26:2480,etcd2=http://172.18.0.17:2480 --initial-cluster-token yj1918-etcd-cluster \
--initial-cluster-state existing
Restart=always
[Install]
WantedBy=multi-user.target
master节点配置文件中的集群成员添加node2节点,只需修改--initial-cluster
参数选项
# vim /etc/systemd/sytem/etcd.service
ExecStart= ... \
--initial-cluster etcd0=http://172.18.0.30:2380,etcd1=http://172.18.0.26:2480,etcd2=http://172.18.0.17:2480
同理node1节点配置文件中的集群成员也添加node2节点,只需修改--initial-cluster
参数选项
# vim /etc/systemd/sytem/etcd.service
ExecStart= ... \
--initial-cluster etcd0=http://172.18.0.30:2380,etcd1=http://172.18.0.26:2480,etcd2=http://172.18.0.17:2480
分别在master、node1、node2上执行同上的启动步骤,但在启动node2的时候启动失败,报如下错误
error validating peerURLs {ClusterID:b0f36b8e1c8349f4 Members:[&{ID:bd3b67d271b1097d RaftAttributes:{PeerURLs:[http://172.18.0.30:2380]} Attributes:{Name:etcd0 ClientURLs:[http://172.18.0.30:2379]
也就是说在验证对端(master)的rul时出错了,谷歌后找到解决方法,我们需要在启动新节点之前,必须把新节点接入集群,然后再启动新节点
# 在master上添加新的节点成员(master机器操作)
$ /data/etcd-v2.3.7-linux-amd64/etcdctl member add node2 http://172.18.0.17:2480
# 重新启动node2的etcd服务就可以成功了(node2机器操作)
$ systemctl restart etcd
# 查看集群的成员(如我在node2上执行,如果监听的不是默认端口2379则需要指定 --endpoints)
$ /data/etcd-v2.3.7-linux-amd64/etcdctl --endpoints http://172.18.0.17:2479 member list
bd3b67d271b1097d: name=etcd0 peerURLs=http://172.18.0.30:2380 clientURLs=http://172.18.0.30:2379,http://localhost:2379 isLeader=true
e20ddba3b692fe46: name=etcd2 peerURLs=http://172.18.0.17:2480 clientURLs=http://172.18.0.17:2479,http://localhost:2479 isLeader=false
e5ad2a6d82697b13: name=etcd1 peerURLs=http://172.18.0.26:2480 clientURLs=http://172.18.0.26:2479,http://localhost:2479 isLeader=false
写在最后: etcd是集群,我们get取数据的时候,怎么保证数据的一致性和可靠性呢?etcd是通过类似选举的方式,过半通过规则返回数据。比如集群有三台机器,如果两台机器的key-value都相同,即2/3过半,则返回数据。为了保证数据的一致和可靠性,etcd集群的机器数必须奇数且大于一个节点,并非偶数,更详细可以查看:https://www.jianshu.com/p/5aed73b288f7