Linux 与服务器运维 / 系统课时
加入环境变量和外部配置
加入环境变量和外部配置
把环境相关参数直接写进 ExecStart 会让 Unit 难读,也让不同环境之间的变更难以管理。本节使用 EnvironmentFile 把运行参数移到独立文件,让应用仍通过普通环境变量读取配置,并验证修改配置后服务确实获得了新值。
学习目标
- 能创建环境变量文件并用
EnvironmentFile在 Service Unit 中引用 - 能让服务进程读取外部环境参数,而不是把参数硬编码进启动命令
- 能区分修改 Unit 与修改环境文件时所需的重载、重启动作
准备读取环境变量的服务
为了让本节可以独立验证,先创建专用账号和一个最小脚本。脚本要求三个变量存在,启动时把读取结果写到标准输出,随后保持运行。
getent group hello-worker-env >/dev/null || sudo groupadd --system hello-worker-env
id -u hello-worker-env >/dev/null 2>&1 || sudo useradd --system --gid hello-worker-env --home-dir /var/lib/hello-worker-env --create-home --shell /usr/sbin/nologin hello-worker-env
sudo install -d -o root -g root -m 0755 /opt/hello-worker-env/bin
sudo tee /opt/hello-worker-env/bin/worker.sh >/dev/null <<'EOF'
#!/bin/sh
set -eu
: "${APP_MODE:?APP_MODE is required}"
: "${POLL_INTERVAL:?POLL_INTERVAL is required}"
: "${MESSAGE:?MESSAGE is required}"
echo "mode=$APP_MODE interval=$POLL_INTERVAL message=$MESSAGE"
while :; do
sleep "$POLL_INTERVAL"
done
EOF
sudo chmod 0755 /opt/hello-worker-env/bin/worker.sh
echo "app-prepared"预期输出:
app-prepared如果任何必需变量缺失,Shell 会在启动阶段直接退出,systemd 就能把它识别为服务失败,而不是让应用带着空配置继续运行。如果本机 nologin 路径不同,可通过 command -v nologin 查询后调整账号创建命令。
创建 EnvironmentFile
环境文件保存变量赋值,不需要在每一行写 export。下面把它放在 /etc/hello-worker-env/worker.env,并限制为 root 可读。
sudo install -d -o root -g root -m 0755 /etc/hello-worker-env
sudo tee /etc/hello-worker-env/worker.env >/dev/null <<'EOF'
APP_MODE=production
POLL_INTERVAL=2
MESSAGE="hello from EnvironmentFile"
EOF
sudo chmod 0600 /etc/hello-worker-env/worker.env
echo "env-file-ready"预期输出:
env-file-readyEnvironmentFile 由 systemd 在创建服务进程时读取,因此服务账号不需要自己打开这个文件。文件内容应保持为环境变量赋值,不要把它当成任意 Shell 脚本,也不要依赖命令替换等 Shell 执行行为。
在 Unit 中引用外部配置
将下面内容保存为 /etc/systemd/system/hello-worker-env.service。EnvironmentFile 使用绝对路径,随后启动的脚本就能从进程环境中读取这些值。
[Unit]
Description=Hello Worker with EnvironmentFile
[Service]
Type=simple
User=hello-worker-env
Group=hello-worker-env
WorkingDirectory=/opt/hello-worker-env
EnvironmentFile=/etc/hello-worker-env/worker.env
ExecStart=/opt/hello-worker-env/bin/worker.sh
Restart=on-failure
RestartSec=3s
[Install]
WantedBy=multi-user.target如果某个环境文件确实是可选项,可以写成 EnvironmentFile=-/path/to/file。路径前的 - 表示文件不存在时忽略该错误;对于必需配置不要这样处理,否则漏部署文件时更难及时发现。
安装并启动 Unit:
set -e
sudo install -o root -g root -m 0644 ./hello-worker-env.service /etc/systemd/system/hello-worker-env.service
sudo systemd-analyze verify /etc/systemd/system/hello-worker-env.service
sudo systemctl daemon-reload
sudo systemctl enable --now hello-worker-env.service >/dev/null 2>&1
echo "state=$(systemctl is-active hello-worker-env.service)"预期输出:
state=active验证服务真正读取了配置
不要只检查环境文件本身,而要验证最终进程观察到的值。应用已经在启动时把三个变量写入日志,可以从 journal 中提取这一行。
sudo journalctl -u hello-worker-env.service -o cat | grep '^mode=' | tail -n 1预期输出:
mode=production interval=2 message=hello from EnvironmentFile这一步能同时证明 Unit 引用了正确文件、systemd 成功读取变量、进程也确实收到了这些环境值。
修改配置时如何生效
只修改 worker.env 时,Unit 文件本身没有变化,因此不需要 daemon-reload;但已经运行的进程不会自动获得新的环境变量,必须重启服务,让 systemd 重新读取文件并创建新进程。
set -e
sudo sed -i 's/^APP_MODE=.*/APP_MODE=staging/' /etc/hello-worker-env/worker.env
sudo systemctl restart hello-worker-env.service
sudo journalctl -u hello-worker-env.service -o cat | grep '^mode=' | tail -n 1预期输出:
mode=staging interval=2 message=hello from EnvironmentFile如果修改的是 .service 文件中的 EnvironmentFile= 路径或其他 Unit 指令,则应先执行 systemctl daemon-reload,再重启服务。环境变量适合普通运行参数,但高价值秘密不应仅因为环境文件权限较严就默认安全;秘密进入进程环境后还可能被诊断工具、崩溃信息或子进程接触,应按系统的秘密管理方案单独设计。