SECRET OF CSS

A Guide to Monitor Docker Containers | by Dwen | Aug, 2022


Commands to help you monitor your containers better

0*27DHnoWAqvjXjqQp
Photo by Arthur Lambillotte on Unsplash

In an enterprise, business is usually not allowed to stop at will. Otherwise, it will bring huge economic losses to the enterprise.

To ensure the normal operation of the business, operation and maintenance engineers must use tools to monitor the running status of the business at all times, and the business in the container is no exception.

In addition to the container’s monitoring commands, some third-party monitoring tools are developed for the dynamic characteristics of the container.

Today, I will share information about container monitoring and its related content.

In the container, the data in the current container can be obtained and presented to the user, usually by executing commands or using third-party tools.

The installed Docker comes with some subcommands for monitoring containers, which is the container monitoring method provided by Docker developers for users.

1. docker ps command

The docker ps command is the command mentioned in the previous section, which is used to view the container status. The sample code is as follows:

In addition, the same effect can be achieved through the docker container ls command. The sample code is as follows:

2. docker topcommand

The docker top command is used to view the processes in the container. The sample code is as follows:

The above example uses the docker top command to add the container ID number to view the processes in the container.

In addition, you can also add the container name to the command to achieve the same effect.

You can display specific process information by adding parameters to the docker top command. Here, the -u parameter is used as an example. The sample code is as follows:

The above example displays the process information of the mysql1 container in a user-based format by adding the -u parameter to the docker top command.

3. docker statscommand

The docker stats command is used to query the consumption of various resources of the container. The sample code is as follows:

The above example executes the docker stats command. It displays the resource usage of each container through a dynamic list in the terminal, such as CPU usage, memory, container network, and other information.

In the case of not limiting the container memory, the host’s memory will be displayed here.

The dynamic list here has an obvious disadvantage. That is, it can only display the container ID number, not the container name.

But as long as you add the container name to the command, you can view the information of the specified container. The sample code is as follows:

Docker’s own container monitoring commands can flexibly capture real-time container information and are easy to use.

But they do not reflect trends in container resource usage and can only display limited data.

Sysdig is a command-line monitoring tool that is popular among users for its lightweight features.

Sysdig is like a magnifying glass, allowing users to see the behavior of hosts and containers more clearly.

It is equivalent to a collection of various Linux monitoring tools, such as strace, htop, lsof , etc. It integrates these tools’ functions and query results into the same interface for users to operate.

Sysdig provides container images in Docker Hub. Users can run Sysdig as a container. The sample code is as follows:

docker run -it -d --name=sysdig --privileged=true --volume=/var/run/Docker.sock:/host/var/run/Docker.sock --volume=/dev/:/host/dev --volume=/proc/:/host/proc:ro --volume=/boot/:/host/boot:ro --volume=/lib/modules:/host/lib/modules:ro --volume=/usr/:/host/usr:ro docker.io/sysdig/sysdig

In the above example, the Sysdig container collects system information by mounting the host directory and grants it sufficient system permissions.

Note that absolute paths must be used in this command. Otherwise, an error will occur during execution.

After the container is started, it will directly enter the container terminal. If the container is exited through the Ctrl+P+Q key combination or the container is running in the background, the Sysdig container can be entered through the exec command. The sample code is as follows:

docker exec -it sysdig bash

In the Sysdig container, start Sysdig monitoring with the following command:

csysdig

After successful execution, the Sysdig function interface will be displayed, as shown in the figure.

Image credit: Author

In the function interface, there is not only the usage information of each resource but also various options below. Users can monitor different types of resources from different angles according to different requirements.

Press the F2 key or click the Views option to enter the monitoring options list, as shown in the figure.

1*NPBdG50fMFu9ZENfO 6E5A
Image credit: Author

In this interface, each monitoring item of Sysdig is listed on the left, and the description of the monitoring item is on the right.

The cursor in the interface can be moved through the keyboard direction keys to switch monitoring items.

Next, move the cursor to the Containers item, press the Enter key, or double-click the option to enter the container monitoring interface, as shown in the figure.

1*EjF6AgcGljvPC2p tqGhTA
Image credit: Author

If the user feels that the content in the figure is too complicated or difficult to understand, he can press the F7 key to enter the data description interface.

There are explanations of various data, which can help users to master the use of Sysdig faster, as shown in the figure.

1*RGajwFt6fgW5LZUSV eEUA
Image credit: Author

If you want to see the process running in a container, such as weavescope, move the cursor to the target container, then press Enter or double-click.

1*g1HTYNdPm UyrBtrhkTkAw
Image credit: Author

You can also continue to double-click to view threads in the process.

1*2MJlJE76LMoRdMvHdXdl g
Image credit: Author

To return to the previous level, press the backspace key.

sysdig has a strong interactive function. If many items are displayed on the interface, you can click the Search menu at the bottom and enter keywords to search.

As shown in the figure below, the keyword is service.

Image credit: Author

If you feel that the interface refreshes too fast and you cannot see the information you are concerned about, you can click the Pause menu at the bottom.

The features of sysdigare as follows:

  • Full monitoring information, including Linux operating systems and containers.
  • The interface is highly interactive.

However, sysdig shows real-time data and does not see changes and trends. Moreover, it is a command-line operation mode, which requires ssh to be executed on the Host, which will bring some inconvenience.

Weave Scope provides users with a more intuitive monitoring perspective, which presents the entire monitoring as a graphical interface.

First, download the binary installation package of Weave Scope to the specified path. The sample code is as follows:

curl -L git.io/scope -o /usr/local/bin/scope

The essence of the Weave Scope installation package is a script, so it needs to be given execution permission.

chmod a+x /usr/local/bin/scope

Then, execute the script with the following command:

[root@VM-0-11-centos ~]# ./scope launch
Unable to find image 'weaveworks/scope:1.13.2' locally
1.13.2: Pulling from weaveworks/scope
ba3557a56b15: Pull complete
3ac4c0e9800c: Pull complete
d052e74a4dae: Pull complete
aacb9bf49f73: Pull complete
06841e6f61a9: Pull complete
ee99b95c7732: Pull complete
dd0e726a9a15: Pull complete
05cb5f9d0d32: Pull complete
e956cf3e716a: Pull complete
Digest: sha256:8591bb11d72f784f784ac8414660759d40b7c0d8819011660c1cc94271480a83
Status: Downloaded newer image for weaveworks/scope:1.13.2
2fe6961338a338fee7261415f05e7848422df9674f208673d02ea45be6fba885
Scope probe started
Weave Scope is listening at the following URL(s):
* http://172.16.0.11:4040/
* http://172.18.0.1:4040/

The script will automatically download the required docker scope image. If it has already been downloaded, it will be skipped here, and the container will be automatically generated.

If you are testing in a local environment, you can access the monitoring page directly by visiting the above URL in your browser.

If you are deploying on a server, you need to use the server’s IP address and port number to access it.

Note: port 4040 needs to be opened.

Check the container status before entering the interface. The sample code is as follows:

As can be seen from the above example, a new container named weavescope is added to the host, which means that Weave Scope runs in the host as a container.

Next, we follow the prompts to enter the Weave Scope interface, as shown in the figure.

1*6hhxsGdj03jWcBDl zw3pQ
Image credit: Author

In the figure, all containers in the host are presented in the form of graphics, which is more convenient for users to manage.

In the Weave Scope interface, the containers on the host are divided into multiple categories, and the containers of the Weave Scope itself is not displayed by default.

To view all containers, you need to operate in the options in the lower-left corner of the interface and select the ALL button to display.

1*W9RHZ9Jr0AgRVeQa sfn6g
Image credit: Author

To view the container’s resource usage, you need to operate in the options at the top of the interface and click the CPU option above to display the CPU usage of the container in the interface.

After clicking on the CPU option, the CPU usage will be displayed on the container icon as a level height.

At this point, move the mouse pointer over the container icon to display the specific data. To view detailed information about a container, click the container icon, as shown in the figure.

1*5lAFdFl7cuZ0Ua2IOtYwoQ
Image credit: Author

The container details include the following:

  • Status: Real-time status graph of CPU and memory.
  • Info: Information about images, image labels, commands, etc.
  • Processes: Information about the processes running in real-time in this container.
  • Docker labels: Information such as the start command of the maintainer or the container.
  • Image: Image information for this container.

In the container details interface, there is a row of options to perform shortcut operations on the container, as shown in the figure.

1*lmWdcr4j3K La43oLUt2NQ
Image credit: Author

Weave Scope provides users with a broad monitoring perspective. In addition to monitoring containers, it can also monitor hosts.

Click the Hosts option at the top of the interface to view the hosts, as shown in the figure.

1*qp mtr0en ww1oCwaUQWzQ
Image credit: Author

Compared with the container, the load information is added to the Staus item of the host.

The detailed information also includes the container information in the host. Click the container name to view the detailed information about the container.

There is only one option in the host information for users to operate on it, click to enter the host terminal, as shown in the figure.

1*xVXeNP1Tw 55rjHzQcQrEQ
Image credit: Author

This blog shares the monitoring commands that come with Docker and the installation and use of some third-party monitoring software.

Among them, Sysdig is an excellent command-line monitoring tool.

Weave Scope is not only easy to operate but also provides users with a more intuitive graphical interface.

I hope you can master the monitoring method of the Docker container through the study of this article to ensure the normal operation of the business in the container.



News Credit

%d bloggers like this: