Home  >  Article  >  Backend Development  >  PHP microservice containerized monitoring and log management practice

PHP microservice containerized monitoring and log management practice

WBOY
WBOYOriginal
2024-05-08 12:06:01608browse

PHP Microservice Containerization Monitoring and Log Management Monitoring: Monitor resource usage, request count and latency using Prometheus and Grafana. Log management: Collect, parse, and visualize logs using the ELK Stack (ElasticSearch, Logstash, Kibana). Deploy the Filebeat agent to send logs to ElasticSearch.

PHP 微服务容器化监控与日志管理实战

PHP Microservice containerized monitoring and log management practice

In modern distributed architecture, the containerization of microservices has became a popular practice. This article will introduce how to use Prometheus and Grafana to monitor PHP microservices, and use ELK Stack for log management.

Monitoring

1. Install Prometheus

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install prometheus prometheus-community/kube-prometheus-stack

2. Install Grafana

helm repo add grafana https://grafana.github.io/helm-charts
helm repo update
helm install grafana grafana/grafana

3. Configure Grafana dashboard
Create the following Grafana dashboard, using Prometheus as the data source:

- Graph: Pod 资源使用情况,监控 CPU 和内存使用
- Gauge: 容器请求数,监控每秒处理的请求数
- Scatter Plot: 请求延迟,绘制请求延迟与时间的关系

Log management

1. Install ELK Stack

docker-compose up

2. Configure ELK Stack
Create an index pattern in Kibana to parse PHP logs. Fields can include:

- timestamp
- level
- message
- ...

3. Deploy the log agent
For example, you can use Filebeat to deploy to each microservice Pod and send the logs to ElasticSearch.

filebeat:
  inputs:
    - type: log
      paths:
        - /var/log/*.log
  output.logstash:
    hosts: ["logstash:5044"]

Practical case

The following is a PHP microservice Dockerfile example for monitoring and logging:

FROM php:8.0-fpm

# Copy application code
COPY . /var/www/html

# Install dependencies
RUN composer install

# Prometheus Exporter
RUN wget https://github.com/prometheus/client_php/releases/download/2.4.2/prometheusclient-php-2.4.2.phar -O /usr/local/bin/promexp --quiet

# Logstash Filebeat
RUN wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.1.0-linux-x86_64.tar.gz -O /tmp/filebeat.tar.gz --quiet
RUN tar -zxf /tmp/filebeat.tar.gz -C /usr/local/bin/

# Start application
CMD ["php", "-S", "0.0.0.0:80"]

Conclusion

By implementing the above monitoring and log management measures, you can gain in-depth understanding of the health of your PHP microservices and detect and resolve any issues in a timely manner, thereby improving the stability and performance of your application.

The above is the detailed content of PHP microservice containerized monitoring and log management practice. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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