Home Operation and Maintenance Linux Operation and Maintenance How to use Docker for container monitoring and log analysis on Linux?

How to use Docker for container monitoring and log analysis on Linux?

Jul 29, 2023 pm 10:49 PM
linux docker Monitoring and log analysis

How to use Docker for container monitoring and log analysis on Linux?

Introduction:
Docker is a popular containerization technology that makes it easier for developers to build, distribute and run applications. However, as the number of applications increases, container monitoring and log analysis become increasingly important. This article will introduce how to use Docker for container monitoring and log analysis on Linux systems, and provide corresponding code examples.

1. Container Monitoring

  1. Use cAdvisor for container monitoring
    cAdvisor is Google’s open source container monitoring tool, which can provide monitoring data of the container’s CPU, memory, network and disk. . Here are the steps to use cAdvisor to monitor containers:

Step 1: Install and start cAdvisor
cAdvisor can be installed with the following command:

docker run --detach=true --name=cadvisor --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --publish=8080:8080 gcr.io/cadvisor/cadvisor:latest
Copy after login

After starting, you can access http: //localhost:8080 to view monitoring data.

Step 2: Monitor the specified container
You can monitor the specified container through the following command:

docker run --detach=true --name=cadvisor --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --publish=8080:8080 gcr.io/cadvisor/cadvisor:latest -c docker_container_name
Copy after login

where docker_container_name is the name of the container to be monitored.

  1. Container monitoring using Prometheus and Grafana
    Prometheus is a time series-based monitoring system that can be used for container monitoring. Grafana is an open source data visualization tool that can display and analyze data collected by Prometheus. The following are the steps for container monitoring using Prometheus and Grafana:

Step 1: Install and configure Prometheus
Prometheus can be installed with the following command:

docker run -d --name=prometheus -p 9090:9090 -v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
Copy after login

Configuration file prometheus.yml The sample content is as follows:

global:
  scrape_interval: 15s
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'cadvisor'
    static_configs:
      - targets: ['cadvisor:8080']
Copy after login

After running, you can view the monitoring data by accessing http://localhost:9090.

Step 2: Install and configure Grafana
You can install Grafana through the following command:

docker run -d --name=grafana -p 3000:3000 grafana/grafana
Copy after login

After installation, visit http://localhost:3000 to configure Grafana and add the Prometheus data source . Dashboards can then be created to display and analyze the collected data.

2. Log analysis

  1. Use ELK for container log analysis
    ELK is a commonly used log analysis solution, consisting of Elasticsearch, Logstash and Kibana. The following are the steps to use ELK for container log analysis:

Step 1: Install and configure Elasticsearch
Elasticsearch can be installed through the following command:

docker run -d --name=elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.15.1
Copy after login

After installation, you can visit by http://localhost:9200 to verify that Elasticsearch is running properly.

Step 2: Install and configure Kibana
Kibana can be installed through the following command:

docker run -d --name=kibana -p 5601:5601 -e "ELASTICSEARCH_HOSTS=http://localhost:9200" docker.elastic.co/kibana/kibana:7.15.1
Copy after login

After installation, you can configure Kibana by visiting http://localhost:5601 and use Elasticsearch as a data source.

Step 3: Install and configure Logstash
Logstash can be installed through the following command:

docker run -d --name=logstash -p 5000:5000 -v /path/to/logstash.conf:/usr/share/logstash/pipeline/logstash.conf docker.elastic.co/logstash/logstash:7.15.1
Copy after login

The sample content of the configuration file logstash.conf is as follows:

input {
  beats {
    port => 5000
  }
}

output {
  elasticsearch {
    hosts => ["http://localhost:9200"]
  }
}
Copy after login

After installation, Logstash will listen on port 5000 and send log data to Elasticsearch.

Step 4: Configure container log collection
You can configure the collection of container logs through the following command:

docker run -it --name=your_container_name --log-driver=gelf --log-opt gelf-address=udp://localhost:5000 your_image_name
Copy after login

where your_container_name is the name of the container to collect logs, your_image_name is the image used by the container name.

Conclusion:
By using Docker for container monitoring and log analysis, we can better understand the running status and log information of the container, thereby improving the stability and reliability of the application. This article introduces two commonly used tools and methods, and provides corresponding code examples. I hope it will be helpful to readers when using Docker for container monitoring and log analysis on Linux systems.

The above is the detailed content of How to use Docker for container monitoring and log analysis on Linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

How to run sublime python How to run sublime python Apr 16, 2025 am 08:54 AM

How to run Python scripts in Sublime Text: Install Python interpreter configuration Interpreter path in Sublime Text Press Ctrl B (Windows/Linux) or Cmd B (macOS) to run the script If an interactive console is required, press Ctrl \ (Windows/Linux) or Cmd \ (macOS)

How to use sublime shortcut keys How to use sublime shortcut keys Apr 16, 2025 am 08:57 AM

Sublime Text provides shortcuts to improve development efficiency, including commonly used (save, copy, cut, etc.), editing (indentation, formatting, etc.), navigation (project panel, file browsing, etc.), and finding and replacing shortcuts. Proficiency in using these shortcut keys can significantly improve Sublime's efficiency.

See all articles