Ales Nosek - The Software Practitioner: Practicing To Make Software Perfect
Ales Nosek - The Software Practitioner: Practicing To Make Software Perfect
Ales Nosek - The Software Practitioner: Practicing To Make Software Perfect
Blog
Archives
Is your application delivered as a set of services running on top of Linux? Did you think about writing a custom controller service that would start your application
services in the correct order and monitor their health? Please, stop thinking about it! In this blog post I would like to convince you that you can leverage the existing
systemd service manager to control your application services to your greatest benefit.
systemd is the default service manager on all major Linux distributions. We’re going to demonstrate how it can be used to control a custom multi-service application.
Our application consists of three services: Service 1, Service 2 and Service 3. The following set of requirements should be met when controlling the services using
systemd:
1 de 5 11/17/2017 10:24 AM
Controlling a Multi-Service Application with systemd - Ales Nosek - Th... http://alesnosek.com/blog/2016/12/04/controlling-a-multi-service-applica...
app.service
1 [Unit]
2 Description=Application
3
4 [Service]
5 # The dummy program will exit
6 Type=oneshot
7 # Execute a dummy program
8 ExecStart=/bin/true
9 # This service shall be considered active after start
10 RemainAfterExit=yes
11
12 [Install]
13 # Components of this application should be started at boot time
14 WantedBy=multi-user.target
In the next step, we’ll create systemd unit files for the three services that constitute our application. I included some explanatory comments in the first app-component1
service definition. The definitions of the remaining two services app-component2 and app-component3 follow the same schema.
app-component1.service
1 [Unit]
2 Description=Application Component 1
3 # When systemd stops or restarts the app.service, the action is propagated to this unit
4 PartOf=app.service
5 # Start this unit after the app.service start
6 After=app.service
7
8 [Service]
9 # Pretend that the component is running
10 ExecStart=/bin/sleep infinity
11 # Restart the service on non-zero exit code when terminated by a signal other than SIGHUP, SIGINT, SIGTERM or SIGPIPE
12 Restart=on-failure
13
14 [Install]
15 # This unit should start when app.service is starting
16 WantedBy=app.service
The definition of the service app-component2 resembles the definition of service app-component1:
app-component2.service
1 [Unit]
2 Description=Application Component 2
3 PartOf=app.service
4 After=app.service
5
6 [Service]
7 ExecStart=/bin/sleep infinity
8 Restart=on-failure
9
10 [Install]
11 WantedBy=app.service
We would like the service app-component3 to start after the service app-component2. Systemd provides the directive After to configure the start ordering. Note that we
2 de 5 11/17/2017 10:24 AM
Controlling a Multi-Service Application with systemd - Ales Nosek - Th... http://alesnosek.com/blog/2016/12/04/controlling-a-multi-service-applica...
don’t use a Wants directive to create a dependency of app-component3 on app-component2. This Wants dependency would instruct systemd to start the app-component2
whenever the app-component3 should be started. The app-component2 would be started even in the case that it was disabled before. This is however not what we wanted
as we require the user to be able to permanently disable any of the components. If app-component2 is not enabled, systemd should just skip it when starting the
application services.
app-component3.service
1 [Unit]
2 Description=Application Component 3
3 PartOf=app.service
4 After=app.service
5 # This unit should start after the app-component2 started
6 After=app-component2.service
7
8 [Service]
9 ExecStart=/bin/sleep infinity
10 Restart=on-failure
11
12 [Install]
13 WantedBy=app.service
If everything went well, you should be able to see the new services in the unit file list:
The systemd status command should display all the services as active:
Note that while the component services are marked as running, the pseudo-service app is showed as exited. This is an expected behaviour as the service app is of type
oneshot. (I’m not showing the output of the systemctl status command here).
Next, let’s check the starting order of the services. Systemd logs its messages into the journal. Let’s list the recent log entries with:
1 $ journalctl -e
In our sample output we can see that the service app-component3 was indeed started after the service app-component2:
3 de 5 11/17/2017 10:24 AM
Controlling a Multi-Service Application with systemd - Ales Nosek - Th... http://alesnosek.com/blog/2016/12/04/controlling-a-multi-service-applica...
Now we’re going to check that we can disable any service independently of other services. Let’s stop the service app-component2 and disable it:
When we stop and start our application the service app-component2 will remain disabled.
Unfortunately, I realized that when using the restart command, systemd will enable the service app-component2:
The behaviour of the systemd stop command followed by the start command is not consistent with the behaviour of the systemd restart command. As a workaround,
you can use the systemd mask command to really disable the application service, for example:
Conclusion
systemd provides a feature-rich service manager. Instead of implementing a home-grown solution you might want to think about using systemd to control your application
services. Some of the benefits of opting for systemd are:
Have fun!
Tweet
Like Share Be the first of your friends to like this.
Comments
4 de 5 11/17/2017 10:24 AM
Controlling a Multi-Service Application with systemd - Ales Nosek - Th... http://alesnosek.com/blog/2016/12/04/controlling-a-multi-service-applica...
LOG IN WITH
OR SIGN UP WITH DISQUS ?
ALESNOSEK.COM
OpenStack Dynamic DNS Updates - Ales Nosek - The Software How I Became a Red Hat Certified Architect in One Year - Ales Nosek
Practitioner - The Software Practitioner
• •
— Glad that you liked my blogpost. If I understand you'd like to know — Definitely! If you're interested in participating in any of
how to modify the openstack-dns-updater.py script to use the key file. I … the partner events, there are a couple coming up. Ansiblefest is next week, and
the …
Passed the OpenShift EX280 certification! - Ales Nosek - The Deploying Kubernetes on OpenStack using Heat - Ales Nosek - The
Software Practitioner Software Practitioner
• •
— Hey Srinivas, did you complete the test? I am able to complete — Is theire a way to scale minion after
all the labs in DO280 but I am worried about all the syntax of YAML and … deployment ?
Recent Posts
5 de 5 11/17/2017 10:24 AM