Reprint: http://www.csdn.net/article/2015-01-08/2823477
Summary: The CoreOS is a highly streamlined system core and a custom built-in operating system. ThoughtWorks's software engineer Linfan will bring the "walk to the Cloud: CoreOS Practice Guide" series of articles on CoreOS essence and recommended practice methods. This article is based on the third article: System service Butler SYSTEMD.
"Editor 's note" As an operating system, CoreOS uses a highly streamlined system core and peripheral customization to implement many features that would otherwise require complex manual operations or third-party software support at the operating system level, while rejecting other non-core software for server systems. such as GUI and package manager. Linfan, a software engineer from ThoughtWorks, will bring the "walk to the Cloud: a CoreOS Practice Guide" series of articles that show you the essence of CoreOS and recommended practices. This article is based on the third article: System service Butler SYSTEMD.
Author Profile:
Linfan, an IT siege lion born in Gen Y, thoughtworks Chengdu Office Cloudops team member, usually likes to study devops-related applications in his spare time and is currently preparing for AWS Certification and promotion of Docker-related technologies.
In the first chapter of the series, we have mentioned Systemd, whose main design goal is to overcome the inherent shortcomings of traditional Linux mainstream launcher sysvinit, and to improve the system startup speed. Compared to the similar sysvinit competitors, such as Ubuntu upstart,systemd design more avant-garde, simply speaking, its design ideas borrowed from the Mac system startup program launchd. In fact, SYSTEMD is far more than just a start-up system, it also takes over the responsibilities of system services such as start, end, status query, and log archive, and supports tasks that are timed and triggered by specific events such as inserting a particular USB device and specific port data. In the CoreOS world, the recommended practice is to use SYSTEMD to manage all user services, including those running in application containers such as Docker.
It is worth noting that SYSTEMD is not a unique service for CoreOS. In essence, SYSTEMD is a standalone project that is not attached to any Linux distribution, and because Systemd's author Lennart Poettering worked for Red Hat, the entire project was actually led by Redhat Corporation. Although Redhat Linux didn't use SYSTEMD until mid-2014, Redhat's Fedora had introduced SYSTEMD as its starting-up management program in 2011.
Before you start using SYSTEMD, find out what's special about SYSTEMD.
SYSTEMD's design philosophy
- Start fewer processes as much as possible
When the Sysvinit program initializes the system, it will run all of the background service processes that may be used. However, users need to wait for the system to start all services before they can log on. This approach poses two problems: The system's startup time is too long and system resources are wasted.
SYSTEMD provides the ability to start a service on demand so that specific services are only launched when they are actually requested. In particular, hardware-related services, such as Bluetooth services only need to run when the Bluetooth adapter is plugged in, the print service only needs to run when the printer is connected or the program is printing, and even the sshd service needs to be started only when the user connects to the server using SSH. This capability is based on the ability to listen to the SYSTEMD on the Dbus bus or a specific socket port, a design that is disruptive to the traditional boot process.
- Try to start more processes in parallel
In the era of Sysvinit, each service item number is executed in sequence with a startup script. Later Ubuntu upstart solved the parallel startup between the startup items that were not directly dependent. and systemd through the socket cache, Dbus cache and the establishment of temporary mount points and other methods to further solve the dependency between the boot process, so that all system services concurrent startup, this design is also systemd unique ideas. Of course, for user-defined services, SYSTEMD allows you to configure their startup dependent projects to ensure that the service runs in the necessary order, with detailed descriptions of how to use them later on.
SYSTEMD startup Model vs. Other Startup models
- Use Cgroup to track and manage the life cycle of processes
The full name of the Cgroup is the controller group, which is the Linux kernel feature that manages any process in a group, originally made by Google engineers and officially enabled from the Linux kernel version 2.6.24. For Android, its application isolation is the technology used. For a long time, in the broader server world, there has been no mainstream service management program that can take advantage of this feature that has already brought a wide range of benefits to the mobile phone.
And Systemd is the cgroup of the experts, its appearance just make up for the gaps in this field. The CGROUP,SYSTEMD not only enables access isolation between services, but also limits the access quotas for specific applications to system resources (such as CPU usage, amount of memory), and the precise lifecycle of management services. The specifics of how to do this are described later in this article.
- Unified Management Service Log
What you must know about using SYSTEMD is its partner: Journald Log service, which is designed to overcome the shortcomings of the existing Syslog service's log content forgery and the log format is not uniform, and it is now a standard sub-service of SYSTEMD. Journald saves all log information in binary format, and the user needs to use the JOURNALCTL command to view the log information. Later in this article, you'll see how to view the logs for a service. First Hello World Service
Introduce two concepts, unit and target first.
Unit is the basic unit of the SYSTEMD Management Service and can be thought of as a unit and is defined using a unit file. The unit file needs to contain a description of the service, attributes, and commands that need to be run. The commands that the service runs in CoreOS are usually a series of container operations, and the specific service processes are encapsulated in the container.
Target is a way to specify the service startup group in SYSTEMD (equivalent to the "RunLevel" in Sysvinit, and if it is not clear that the concept is irrelevant, search for "Linux runlevel" to find a lot of related articles). Every time the system starts, it runs all the services associated with the same level of target as the current system, and if the service does not need to follow the system to start automatically, it can completely ignore the target content. Most of our Linux users usually use the "multi-user mode" level, and the corresponding target value is "Multi-user.target".
- The unit file for the Hello World Service
Deeds Fake Bashi, now let's use SYSTEMD to create a simple system service.
In the previous section of this series, we created a cluster of 3 CoreOS virtual machine nodes, in which we only needed to use any one of them, such as the COREO-01 node. First Use SSH connection to enter this node (this method is suitable for LINUX/MAC users, for Windows users need to use Putty client, for specific reference).
vagrant ssh core-01
After successful login, the prompt becomes "[email protected] ~ $", and congratulations on your important step to CoreOS, then you can start playing in CoreOS.
SYSTEMD Convention, the unit file of the service needs to be placed in the/etc/systemd/system or/usr/lib/systemd/system directory, but because the latter directory in CoreOS is a read-only partition (the entire/ The USR directory is loaded with read-only system partitions, so we typically place user-defined unit service files in the/etc/systemd/system directory. Enter this directory, create a new file called "Hello.service", the contents into the next.
[Unit] Description=hello World after=docker.service requires=docker.service [service] timeoutstartsec=0 ExecSt Artpre=-/usr/bin/docker kill Busybox1 Execstartpre=-/usr/bin/docker RM busybox1 Execstartpre=/usr/bin/docker Pull BusyBox Execstart=/usr/bin/docker Run--name busybox1 busybox/bin/sh-c "while true; do Echo Hello world; Sleep 1; Done "execstop="/usr/bin/docker kill Busybox1 "[Install] Wantedby=multi-user.target
In this unit file, we first provide a short description of the service, and then indicate that it needs to rely on Docker services and will not run until the Docker service runs. The entire unit file is formatted with the INI file style, and the initial configuration is placed in the unit group. In the next service group, the Execstart and Execstop properties are used to specify the commands that need to be executed at runtime and at the end of the service. Finally, in the install group configuration, we specified that the service belongs to the target of Multi-user.target.
There are two places to note, first the Execstart property can contain only one primary command, and before and after this property, it is possible to use Execstartpre and execstartpost to specify more auxiliary commands, execstop the same way. Some auxiliary commands add a minus sign to indicate an error in ignoring these commands (because some "auxiliary" commands are not necessarily successful, such as trying to empty a file, but the file may not exist). Second timeoutstartsec=0 The purpose of this line is to shut down the SYSTEMD service startup time-out check, which is necessary for Docker applications, because Docker may need to download or update the image file at runtime, making the service startup time very long, This prevents Systemd from thinking that the service failed to start and kills the process.
With the unit file, you can now start the Hello World Service and enter the following command in the console:
sudo systemctl start hello.service
Tip: the. Service suffix at the end of this name can be omitted because the systemctl default suffix is. Service. The meaning of the unit file suffix is explained in detail in the subsequent advanced article.
SYSTEMD will automatically find the Hello.service file in the/usr/lib/systemd/system directory and start the services defined in it. If the unit file you created earlier is placed in a different directory, you need to use the full path of the file. It will take a while for the first time to run because Docker needs to download the required image from the network. After startup, you can see if the service is already running with the "Systemctl list-units" command (This command takes an optional parameter as the filter for the service name, and outputs all services without any parameters).
[email protected] ~ $ sudo systemctl list-units hello* UNIT LOAD ACTIVE SUB DESCRIPTION hello.service Loaded Active running Hello
World
We can also specify the service to start automatically at system startup by using the "systemctl enable" command.
sudo systemctl enable Hello.service
The target group that was previously defined is used, in effect, the enable operation simply creates a connection file under the directory of the specified target group. This can be confirmed by the following command.
[email protected] ~ $ ls-l/etc/systemd/system/multi-user.target.wants/hello.service/etc/systemd/system/ Multi-user.target.wants/hello.service-/etc/systemd/system/hello.service
When the system starts, it automatically runs all the linked services in the corresponding directory at the target level. Log Management
At this point, our first service has been playing in the background of the HA, but the good "echo Hello World"? We haven't seen any output of the service from beginning to end.
In fact, we started the service has been in the background silently output "Hello world".
SYSTEMD redirects all of its managed background processes to the log file through its standard log service Journald output to Std:out (that is, the console). The log file is in binary format, so you must use a specific tool to view it. Journald provides a companion program journalctl for processing log content. The use of Journalctl is very simple, the default without any parameters will output the system and all background process of the mixed log, commonly used parameters are--DMESG used to view the kernel output of the log,--system used to view the system output of the log,-- Unit plus the name of the unit to specify a log to output a specific unit, such as the following command.
journalctl--unit Hello.service
Other more useful parameters, such as using--follow real-time tracking log output, using--since and--until to specify the displayed log time interval, etc., can be obtained through the JOURNALCTL--help command complete parameter description. Service lifecycle We used the Systemctl start and enable two commands to start the Hello World Service in the background of the system and set it to run automatically at boot time. The actual situation we will encounter is far more than this, we will be a service process in the SYSTEMD life cycle to complement the complete.
- Activation of the service
When a new unit file is placed into the/etc/systemd/system/or/usr/lib/systemd/system/directory, it is not automatically recognized by SYSTEMD. For example, if the Hello.service file was just created, if we let SYSTEMD list all the unit.
sudo systemctl list-units
At this point in the output of the content is not found Hello.service this unit. Until we register this unit with the SYSTEMCTL's start or enable command to the SYSTEMD service list, this process is the activation of the unit.
Before the service is activated, the unit only exists as a unit file, and SYSTEMD provides the list-unit-files command to view all the unit files.
sudo systemctl list-unit-files
This command also accepts an optional parameter as the match condition for the unit name, and outputs all systemd found (that is, the two directories) in the unit file without any parameters.
PS: By the way to answer a frequently asked question, the first column of the output of this command is the unit file name, the second column is the corresponding unit boot, its value can be enabled, disable or static, where the static is God horse meaning? In fact, it means that the [Install] zone is not defined in the corresponding Unit file and therefore cannot be configured to start the service on startup.
- Start, end, forced termination, and restart of the service
Start, end, force terminate and restart, there is nothing to say, corresponding to the following several commands.
sudo systemctl start <unit name > sudo systemctl stop <unit name > sudo systemctl kill <unit name > sudo sy Stemctl Restart <unit name >
There is a trap here, until the current version of SYSTEMD (v215) and Docker (v1.4.0), when the main command of the unit is managed by the Docker container, the Systemctl kill command will not kill the service process correctly, but must use Kill-s SIGKILL to normal work, the reasons see the author in the "Imperfect CoreOS" in the analysis.
The start-up and cancellation of the service, respectively, corresponds to the following two.
sudo systemctl enable <unit name > sudo systemctl disable <unit name >
- Modification and removal of services
These two parts are more tricky in Systemd.
First, if we modify a file placed in/etc/systemd/system/, such as the output "Hello world" changed to "Bye World", after the execution of Systemctl restart, the restarted service output will remain " Hello world. " This is because when the unit file is activated, SYSTEMD will write the contents into its own cache, so in order to get the updated content, we need to tell Systemd to reread all the unit files.
sudo systemctl daemon-reload
Restart the unit again to see that the update will take effect.
Second is the unit file removal, directly after the deletion of the unit file due to the role of the cache, SYSTEMD can still continue to use this unit, even through Daemon-reload update cache, In the list-units will see this unit is only marked for Not-found, still haunted.
[email protected]e-01 ~ $ sudo systemctl list-units hello* UNIT LOAD ACTIVE SUB DESCRIPTION hello.service not -found failed failed Hello.service
At this point, we need to explicitly tell Systemd to remove these unit files that have been marked as missing.
sudo systemctl reset-failed
Now the unit is really erased from Systemd's record. Target: Cluster
SYSTEMD as the default system boot and service Manager not only have excellent parallelization capabilities, but also provide better system process tracking management capabilities, coupled with the characteristics of on-demand startup, combined with the rapid start of Docker, in the CoreOS cluster large-scale deployment of Docker The performance advantage of containers compared to other operating systems is more pronounced.
In fact, SYSTEMD's ability is far more than that, and in this article only describes its use in service management. In the next article we will continue to explore how CoreOS is extending these capabilities to large-scale clusters through the fleet service. (Author/linfan reviewer/Zhou Xiaolu)
[CoreOS Reprint] CoreOS Practice Guide (III): System service Butler SYSTEMD