blob: b6ed19335d90469c90e62bce9abc264a4a76418f [file] [log] [blame]
Sean Dague5edae542017-03-21 20:50:24 -04001===========================
2 Using Systemd in DevStack
3===========================
4
5.. note::
6
7 This is an in progress document as we work out the way forward here
8 with DevStack and systemd.
9
10DevStack can be run with all the services as systemd unit
11files. Systemd is now the default init system for nearly every Linux
12distro, and systemd encodes and solves many of the problems related to
13poorly running processes.
14
15Why this instead of screen?
16===========================
17
18The screen model for DevStack was invented when the number of services
19that a DevStack user was going to run was typically < 10. This made
20screen hot keys to jump around very easy. However, the landscape has
21changed (not all services are stoppable in screen as some are under
22Apache, there are typically at least 20 items)
23
24There is also a common developer workflow of changing code in more
25than one service, and needing to restart a bunch of services for that
26to take effect.
27
28To enable this add the following to your local.conf::
29
30 USE_SYSTEMD=True
31
32
33
34Unit Structure
35==============
36
37.. note::
38
39 Originally we actually wanted to do this as user units, however
40 there are issues with running this under non interactive
41 shells. For now, we'll be running as system units. Some user unit
42 code is left in place in case we can switch back later.
43
44All DevStack user units are created as a part of the DevStack slice
45given the name ``devstack@$servicename.service``. This lets us do
46certain operations at the slice level.
47
48Manipulating Units
49==================
50
51Assuming the unit ``n-cpu`` to make the examples more clear.
52
53Enable a unit (allows it to be started)::
54
55 sudo systemctl enable devstack@n-cpu.service
56
57Disable a unit::
58
59 sudo systemctl disable devstack@n-cpu.service
60
61Start a unit::
62
63 sudo systemctl start devstack@n-cpu.service
64
65Stop a unit::
66
67 sudo systemctl stop devstack@n-cpu.service
68
69Restart a unit::
70
71 sudo systemctl restart devstack@n-cpu.service
72
73See status of a unit::
74
75 sudo systemctl status devstack@n-cpu.service
76
77
78Querying Logs
79=============
80
81One of the other major things that comes with systemd is journald, a
82consolidated way to access logs (including querying through structured
83metadata). This is accessed by the user via ``journalctl`` command.
84
85
86Logs can be accessed through ``journalctl``. journalctl has powerful
87query facilities. We'll start with some common options.
88
89Follow logs for a specific service::
90
91 journalctl -f --unit devstack@n-cpu.service
92
93Following logs for multiple services simultaneously::
94
95 journalctl -f --unit devstack@n-cpu.service --user-unit
96 devstack@n-cond.service
97
98Use higher precision time stamps::
99
100 journalctl -f -o short-precise --unit devstack@n-cpu.service
101
102
103Known Issues
104============
105
106Be careful about systemd python libraries. There are 3 of them on
107pypi, and they are all very different. They unfortunately all install
108into the ``systemd`` namespace, which can cause some issues.
109
110- ``systemd-python`` - this is the upstream maintained library, it has
111 a version number like systemd itself (currently ``233``). This is
112 the one you want.
113- ``systemd`` - a python 3 only library, not what you want.
114- ``python-systemd`` - another library you don't want. Installing it
115 on a system will break ansible's ability to run.
116
117
118If we were using user units, the ``[Service]`` - ``Group=`` parameter
119doesn't seem to work with user units, even though the documentation
120says that it should. This means that we will need to do an explicit
121``/usr/bin/sg``. This has the downside of making the SYSLOG_IDENTIFIER
122be ``sg``. We can explicitly set that with ``SyslogIdentifier=``, but
123it's really unfortunate that we're going to need this work
124around. This is currently not a problem because we're only using
125system units.
126
127Future Work
128===========
129
130oslo.log journald
131-----------------
132
133Journald has an extremely rich mechanism for direct logging including
134structured metadata. We should enhance oslo.log to take advantage of
135that. It would let us do things like::
136
137 journalctl REQUEST_ID=......
138
139 journalctl INSTANCE_ID=......
140
141And get all lines related to the request id or instance id.
142
143sub targets/slices
144------------------
145
146We might want to create per project slices so that it's easy to
147follow, restart all services of a single project (like swift) without
148impacting other services.
149
150log colorizing
151--------------
152
153We lose log colorization through this process. We might want to build
154a custom colorizer that we could run journalctl output through
155optionally for people.
156
157user units
158----------
159
160It would be great if we could do services as user units, so that there
161is a clear separation of code being run as not root, to ensure running
162as root never accidentally gets baked in as an assumption to
163services. However, user units interact poorly with devstack-gate and
164the way that commands are run as users with ansible and su.
165
166Maybe someday we can figure that out.
167
168References
169==========
170
171- Arch Linux Wiki - https://wiki.archlinux.org/index.php/Systemd/User
172- Python interface to journald -
173 https://www.freedesktop.org/software/systemd/python-systemd/journal.html
174- Systemd documentation on service files -
175 https://www.freedesktop.org/software/systemd/man/systemd.service.html
176- Systemd documentation on exec (can be used to impact service runs) -
177 https://www.freedesktop.org/software/systemd/man/systemd.exec.html