大网数据广告活动
服务器运维问答

Always save HAProxy server state before stop

· 52CE 我爱测

We sometimes disable specific servers in HAProxy (running on RedHat 7, but compiled locally because 1.8 is not available from RedHat) using an admin socket and we want the state to be maintained if the server is restarted. We set…

问题原文

原帖链接 · yakatz · CC BY-SA 3.0

We sometimes disable specific servers in HAProxy (running on RedHat 7, but compiled locally because 1.8 is not available from RedHat) using an admin socket and we want the state to be maintained if the server is restarted. We set HAProxy to load its state from a file with server-state-file, but we need to remember to save the state file every time we make a change. Is there a good way to make sure the state is saved every time the server stops?
If systemd had ExecPreStop=, we could include it from a file in /etc/systemd/system without modifying the distributed unit file, but systemd developers said they don't see the need for that directive.

已采纳解答

原帖链接 · yakatz · CC BY-SA 3.0

We ended up creating a "fake" service with just a stop action that depends on haproxy.service and therefor will have its ExecStop run before HAProxy is stopped.

haproxy-state.service :

[Unit]
Description=Save HAProxy State Before Service Stop
After=haproxy.service
Requires=haproxy.service

[Service]
Type=oneshot
RemainAfterExit=true
ExecStop=/bin/sh -c "/usr/bin/socat /run/haproxy/control.socket - <<< \"show servers state\" > /var/lib/haproxy/last_state"

[Install]
WantedBy=multi-user.target haproxy.service

It needs to be after haproxy.service so it will stop before. It needs to be WantedBy so that it will always be started any time HAProxy is running.

This isn't perfect because the state could change if there is a long pause before systemd stops the next service, but that is a risk we will take.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-04-18)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。