https://etcd.io/docs/v3.6/install/
Install pre-built binaries
- Download the compressed archive file for your platform from Releases
ETCD_VER=v3.6.6
# choose either URL
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 /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
- Unpack the archive file. This results in a directory containing the binaries.
tar xvf etcd-${ETCD_VER}-linux-amd64.tar.gz
- Add the executable binaries to your path. For example, rename and/or move the binaries to a directory in your path (like
/usr/local/bin), or add the directory created by the previous step to your path.
cd etcd-${ETCD_VER}-linux-amd64
sudo mv etcd etcdctl etcdutl /usr/local/bin/
- From a shell, test that
etcdis in your path:
$ etcd --version
etcd Version: 3.6.6
...
Add etcd to systemd
- Start by creating a configuration and data directory for the etcd service.
$ sudo mkdir -p /var/lib/etcd/default
$ sudo mkdir /etc/etcd/
- Change the ownership of the
/var/lib/etcddirectory to your target user.
$ sudo useradd etcd -r -s /sbin/nologin
$ sudo chown -R etcd:etcd /var/lib/etcd
- Create
/etc/systemd/system/etcd.serviceand add the following content to it.
[Unit]
Description=etcd - highly-available key value store
Documentation=https://etcd.io/docs
After=network.target
Wants=network-online.target
[Service]
# 使用专用的、非登录的系统用户
User=etcd
Group=etcd
# 从可选的配置文件中加载环境变量
# 例如: /etc/default/etcd
EnvironmentFile=-/etc/default/%p
# 设置进程的工作目录
WorkingDirectory=/var/lib/etcd/default
Type=notify
# 启动 etcd 的主命令
# $DAEMON_ARGS 会被 EnvironmentFile 或 Environment 指令中的值替换
ExecStart=/usr/local/bin/etcd $DAEMON_ARGS
Restart=on-abnormal
RestartSec=10s
# 为 etcd 设置一个高的文件描述符限制
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
- 配置文件
/etc/default/etcd
A single-node etcd instance (used for development or simple applications).
DAEMON_ARGS="\
--name etcd-node1 \
--data-dir /var/lib/etcd/default \
--listen-client-urls http://172.20.42.221:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://172.20.42.221:2379 \
"
- Run the etcd
sudo systemctl daemon-reload
sudo systemctl start etcd
%p 是 systemd 提供的一种说明符 (specifier),可以把它看作一个动态变量。
- 含义: 它代表了单元的 “前缀名” (Prefix Name) 或 “主名” (Primary Name)。具体来说,就是单元文件名中去掉类型后缀 (
.service,.target等) 的部分。 - 在您的例子中:
- 您的服务文件名是
etcd.service。 - 当
systemd解析这个文件时,它会把%p自动替换为etcd。 - 因此,
EnvironmentFile=-/etc/default/%p这一行最终被解释为EnvironmentFile=-/etc/default/etcd。
- 您的服务文件名是
EnvironmentFile=:加载环境变量文件
这个指令告诉 systemd 在启动服务之前,从一个指定的文件中读取环境变量。
-
作用:
systemd会打开这个文件。- 它会逐行读取文件内容,寻找
KEY=VALUE格式的行。 - 对于每一个有效的键值对,
systemd都会为即将启动的服务进程设置一个同名的环境变量。
-
文件格式: 这个文件就是一个简单的键值对列表,可以使用
#开头作为注释。例如,/etc/default/etcd文件的内容可以是:# /etc/default/etcd # # Etcd daemon arguments DAEMON_ARGS="--name my-etcd-1 --initial-cluster-token etcd-cluster-1 --listen-client-urls http://0.0.0.0:2379" # Other environment variables ETCD_HEARTBEAT_INTERVAL=100 ETCD_ELECTION_TIMEOUT=1000当服务启动时,
ExecStart=/usr/local/bin/etcd $DAEMON_ARGS命令中的$DAEMON_ARGS就会被替换为上面定义的长字符串。
留言