Reputation: 1183
For performance reasons I need to run multiple instances of an app, pinned to CPU, listening on different ports. An HAProxy TCP load balancer sits in front of them to distribute the traffic.
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).
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.
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, e.g.
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).
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.
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.
Is that possible? Or does that go beyond what systemd can provide?
Upvotes: 4
Views: 6488
Reputation: 6850
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. 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.
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). Here is a tutorial about templates and systemd in general.
From man systemd.unit
(shortened):
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.
Upvotes: 8