1

For reasons above my paygrade, I need to run multiple instances of a DB server on a physical host with a bare metal OS. I also need to limit the physical memory use of each instance.

I am using a template unit file to start up a few dozen instances. I am using EnvironmentFile to specify different environment variables and command line arguments for each instance.

However, I also want to be able to specify a different memory limit for each instance. AFAICT, environment variable substitution does not work for resource limit options like MemoryHigh.

I thought drop-in files would be the answer, but I couldn't a single example of using drop-in files with an instantiated service. Is there some way I can create one drop-in file for myservice@1, a different drop-in file for myservice@2, etc?

$ cat /etc/systemd/system/myservice@.service

[Unit]
Description=Something
After=syslog.target network.target

[Service]
Type=forking    
EnvironmentFile=/etc/myservice/myservice.%i.env
ExecStart=/usr/bin/myservice $OPTIONS_FROM_ENV
MemoryHigh=4G

[Install]
WantedBy=multi-user.target
twblamer
  • 417
  • 1
  • 4
  • 7
  • I have no idea if systemd can currently do this. I would like to see any workarounds. – Michael Hampton Feb 15 '18 at 18:28
  • I created a few separate service files with pre-defined resource limits, e.g. "myservice-large@.service", "myservice-medium@.service", "myservice-small@.service", etc. Obviously this is painful from a management perspective, now I have to know/remember/guess which service is running which instance. – twblamer Feb 16 '18 at 00:06

1 Answers1

1

I think I found what I'm looking for in this answer: https://serverfault.com/a/879705/62991

Just create a drop-in directory with the instance name, and you can override settings there just like you can for a non-instantiated service.

In the below example, instances of myservice have a 4GiB memory limit by default, but myservice@1 and myservice@2 have their own settings. Works fine with systemd 231 on Fedora 25.

$ cat /etc/systemd/system/myservice@.service
[Unit]
Description=Something
After=syslog.target network.target

[Service]
Type=forking    
EnvironmentFile=/etc/myservice/myservice.%i.env
ExecStart=/usr/bin/myservice $OPTIONS_FROM_ENV
MemoryHigh=4G

[Install]
WantedBy=multi-user.target

$ cat /etc/systemd/system/myservice@1.service.d/99-memory.conf
[Service]
MemoryHigh=10G

$ cat /etc/systemd/system/myservice@2.service.d/99-memory.conf
[Service]
MemoryHigh=20G
twblamer
  • 417
  • 1
  • 4
  • 7