Bài 9. Docker Monitoring: Theo Dõi và Logging Container Hiệu Quả

Chạy container là một chuyện, nhưng biết chúng hoạt động ra sao lại là chuyện khác. Trong DevOps, nếu không theo dõi hiệu năng hay thu thập log, bạn sẽ mò mẫm trong bóng tối khi pipeline lỗi hay ứng dụng production crash. Trong bài thứ chín của series, tôi sẽ dẫn bạn qua Docker Monitoring trong DevOps, từ logging container, dùng Prometheus Docker để giám sát, đến thực hành cụ thể – dựa trên những gì tôi đã áp dụng thực tế, không chỉ là lý thuyết đâu nhé!

Tại Sao Monitoring và Logging Quan Trọng?

Vai Trò trong DevOps

  • Logging: Ghi lại lỗi, sự kiện trong container (VD: API crash, DB timeout).
  • Monitoring: Đo CPU, RAM, network để phát hiện vấn đề (VD: container ăn 100% CPU).

Ví dụ thực tế: Một team DevOps từng mất 3 giờ tìm lỗi API chậm. Dùng log và metric từ Prometheus, họ phát hiện bottleneck trong 15 phút.

Mục Tiêu

  • Debug nhanh khi pipeline/test fail.
  • Đảm bảo production ổn định.
  • Scale kịp thời khi tải tăng.

DevOps: Không có monitoring, bạn như lái xe không đèn pha trong đêm.

Logging Container trong Docker

Logging Cơ Bản

Docker hỗ trợ log qua driver (mặc định: json-file).

  • Xem log:
    docker logs <container-id>

Ví dụ: Chạy Nginx, xem log:

docker run -d -p 8080:80 nginx
docker logs <container-id>
  • Thấy access log (VD: GET / 200).

Logging Driver

  • json-file: Lưu log dạng JSON trên host.
  • syslog: Gửi log đến syslog server.
  • fluentd: Chuyển log đến Fluentd/ELK.

Cấu hình:

docker run -d --log-driver=syslog --log-opt syslog-address=udp://localhost:514 nginx

DevOps: Dùng Fluentd để gửi log từ pipeline sang ELK, dễ phân tích lỗi.

Monitoring với Prometheus và cAdvisor

Prometheus: Theo Dõi Metric

Prometheus thu thập metric (CPU, RAM, I/O) từ container qua endpoint.

  • Cài Prometheus:
    # prometheus.yml
    scrape_configs:
    - job_name: 'docker'
      static_configs:
        - targets: ['host.docker.internal:9323']
    docker run -d -p 9090:9090 -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

cAdvisor: Metric Container

cAdvisor (Container Advisor) cung cấp metric chi tiết từ Docker daemon.

  • Chạy cAdvisor:
    docker run -d -p 8080:8080 --volume=/:/rootfs:ro --volume=/var/run:/var/run:ro --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro google/cadvisor:latest
  • Prometheus scrape từ localhost:8080/metrics.

DevOps: Kết hợp Prometheus + cAdvisor để theo dõi pipeline và production.

Thực Hành: Giám Sát Ứng Dụng Flask

Chuẩn Bị

  1. Dockerfile:

    FROM python:3.9-slim
    WORKDIR /app
    COPY requirements.txt .
    RUN pip install -r requirements.txt
    COPY app.py .
    CMD ["python", "app.py"]
    • app.py:

      from flask import Flask
      import time
      
      app = Flask(__name__)
      
      @app.route('/')
      def hello():
       time.sleep(0.1)  # Giả lập tải
       return "Hello Monitoring!"
      
      if __name__ == "__main__":
       app.run(host="0.0.0.0", port=5000)
    • requirements.txt:
      flask==2.0.1
  2. Build:

    docker build -t myapp .

Chạy và Giám Sát

  1. Chạy Flask:
    docker run -d -p 5000:5000 --name flask-app myapp
  2. Chạy cAdvisor:
    docker run -d -p 8080:8080 --volume=/:/rootfs:ro --volume=/var/run:/var/run:ro --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro google/cadvisor:latest
  3. Chạy Prometheus:
    • prometheus.yml:
      scrape_configs:
      - job_name: 'cadvisor'
       static_configs:
         - targets: ['localhost:8080']
      docker run -d -p 9090:9090 -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
  4. Test tải:
    while true; do curl localhost:5000; sleep 0.1; done
  5. Xem metric:
    • Truy cập localhost:9090, query: container_cpu_usage_seconds_total{name="flask-app"}.

Debug tip:

  • Log không thấy: Kiểm tra driver (docker inspect <container-id>).
  • Metric trống: Xác nhận cAdvisor chạy, port 8080 mở.

Ứng Dụng trong DevOps

Pipeline CI/CD

  • Log trong CI:
    test:
    image: myapp
    script:
      - pytest
    after_script:
      - docker logs $CI_JOB_ID > test.log
      - cat test.log
  • Kết quả: Lưu log test để debug.

Production Monitoring

  • Dùng Prometheus + Grafana:
    • Thêm Grafana:
      docker run -d -p 3000:3000 grafana/grafana
    • Tạo dashboard CPU/RAM từ Prometheus.

Case study: Một team setup monitoring cho 20 container, phát hiện 1 API ăn 90% RAM nhờ Grafana, fix kịp thời trước khi crash.

Alerting

  • Cấu hình alert trong Prometheus:
    alerting:
    alertmanagers:
      - static_configs:
          - targets: ['alertmanager:9093']
  • Alert khi CPU > 80%.

Kết Luận

Docker Monitoring trong DevOps giúp bạn nắm rõ container qua logging containertheo dõi hiệu năng với Prometheus Docker. Thực hành với Flask cho thấy bạn có thể áp dụng ngay vào pipeline và production. Bài tiếp theo – bài cuối – chúng ta sẽ khám phá Docker Nâng Cao – tối ưu và debug container trong DevOps.

Điều hướng chuỗi bài viết<< Bài 8. Docker CI/CD: Tích Hợp Container vào Pipeline DevOps
>> Bài 10. Docker resources: Tối Ưu và Debug Container Hiệu Quả
Article Thumbnail
Article Thumbnail
Datadog Webinar: Modernize AWS Logs at Scale
Chia sẻ bài viết:
Theo dõi
Thông báo của
0 Góp ý
Được bỏ phiếu nhiều nhất
Mới nhất Cũ nhất
Phản hồi nội tuyến
Xem tất cả bình luận