linux - systemd 能否自动重启/停止依赖服务?

标签 linux multithreading real-time systemd system-dependent

出于性能原因,我需要运行一个应用程序的多个实例,固定到 CPU,监听不同的端口。一个 HAProxy TCP 负载平衡器位于它们前面以分配流量。

这样做是为了防止任何线程上下文切换并强制执行无共享设计(因此在应用程序中不需要任何类型的锁,假设它是单线程的)。

这意味着在具有 64 个 CPU 的服务器上,我可能将 HAProxy 固定到 CPU 0,然后我的应用程序的 63 个实例分别固定到单独的 CPU (1-63)。

显然,在启动、重启、关闭等方面管理起来相当复杂。

我想知道是否有任何方法可以使用 systemd 来为我处理这种复杂性。

我知道如果我将 HAProxy 定义为一个单元然后声明它需要它需要与之通信的其他应用程序,它可以解决启动问题,例如

Require=app1,app2,.....,app63

我能做到

 systemctl start myhaproxy

它会首先启动它需要的 63 个实例(假设它们中的每一个在应用程序安装期间都被定义为一个单独的 systemd 单元)。

但是,我想知道是否有办法让它也适用于重启和关机。

如果我这样做:

systemctl stop myhaproxy

我希望它自动关闭所有 63 个与之对话的应用程序实例。

如果我这样做

systemctl restart myhaproxy

然后我希望它在最后重新启动自己之前首先重新启动 Require 中列出的所有服务。

这可能吗?或者这是否超出了 systemd 所能提供的范围?

最佳答案

我建议您创建一个目标(例如app-all.target)并且您所有的应用程序单元都将具有WantedBy=app-all.target 对它的依赖。这将确保如果您启动目标,它将启动您的所有应用程序单元。但是,这不适用于停止和重新启动。为此,您需要为每个应用程序单元添加 PartOf=app-all.target 依赖项。

此外,我建议您为您的应用程序创建一个模板单元,然后创建它的 63 个实例 - 这将使管理更容易(只有 1 个配置文件,其中包含 63 个符号链接(symbolic link))。这是一个tutorial关于模板和 systemd 的一般信息。

来自man systemd.unit(缩写):

WantedBy=

A symbolic link is created in the .wants/ or .requires/ directory of each of the listed units when this unit is installed by systemctl enable. The primary result is that the current unit will be started when the listed unit is started.

PartOf=

Configures dependencies similar to Requires=, but limited to stopping and restarting of units. When systemd stops or restarts the units listed here, the action is propagated to this unit. Note that this is a one-way dependency -- changes to this unit do not affect the listed units.

关于linux - systemd 能否自动重启/停止依赖服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38112638/

相关文章:

linux - linux内核模块的读/写锁

C# 计时器与服务中的线程

c - C 中的 fork() 流程

JavaFX UI 创建和进度条更新

android - 如何制作实时多折线图?

Python 从 csv 中删除负数条目/行

linux - VT-100 命令运行异常

regex - Linux 上的 Sed 命令只匹配 1 个,而在 Solaris 上会找到多个匹配项

linux - POSIX 实时信号能否实现软实时?

firebase-realtime-database - 使用 Firebase 的两人象棋游戏