简体   繁体   English

systemd可以自动重启/停止依赖服务吗?

[英]Can systemd automatically restart / stop dependent services?

For performance reasons I need to run multiple instances of an app, pinned to CPU, listening on different ports. 出于性能原因,我需要运行一个应用程序的多个实例,这些实例固定在CPU上,并在不同的端口上侦听。 An HAProxy TCP load balancer sits in front of them to distribute the traffic. HAProxy TCP负载平衡器位于它们前面,以分配流量。

This is done to prevent any thread context switching and to enforce a shared-nothing design (so no need for any sort of locks within an application, assuming it is single threaded). 这样做是为了防止任何线程上下文切换并强制执行无共享设计(因此,假设应用程序是单线程的,则在应用程序内不需要任何类型的锁)。

So that means on a server with 64 CPUs, I may have HAProxy pinned to CPU 0 and then 63 instances of my app each pinned to separate CPUs (1-63). 因此,这意味着在具有64个CPU的服务器上,我可能会将HAProxy固定到CPU 0,然后将我的应用程序的63个实例分别固定到单独的CPU(1-63)。

Obviously that is quite complex to manage in terms of startup, restart, shutdown, etc. 显然,就启动,重新启动,关闭等而言,管理起来非常复杂。

I was wondering if there is any way I could use systemd to handle this complexity for me. 我想知道是否可以使用systemd为我处理这种复杂性。

I know that if I defined HAProxy as a unit and then stated it Requires the other apps it needs to talk to, it could solve the start up problem, eg 我知道,如果我将HAProxy定义为一个单元,然后说它需要与之对话的其他应用程序,则可以解决启动问题,例如

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

I could do 我可以做

 systemctl start myhaproxy

and it would first start the 63 instances it requires (assuming each of them is defined as a separate systemd unit during app installation). 并且它将首先启动所需的63个实例(假设在安装应用程序时将每个实例定义为一个单独的systemd单元)。

However, I am wondering if there is anyway I could get this to also work for restart and shutdown. 但是,我想知道是否无论如何我可以使它也可以重新启动和关闭。

So if I do: 因此,如果我这样做:

systemctl stop myhaproxy

I would like it to automatically shutdown all the 63 instances of the app it talks to. 我希望它自动关闭与之通信的应用程序的所有63个实例。

And if I do 如果我愿意

systemctl restart myhaproxy

then I would like it to first restart all the services listed in Require before restarting itself at the end. 那么我希望它首先重新启动Require中列出的所有服务,然后最后重新启动自己。

Is that possible? 那可能吗? Or does that go beyond what systemd can provide? 还是超出了systemd可以提供的范围?

I would suggest you create a target ( app-all.target for example) and all your application units will have WantedBy=app-all.target dependency on it. 我建议您创建一个目标 (例如app-all.target ),所有应用程序单元都WantedBy=app-all.target它具有WantedBy=app-all.target依赖性。 This will ensure that if you start the target, it will start all your application units. 这将确保如果启动目标,它将启动所有应用程序单元。 This however does not work for stopping and restarting. 但是,这对于停止和重新启动不起作用。 For that, you need to add PartOf=app-all.target dependency to each app unit. 为此,您需要向每个应用程序单元添加PartOf=app-all.target依赖关系。

Also I would suggest that you create a template unit for your app and then create 63 instances of it - it will make the management easier (only 1 config file with 63 symlinks into it). 我也建议您为应用程序创建一个模板单元,然后创建它的63个实例-这将使管理更加容易(只有1个配置文件和63个符号链接)。 Here is a tutorial about templates and systemd in general. 这是有关模板和systemd的一般教程

From man systemd.unit (shortened): man systemd.unit (简称):

WantedBy= 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. 通过systemctl enable安装每个列出的单元的.wants /或.requires /目录中会创建一个符号链接。 The primary result is that the current unit will be started when the listed unit is started. 主要结果是,当启动列出的单元时,将启动当前单元。

PartOf= PartOf =

Configures dependencies similar to Requires=, but limited to stopping and restarting of units. 配置与Requires =类似的依赖关系,但仅限于停止和重新启动单元。 When systemd stops or restarts the units listed here, the action is propagated to this unit. 当systemd停止或重新启动此处列出的单元时,操作将传播到该单元。 Note that this is a one-way dependency -- changes to this unit do not affect the listed units. 请注意,这是一种单向依赖关系,对此单位的更改不会影响列出的单位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM