- Bài 1. Cơ Bản Docker: Hiểu Container và Vai Trò trong DevOps
- Bài 2. Docker Image: cách xây Dựng và tối Ưu
- Bài 3. Docker Networking: Kết Nối Container Hiệu Quả
- Bài 4. Docker Volume: Quản Lý Dữ Liệu Hiệu Quả cho Container
- Bài 5. Docker Compose: Điều Phối Multi-Container Hiệu Quả
- Bài 6. Docker Swarm: Orchestration Cơ Bản để Scale Container
- Bài 7. Docker Security: Bảo Mật Container Hiệu Quả
- Bài 8. Docker CI/CD: Tích Hợp Container vào Pipeline DevOps
- Bài 9. Docker Monitoring: Theo Dõi và Logging Container Hiệu Quả
- Bài 10. Docker resources: Tối Ưu và Debug Container Hiệu Quả
- Bài 11. Docker và Microservices: Thiết Kế Ứng Dụng Phân Tán
- Bài 12. Docker Registry: Tự Host và Quản Lý Image Hiệu Quả
- Bài 13. Docker với Cloud: Tích Hợp Container trên AWS ECS và GCP Cloud Run
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ị
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
Build:
docker build -t myapp .
Chạy và Giám Sát
- Chạy Flask:
docker run -d -p 5000:5000 --name flask-app myapp
- 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
- 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
- Test tải:
while true; do curl localhost:5000; sleep 0.1; done
- Xem metric:
- Truy cập
localhost:9090
, query:container_cpu_usage_seconds_total{name="flask-app"}
.
- Truy cập
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.
- Thêm Grafana:
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 container và theo 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.
Website không chứa bất kỳ quảng cáo nào, mọi đóng góp để duy trì phát triển cho website (donation) xin vui lòng gửi về STK 90.2142.8888 - Ngân hàng Vietcombank Thăng Long - TRAN VAN BINH
=============================
Nếu bạn không muốn bị AI thay thế và tiết kiệm 3-5 NĂM trên con đường trở thành DBA chuyên nghiệp hay làm chủ Database thì hãy đăng ký ngay KHOÁ HỌC ORACLE DATABASE A-Z ENTERPRISE, được Coaching trực tiếp từ tôi với toàn bộ bí kíp thực chiến, thủ tục, quy trình của gần 20 năm kinh nghiệm (mà bạn sẽ KHÔNG THỂ tìm kiếm trên Internet/Google) từ đó giúp bạn dễ dàng quản trị mọi hệ thống Core tại Việt Nam và trên thế giới, đỗ OCP.
- CÁCH ĐĂNG KÝ: Gõ (.) hoặc để lại số điện thoại hoặc inbox https://m.me/tranvanbinh.vn hoặc Hotline/Zalo 090.29.12.888
- Chi tiết tham khảo:
https://bit.ly/oaz_w
=============================
2 khóa học online qua video giúp bạn nhanh chóng có những kiến thức nền tảng về Linux, Oracle, học mọi nơi, chỉ cần có Internet/4G:
- Oracle cơ bản: https://bit.ly/admin_1200
- Linux: https://bit.ly/linux_1200
=============================
KẾT NỐI VỚI CHUYÊN GIA TRẦN VĂN BÌNH:
📧 Mail: binhoracle@gmail.com
☎️ Mobile/Zalo: 0902912888
👨 Facebook: https://www.facebook.com/BinhOracleMaster
👨 Inbox Messenger: https://m.me/101036604657441 (profile)
👨 Fanpage: https://www.facebook.com/tranvanbinh.vn
👨 Inbox Fanpage: https://m.me/tranvanbinh.vn
👨👩 Group FB: https://www.facebook.com/groups/DBAVietNam
👨 Website: https://www.tranvanbinh.vn
👨 Blogger: https://tranvanbinhmaster.blogspot.com
🎬 Youtube: https://www.youtube.com/@binhguru
👨 Tiktok: https://www.tiktok.com/@binhguru
👨 Linkin: https://www.linkedin.com/in/binhoracle
👨 Twitter: https://twitter.com/binhguru
👨 Podcast: https://www.podbean.com/pu/pbblog-eskre-5f82d6
👨 Địa chỉ: Tòa nhà Sun Square - 21 Lê Đức Thọ - Phường Mỹ Đình 1 - Quận Nam Từ Liêm - TP.Hà Nội
=============================
cở sở dữ liệu, cơ sở dữ liệu quốc gia, database, AI, trí tuệ nhân tạo, artificial intelligence, machine learning, deep learning, LLM, ChatGPT, DeepSeek, Grok, oracle tutorial, học oracle database, Tự học Oracle, Tài liệu Oracle 12c tiếng Việt, Hướng dẫn sử dụng Oracle Database, Oracle SQL cơ bản, Oracle SQL là gì, Khóa học Oracle Hà Nội, Học chứng chỉ Oracle ở đầu, Khóa học Oracle online,sql tutorial, khóa học pl/sql tutorial, học dba, học dba ở việt nam, khóa học dba, khóa học dba sql, tài liệu học dba oracle, Khóa học Oracle online, học oracle sql, học oracle ở đâu tphcm, học oracle bắt đầu từ đâu, học oracle ở hà nội, oracle database tutorial, oracle database 12c, oracle database là gì, oracle database 11g, oracle download, oracle database 19c, oracle dba tutorial, oracle tunning, sql tunning , oracle 12c, oracle multitenant, Container Databases (CDB), Pluggable Databases (PDB), oracle cloud, oracle security, oracle fga, audit_trail,oracle RAC, ASM, oracle dataguard, oracle goldengate, mview, oracle exadata, oracle oca, oracle ocp, oracle ocm , oracle weblogic, postgresql tutorial, mysql tutorial, mariadb tutorial, ms sql server tutorial, nosql, mongodb tutorial, oci, cloud, middleware tutorial, hoc solaris tutorial, hoc linux tutorial, hoc aix tutorial, unix tutorial, securecrt, xshell, mobaxterm, putty