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