Etcd教程 — Etcd单机版安装

洪子晋
2023-12-01

前述

Etcd教程 — 第一章 Etcd简介、Etcd单机安装安装的单机版Etcd,可以使用,但是本机只能访问本机的Etcd,其他电脑是无法使用该机Etcd的,所以本文重点解决的就是Etcd不能访问。

1 Linux安装Etcd

1.1 创建安装执行脚本

在 linux的 /opt/soft/etcd 目录下创建一个脚本文件:

vim etcd

1.2 脚本内容

ETCD_VER=v3.5.4

# choose either URL 翻译:选择下面两个任意一个地址即可
GOOGLE_URL=https://storage.googleapis.com/etcd
GITHUB_URL=https://github.com/etcd-io/etcd/releases/download
DOWNLOAD_URL=${GOOGLE_URL}

rm -f /opt/soft/etcd/etcd-${ETCD_VER}-linux-amd64.tar.gz
rm -rf /opt/soft/etcd/etcd-download-test && mkdir -p /opt/soft/etcd/etcd-download-test

curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /opt/soft/etcd/etcd-${ETCD_VER}-linux-amd64.tar.gz
tar xzvf /opt/soft/etcd/etcd-${ETCD_VER}-linux-amd64.tar.gz -C /opt/soft/etcd/etcd-download-test --strip-components=1
rm -f /opt/soft/etcd/etcd-${ETCD_VER}-linux-amd64.tar.gz

#查看安装是否成功的相关命令
/opt/soft/etcd/etcd-download-test/etcd --version
/opt/soft/etcd/etcd-download-test/etcdctl version
/opt/soft/etcd/etcd-download-test/etcdutl version

1.3 执行脚本里的命令

/opt/soft/etcd 执行:

./etcd

如果在执行时提示权限不够,则需要授权,然后再次执行即可:

chmod 777 etcd

1.4 查看Etcd安装是否成功

1.4.1 查看Etcd服务端安装是否成功

/opt/soft/etcd/etcd-download-test/etcd --version

如下显示则安装成功:

etcd Version: 3.5.4
Git SHA: 08407ff76
Go Version: go1.16.15
Go OS/Arch: linux/amd64

1.4.2 查看Etcd客户端安装是否成功

/opt/soft/etcd/etcd-download-test/etcdctl version

如下显示则安装成功:

etcdctl version: 3.5.4
API version: 3.5

1.5 启动Etcd

/opt/soft/etcd/etcd-download-test/etcd

1.6 测试

/opt/soft/etcd/etcd-download-test/etcdctl put foo bar
/opt/soft/etcd/etcd-download-test/etcdctl get foo

2 Etcd配置

2.1 开放端口

开放:2379、2380端口

firewall-cmd --zone=public --add-port=2379/tcp --permanent
firewall-cmd --zone=public --add-port=2380/tcp --permanent

重启防火墙:

firewall-cmd --reload

查看开放的端口:

firewall-cmd --list-port

关闭firewall:

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动

2.2 修改配置

1、切换至 /opt/soft/etcd/etcd-download-test/ 目录,将 etcdetcdctl 这两个二进制文件复制到 /usr/local/bin 目录下,这样就可以在系统中直接调用etcd/etcdctl这两个程序了。

cp etcd etcdctl /usr/local/bin

2、输入命令etcd,即可启动一个单节点的etcd服务,ctrl+c即可停止服务。这里讲解一下etcd服务启动后控制台显示的各个参数的意义:

{"level":"info","ts":"2022-06-05T22:46:31.751+0800","caller":"etcdmain/etcd.go:73","msg":"Running: ","args":["etcd"]}
{"level":"warn","ts":"2022-06-05T22:46:31.751+0800","caller":"etcdmain/etcd.go:105","msg":"'data-dir' was empty; using default","data-dir":"default.etcd"}
{"level":"info","ts":"2022-06-05T22:46:31.751+0800","caller":"etcdmain/etcd.go:116","msg":"server has been already initialized","data-dir":"default.etcd","dir-type":"member"}
{"level":"info","ts":"2022-06-05T22:46:31.751+0800","caller":"embed/etcd.go:131","msg":"configuring peer listeners","listen-peer-urls":["http://localhost:2380"]}
{"level":"info","ts":"2022-06-05T22:46:31.752+0800","caller":"embed/etcd.go:139","msg":"configuring client listeners","listen-client-urls":["http://localhost:2379"]}
{"level":"info","ts":"2022-06-05T22:46:31.752+0800","caller":"embed/etcd.go:308","msg":"starting an etcd server","etcd-version":"3.5.4","git-sha":"08407ff76","go-version":"go1.16.15","go-os":"linux","go-arch":"amd64","max-cpu-set":1,"max-cpu-available":1,"member-initialized":true,"name":"default","data-dir":"default.etcd","wal-dir":"","wal-dir-dedicated":"","member-dir":"default.etcd/member","force-new-cluster":false,"heartbeat-interval":"100ms","election-timeout":"1s","initial-election-tick-advance":true,"snapshot-count":100000,"snapshot-catchup-entries":5000,"initial-advertise-peer-urls":["http://localhost:2380"],"listen-peer-urls":["http://localhost:2380"],"advertise-client-urls":["http://localhost:2379"],"listen-client-urls":["http://localhost:2379"],"listen-metrics-urls":[],"cors":["*"],"host-whitelist":["*"],"initial-cluster":"","initial-cluster-state":"new","initial-cluster-token":"","quota-size-bytes":2147483648,"pre-vote":true,"initial-corrupt-check":false,"corrupt-check-time-interval":"0s","auto-compaction-mode":"periodic","auto-compaction-retention":"0s","auto-compaction-interval":"0s","discovery-url":"","discovery-proxy":"","downgrade-check-interval":"5s"}
{"level":"info","ts":"2022-06-05T22:46:31.752+0800","caller":"etcdserver/backend.go:81","msg":"opened backend db","path":"default.etcd/member/snap/db","took":"75.889µs"}
{"level":"info","ts":"2022-06-05T22:46:31.752+0800","caller":"etcdserver/server.go:529","msg":"No snapshot found. Recovering WAL from scratch!"}
{"level":"info","ts":"2022-06-05T22:46:31.753+0800","caller":"etcdserver/raft.go:483","msg":"restarting local member","cluster-id":"cdf818194e3a8c32","local-member-id":"8e9e05c52164694d","commit-index":6}
{"level":"info","ts":"2022-06-05T22:46:31.753+0800","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d switched to configuration voters=()"}
{"level":"info","ts":"2022-06-05T22:46:31.753+0800","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d became follower at term 3"}
{"level":"info","ts":"2022-06-05T22:46:31.753+0800","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"newRaft 8e9e05c52164694d [peers: [], term: 3, commit: 6, applied: 0, lastindex: 6, lastterm: 3]"}
{"level":"warn","ts":"2022-06-05T22:46:31.754+0800","caller":"auth/store.go:1220","msg":"simple token is not cryptographically signed"}
{"level":"info","ts":"2022-06-05T22:46:31.754+0800","caller":"mvcc/kvstore.go:415","msg":"kvstore restored","current-rev":1}
{"level":"info","ts":"2022-06-05T22:46:31.755+0800","caller":"etcdserver/quota.go:94","msg":"enabled backend quota with default value","quota-name":"v3-applier","quota-size-bytes":2147483648,"quota-size":"2.1 GB"}
{"level":"info","ts":"2022-06-05T22:46:31.755+0800","caller":"etcdserver/server.go:851","msg":"starting etcd server","local-member-id":"8e9e05c52164694d","local-server-version":"3.5.4","cluster-version":"to_be_decided"}
{"level":"info","ts":"2022-06-05T22:46:31.757+0800","caller":"embed/etcd.go:277","msg":"now serving peer/client/metrics","local-member-id":"8e9e05c52164694d","initial-advertise-peer-urls":["http://localhost:2380"],"listen-peer-urls":["http://localhost:2380"],"advertise-client-urls":["http://localhost:2379"],"listen-client-urls":["http://localhost:2379"],"listen-metrics-urls":[]}
{"level":"info","ts":"2022-06-05T22:46:31.757+0800","caller":"etcdserver/server.go:752","msg":"starting initial election tick advance","election-ticks":10}
{"level":"info","ts":"2022-06-05T22:46:31.758+0800","caller":"embed/etcd.go:581","msg":"serving peer traffic","address":"127.0.0.1:2380"}
{"level":"info","ts":"2022-06-05T22:46:31.758+0800","caller":"embed/etcd.go:553","msg":"cmux::serve","address":"127.0.0.1:2380"}
{"level":"info","ts":"2022-06-05T22:46:31.758+0800","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d switched to configuration voters=(10276657743932975437)"}
{"level":"info","ts":"2022-06-05T22:46:31.758+0800","caller":"membership/cluster.go:421","msg":"added member","cluster-id":"cdf818194e3a8c32","local-member-id":"8e9e05c52164694d","added-peer-id":"8e9e05c52164694d","added-peer-peer-urls":["http://localhost:2380"]}
{"level":"info","ts":"2022-06-05T22:46:31.758+0800","caller":"membership/cluster.go:584","msg":"set initial cluster version","cluster-id":"cdf818194e3a8c32","local-member-id":"8e9e05c52164694d","cluster-version":"3.5"}
{"level":"info","ts":"2022-06-05T22:46:31.758+0800","caller":"api/capability.go:75","msg":"enabled capabilities for version","cluster-version":"3.5"}
{"level":"info","ts":"2022-06-05T22:46:33.453+0800","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d is starting a new election at term 3"}
{"level":"info","ts":"2022-06-05T22:46:33.453+0800","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d became pre-candidate at term 3"}
{"level":"info","ts":"2022-06-05T22:46:33.454+0800","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d received MsgPreVoteResp from 8e9e05c52164694d at term 3"}
{"level":"info","ts":"2022-06-05T22:46:33.454+0800","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d became candidate at term 4"}
{"level":"info","ts":"2022-06-05T22:46:33.454+0800","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d received MsgVoteResp from 8e9e05c52164694d at term 4"}
{"level":"info","ts":"2022-06-05T22:46:33.454+0800","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"8e9e05c52164694d became leader at term 4"}
{"level":"info","ts":"2022-06-05T22:46:33.454+0800","logger":"raft","caller":"etcdserver/zap_raft.go:77","msg":"raft.node: 8e9e05c52164694d elected leader 8e9e05c52164694d at term 4"}
{"level":"info","ts":"2022-06-05T22:46:33.474+0800","caller":"etcdserver/server.go:2042","msg":"published local member to cluster through raft","local-member-id":"8e9e05c52164694d","local-member-attributes":"{Name:default ClientURLs:[http://localhost:2379]}","request-path":"/0/members/8e9e05c52164694d/attributes","cluster-id":"cdf818194e3a8c32","publish-timeout":"7s"}
{"level":"info","ts":"2022-06-05T22:46:33.475+0800","caller":"embed/serve.go:98","msg":"ready to serve client requests"}
{"level":"info","ts":"2022-06-05T22:46:33.475+0800","caller":"embed/serve.go:140","msg":"serving client traffic insecurely; this is strongly discouraged!","address":"127.0.0.1:2379"}
{"level":"info","ts":"2022-06-05T22:46:33.475+0800","caller":"etcdmain/main.go:44","msg":"notifying init daemon"}
{"level":"info","ts":"2022-06-05T22:46:33.475+0800","caller":"etcdmain/main.go:50","msg":"successfully notified init daemon"}

1.etcd-version:etcd的版本。
2.git-sha。
3.go-version:基于的go语言版本。
4.go-os:运行的系统。
5.go-arch:运行的系统架构。
6.max-cpu-set:设置的CPU数量。
7.max-cpu-available:最多可用的CPU数量。
8.member-initialized:集群成员是否初始化,默认false。

9.name表示节点名称,默认为default。
2.data-dir 保存日志和快照的数据目录,默认为当前工作目录default.etcd/目录下。
3.在http://localhost:2380和集群中其他节点通信。
4.在http://localhost:2379提供和客户端交互。
5.heartbeat-interval:为100ms,该参数的作用是leader多久发送一次心跳到followers,默认值是100ms。
6.election-timeout:为1000ms,该参数的作用是重新投票的超时时间,如果follow在该时间间隔没有收到心跳包,会触发重新投票,默认为1000ms。
7.snapshot-count:为10000,该参数的作用是指定有多少事务被提交时,触发截取快照保存到磁盘。
8.集群和每个节点都会生成一个uuid,且固定不变,`cluster-id`:集群UUID,`local-member-id`:本机UUID。

{"level":"info","ts":"2022-06-05T22:46:31.758+0800","caller":"membership/cluster.go:421","msg":"added member","cluster-id":"cdf818194e3a8c32","local-member-id":"8e9e05c52164694d","added-peer-id":"8e9e05c52164694d","added-peer-peer-urls":["http://localhost:2380"]}

9.启动的时候会运行raft,选举出leader

2.3 创建etcd数据保存文件

mkdir -p /opt/soft/etcd/etcd_data/

2.4 创建etcd配置文件

/opt/soft/etcd/下创建 etcd.conf 文件,注意要修改EtcdIp为所在服务器的Ip

vim /opt/soft/etcd/etcd.conf
#########################################################
########### 请根据各节点服务器实际情况修改配置 ##########
#########################################################
#[Member]
#1.节点名称,必须唯一
ETCD_NAME="etcd01"
 
#2.设置数据保存的目录
ETCD_DATA_DIR="/opt/soft/etcd/etcd_data"
 
#3.本节点机器用于监听其他节点的url,url是本机上的2380
ETCD_LISTEN_PEER_URLS="http://192.168.13.10:2380"
 
#4.建议本节点用于和其他节点之间通信的url,且会通告集群的其余成员节点
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.13.10:2380"

#5.本节点机器用于和客户端通信的url,url是本机上的 2379
ETCD_LISTEN_CLIENT_URLS="http://192.168.13.10:2379,http://127.0.0.1:2379"
 
#[Clustering]
#6.建议本节点和客户端通信使用的url
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.13.10:2379"
 
#7.集群中所有节点的信息
ETCD_INITIAL_CLUSTER="etcd01=http://192.168.13.10:2380"
 
#8.创建集群的token,这个值每个集群均相同
ETCD_INITIAL_CLUSTER_TOKEN="148e7b13-51fc-4cc8-965b-a7c9d58c18f5"
 
#9.初始集群状态,新建集群的时候,这个值为new,后续再启动时需要将“new”更改为“existing”
ETCD_INITIAL_CLUSTER_STATE="new"
 
#10.flannel操作etcd使用的是v2的API,而kubernetes操作etcd使用的v3的API
#   为了兼容flannel,将默认开启v2版本,故配置文件中设置 
ETCD_ENABLE_V2="true"

2.5 清除旧数据,重启服务

修改/opt/soft/etcd/etcd.conf文件后,要先删除/opt/soft/etcd/etcd_data目录下保存的数据,不然再重新启用服务会失败。

cd /opt/soft/etcd/etcd_data && rm -rf *

2.6 创建systemd配置文件【系统启动】

vim /etc/systemd/system/etcd.service

输入内容:

[Unit]
Description=Etcd Server
After=network.target
Wants=network-online.target
 
[Service]
User=root
Type=notify
## 根据实际情况修改WorkingDirectory、EnvironmentFile、ExecStart这三个参数值
## 1.WorkingDirectory:etcd数据存放的位置
WorkingDirectory=/opt/soft/etcd/etcd_data/
## 2.EnvironmentFile即配置文件的位置,注意“-”不能少
EnvironmentFile=-/opt/soft/etcd/etcd.conf
## 3.ExecStart即etcd启动程序位置
ExecStart=/usr/local/bin/etcd
Restart=always
RestartSec=10s
LimitNOFILE=65536
 
[Install]
WantedBy=multi-user.target

2.7 启动/查看etcd服务

systemctl daemon-reload && systemctl enable etcd && systemctl start etcd && systemctl status etcd

附:

systemctl daemon-reload
systemctl enable etcd
systemctl start etcd
systemctl status etcd
systemctl stop etcd
systemctl restart etcd

3 Etcd命令

3.1 查询Etcd所有的key

etcdctl get --prefix ""

3.2 删除Etcd的key

etcdctl del key
 类似资料: