一、下载安装包
ETCD_VER=v3.4.16 GOOGLE_URL=https://storage.googleapis.com/etcd
GITHUB_URL=https://github.com/etcd-io/etcd/releases/download
DOWNLOAD_URL=${GOOGLE_URL}
curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /opt/etcd-${ETCD_VER}-linux-amd64.tar.gz
二、创建安装目录及数据目录
mkdir -p /opt/etcd/conf/ /etcd-data/logs/
tar xf /opt/etcd-${ETCD_VER}-linux-amd64.tar.gz -C /opt/etcd/
cp /opt/etcd/etcd-v3.4.16-linux-amd64/etcd /usr/bin/. && cp /opt/etcd/etcd-v3.4.16-linux-amd64/etcdctl /usr/bin/.
三、创建启动脚本及配置文件
etcd配置文件
vim /opt/etcd/conf/etcd.yml
#写入如下内容
name: etcd-0 #一般etcd集群为三个节点,修改后面的序号即可
# 数 据 存 储 目 录
data-dir: /etcd-data
# 日 志 文 件 配 置
# logger: zap
--log-outputs: stderr
# log-level: info
#开 启 grpc gateway
enable-grpc-gateway: true
# 客 户 端 连 接 相 关 配 置
# 服 务 监 听 端 点
listen-client-urls: http://0.0.0.0:2379
# 向 客 户 端 发 布 的 服 务 端 点
advertise-client-urls: http://10.183.90.10:2379 #修改为本机的ip
# 集 群 相 关 配 置
# 监 听 集 群 其 他 节 点 连 接 的 端 点
listen-peer-urls: http://0.0.0.0:2380
# 向 集 群 其 他 节 点 发 布 的 服 务 端 点
initial-advertise-peer-urls: http://10.183.90.10:2380 #修改为本机的ip
# 集 群 成 员 的 名 字 以 及 服 务 端 点 列 表 , 名 字 与 每 个 节 点 配 置 的 name字 段 值 对 应
initial-cluster: etcd-0=http://10.183.90.10:2380,etcd-1=http://10.183.88.52:2380,etcd-2=http://10.183.89.143:2380
# 集 群 标 识 token, 可 以 认 为 是 集 群 名 字
initial-cluster-token: etcd-devops-prod #集群名称
# 创 建 一 个 新 的 集 群 。 如 果 data-dir目 录 下 的 数 据 属 于 另 外 一 个 集 群 , 则 无 法 启 动
inital-cluster-state: new
# etcd使用内存limit 如果机器为8G,就填6G
quota-backend-bytes: 6442450944
# 最大快照数量
max-snapshots: 5
# 保留的wal文件的最大数量
max-wals: 5
# 触发磁盘快照的已提交事务数。
snapshot-count: 5000
# 服务器将接受的最大客户端请求大小(以字节为单位)。
max-request-bytes: 10485760
# 开启自动压缩。0表示禁用自动压缩。
auto-compaction-retention: "1"
启动脚本
vim etcd0_start.sh #依次类推,例如etcd第二个节点etcd1_start.sh,第三个节点etcd2_start.sh
nohup /usr/bin/etcd --config-file=/opt/etcd/conf/etcd.yml > /etcd-data/logs/etcd.log 2>&1 &
chmod +x etcd0_start.sh
四、启动etcd服务
etcd0
bash etcd0_start.sh
etcd1
bash etcd1_start.sh
etcd2
bash etcd2_start.sh
五、验证服务
命令行验证
[root@ip-10-152-0-37 etcd]# etcdctl endpoint status --cluster -w table
+---------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+---------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| http://10.152.0.37:2379 | 4139c8599b429a90 | 3.4.16 | 20 kB | true | false | 21 | 13 | 13 | |
| http://10.152.18.84:2379 | f1114884241bd302 | 3.4.16 | 16 kB | false | false | 21 | 13 | 13 | |
| http://10.152.15.161:2379 | f65346c2ae141289 | 3.4.16 | 20 kB | false | false | 21 | 13 | 13 | |
+---------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
[root@ip-10-152-0-37 etcd]# etcdctl endpoint health --cluster -w table
+---------------------------+--------+------------+-------+
| ENDPOINT | HEALTH | TOOK | ERROR |
+---------------------------+--------+------------+-------+
| http://10.152.0.37:2379 | true | 1.032243ms | |
| http://10.152.18.84:2379 | true | 2.094489ms | |
| http://10.152.15.161:2379 | true | 3.483197ms | |
+---------------------------+--------+------------+-------+
[root@ip-10-152-0-37 etcd]# etcdctl member list -w table
+------------------+---------+--------+---------------------------+---------------------------+------------+
| ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS | IS LEARNER |
+------------------+---------+--------+---------------------------+---------------------------+------------+
| 4139c8599b429a90 | started | etcd-0 | http://10.152.0.37:2380 | http://10.152.0.37:2379 | false |
| f1114884241bd302 | started | etcd-2 | http://10.152.18.84:2380 | http://10.152.18.84:2379 | false |
| f65346c2ae141289 | started | etcd-1 | http://10.152.15.161:2380 | http://10.152.15.161:2379 | false |
+------------------+---------+--------+---------------------------+---------------------------+------------+
#写入key进行测试
[root@ip-10-152-0-37 etcd]# etcdctl put "sunwenbo is good man" yes
OK
[root@ip-10-152-0-37 etcd]# etcdctl get "sunwenbo is good man"
sunwenbo is good man
yes
[root@ip-10-152-0-37 etcd]#
python代码进行简单的测试
import etcd3
client = etcd3.client(host='10.152.0.37',port=2379) #连接etcd
r = client.put('test', 'sunwenbo') #往etcd中存键值
b = client.get('test') #获取etcd中的键值
print(b)