https://etcd.io/docs/v3.6/install/


Install pre-built binaries

  1. 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
  1. Unpack the archive file. This results in a directory containing the binaries.
tar xvf etcd-${ETCD_VER}-linux-amd64.tar.gz
  1. 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/
  1. From a shell, test that etcd is in your path:
$ etcd --version
etcd Version: 3.6.6
...


Add etcd to systemd

  1. Start by creating a configuration and data directory for the etcd service.
$ sudo mkdir -p /var/lib/etcd/default
$ sudo mkdir /etc/etcd/
  1. Change the ownership of the /var/lib/etcd directory to your target user.
$ sudo useradd etcd -r -s /sbin/nologin
$ sudo chown -R etcd:etcd /var/lib/etcd
  1. Create /etc/systemd/system/etcd.service and 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
  1. 配置文件 /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 \
"
  1. Run the etcd
sudo systemctl daemon-reload
sudo systemctl start etcd


%psystemd 提供的一种说明符 (specifier),可以把它看作一个动态变量

  • 含义: 它代表了单元的 “前缀名” (Prefix Name)“主名” (Primary Name)。具体来说,就是单元文件名中去掉类型后缀 (.service, .target 等) 的部分。
  • 在您的例子中:
    • 您的服务文件名是 etcd.service
    • systemd 解析这个文件时,它会把 %p 自动替换为 etcd
    • 因此,EnvironmentFile=-/etc/default/%p 这一行最终被解释为 EnvironmentFile=-/etc/default/etcd


EnvironmentFile=:加载环境变量文件

这个指令告诉 systemd 在启动服务之前,从一个指定的文件中读取环境变量。

  • 作用:

    1. systemd 会打开这个文件。
    2. 它会逐行读取文件内容,寻找 KEY=VALUE 格式的行。
    3. 对于每一个有效的键值对,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 就会被替换为上面定义的长字符串。

最后修改日期: 21 3 月, 2026

留言

撰写回覆或留言

发布留言必须填写的电子邮件地址不会公开。

允许上传的最大文件为80 MB。 您可以上传:图像, 音频, 视频, 文档, 电子表格, 互动, 文本, 存档, 代码, 其他 评论文本中插入的YouTube、Facebook、Twitter和其他服务的链接将自动嵌入。 Drop files here