## Introduction
In this article, we will discuss how to set up application performance monitoring (APM) for your applications running on Kubernetes using Prometheus and Grafana. APM tools help you monitor the performance of your applications in real-time, allowing you to identify bottlenecks and optimize resource usage.
### Steps to set up "k8s apm 应用性能监控"
| Step | Description |
| --- | --- |
| 1 | Deploy Prometheus on Kubernetes |
| 2 | Instrument your application with Prometheus client libraries |
| 3 | Configure Prometheus to scrape metrics from your application |
| 4 | Deploy Grafana on Kubernetes |
| 5 | Configure Grafana to visualize Prometheus metrics |
### Step 1: Deploy Prometheus on Kubernetes
```yaml
apiVersion: v1
kind: Service
metadata:
name: prometheus
spec:
selector:
app: prometheus
ports:
- protocol: TCP
port: 9090
targetPort: 9090
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus
spec:
selector:
matchLabels:
app: prometheus
replicas: 1
template:
metadata:
labels:
app: prometheus
spec:
containers:
- name: prometheus
image: prom/prometheus
ports:
- containerPort: 9090
args:
- --config.file=/etc/prometheus/prometheus.yml
```
### Step 2: Instrument your application with Prometheus client libraries
Add Prometheus client libraries to your application's code to expose metrics. For example, in a Node.js application:
```bash
npm install prom-client
```
```javascript
const promClient = require('prom-client');
const register = new promClient.Registry();
// Define your custom metrics
const myCustomMetric = new promClient.Gauge({
name: 'my_custom_metric',
help: 'This is my custom metric',
labelNames: ['status'],
registers: [register]
});
// Update the metric value
myCustomMetric.set({ status: 'success' }, 1);
// Expose metrics endpoint
app.get('/metrics', (req, res) => {
res.set('Content-Type', register.contentType);
res.end(register.metrics());
});
```
### Step 3: Configure Prometheus to scrape metrics from your application
Create a `prometheus.yml` file to configure Prometheus to scrape metrics from your application:
```yaml
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'my_application'
static_configs:
- targets: ['your_application_service_name:port']
```
### Step 4: Deploy Grafana on Kubernetes
```yaml
apiVersion: v1
kind: Service
metadata:
name: grafana
spec:
selector:
app: grafana
ports:
- protocol: TCP
port: 3000
targetPort: 3000
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: grafana
spec:
selector:
matchLabels:
app: grafana
replicas: 1
template:
metadata:
labels:
app: grafana
spec:
containers:
- name: grafana
image: grafana/grafana
ports:
- containerPort: 3000
```
### Step 5: Configure Grafana to visualize Prometheus metrics
1. Access the Grafana web UI at `http://grafana-service-ip:3000`
2. Log in using the default credentials (admin/admin)
3. Add Prometheus data source in Grafana configuration with the URL `http://prometheus-service-ip:9090`
4. Create dashboards in Grafana to visualize metrics from Prometheus
By following these steps, you can set up application performance monitoring for your Kubernetes applications using Prometheus and Grafana. This will help you gain insights into the performance of your applications and identify areas for optimization. Happy monitoring!