转载请注明出处:https://blog.csdn.net/sublio/article/details/112888870
注:
本文是在单台机器上搭建集群,多台搭建同理且更简单,因为不用考虑端口以及各种目录冲突问题,配置几乎大多一样,细微差别会在文中指出。
下载解压
cd /opt
wget https://github.com/etcd-io/etcd/releases/download/v3.3.10/etcd-v3.3.10-linux-amd64.tar.gz
tar zxvf etcd-v3.3.10-linux-amd64.tar.gz
mv etcd-v3.3.10-linux-amd64 etcd
cd etcd
配置文件准备
个别需要注意的配置文件描述:
#name: 节点名,区分各个节点
#initial-cluster: 集群配置,这里会用到各个节点的name
#整三份配置文件
mkdir config
vim config/etcd01.yaml
vim config/etcd02.yaml
vim config/etcd03.yaml
#etcd01.yaml
name: etcd-1
data-dir: /data/etcd01
listen-client-urls: http://ip1:2379,http://127.0.0.1:2379
advertise-client-urls: http://ip1:2379,http://127.0.0.1:2379
listen-peer-urls: http://ip1:2380
initial-advertise-peer-urls: http://ip1:2380
initial-cluster: etcd-1=http://ip1:2380,etcd-2=http://ip2:2480,etcd-3=http://ip3:2580
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new
enable-v2: true
#etcd02.yaml
name: etcd-2
data-dir: /data/etcd02
listen-client-urls: http://ip2:2479,http://127.0.0.1:2479
advertise-client-urls: http://ip2:2479,http://127.0.0.1:2479
listen-peer-urls: http://ip2:2480
initial-advertise-peer-urls: http://ip2:2480
initial-cluster: etcd-1=http://ip1:2380,etcd-2=http://ip2:2480,etcd-3=http://ip3:2580
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new
enable-v2: true
#etcd03.yaml
name: etcd-3
data-dir: /data/etcd03
listen-client-urls: http://ip3:2579,http://127.0.0.1:2579
advertise-client-urls: http://ip3:2579,http://127.0.0.1:2579
listen-peer-urls: http://ip3:2580
initial-advertise-peer-urls: http://ip3:2580
initial-cluster: etcd-1=http://ip1:2380,etcd-2=http://ip2:2480,etcd-3=http://ip3:2580
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new
enable-v2: true
启动(这里用的是supercisor)
配置如下:
#vim /etc/supervisord.d/etcd01.ini
[program:etcd01]
command = /opt/etcd/etcd --config-file /opt/etcd/config/etcd01.yaml
autostart = true
startsecs = 5
autorestart = true
startretries = 3
user = root
redirect_stderr = true
stdout_logfile_maxbytes = 20MB
stdout_logfile_backups = 20
stdout_logfile = /data/etcd01-log/etcd.log
#vim /etc/supervisord.d/etcd02.ini
[program:etcd02]
command = /opt/etcd/etcd --config-file /opt/etcd/config/etcd02.yaml
autostart = true
startsecs = 5
autorestart = true
startretries = 3
user = root
redirect_stderr = true
stdout_logfile_maxbytes = 20MB
stdout_logfile_backups = 20
stdout_logfile = /data/etcd02-log/etcd.log
#vim /etc/supervisord.d/etcd03.ini
[program:etcd03]
command = /opt/etcd/etcd --config-file /opt/etcd/config/etcd03.yaml
autostart = true
startsecs = 5
autorestart = true
startretries = 3
user = root
redirect_stderr = true
stdout_logfile_maxbytes = 20MB
stdout_logfile_backups = 20
stdout_logfile = /data/etcd03-log/etcd.log
重新载入supervisor下所有服务
supervisorctl
#进入之后
reload
测试是否搭建成功
#因为--endpoints=是3的用法(大概)
export ETCDCTL_API=3
#put
/opt/etcd/etcdctl --endpoints=[ip1:2379,ip2:2479,ip3:2579] put test1 value1
#get
/opt/etcd/etcdctl --endpoints=[ip1:2379,ip2:2479,ip3:2579] get test1
开启用户认证,参考:
https://blog.csdn.net/ucmir183/article/details/84454506
https://blog.csdn.net/weixin_45541368/article/details/103846584
#创建root角色
/opt/etcd/etcdctl role add root --endpoints=[addr,逗号分隔]
#创建root用户(这里需要输入密码并确认)
/opt/etcd/etcdctl user add root --endpoints=[addr,逗号分隔]
#给root用户赋予root角色
/opt/etcd/etcdctl user grant-role root root --endpoints=[addr,逗号分隔]
#开启auth
/opt/etcd/etcdctl auth enable --endpoints=[addr,逗号分隔]
#赋予角色root
/opt/etcd/etcdctl role grant-permission root --prefix=true readwrite / --endpoints=[addr,逗号分隔]