Container Monitoring Installation Guide166


Installing a container monitoring system is essential for ensuring the smooth operation and security of your containerized applications. This guide provides step-by-step instructions on how to set up a container monitoring system using commonly available tools.

Prerequisites
A Kubernetes cluster
Prometheus (a monitoring and alerting system)
Grafana (a visualization and dashboarding tool)
Fluentd (a logging aggregator)
Elasticsearch (a log storage and search engine)
Kibana (a visualization and dashboarding tool for Elasticsearch)

Prometheus Installation

Install Prometheus using Helm:```bash
helm repo add prometheus-community /helm-charts
helm install prometheus prometheus-community/prometheus
```

Create a Prometheus service account and cluster role:```bash
kubectl create serviceaccount prometheus-user
kubectl create clusterrolebinding prometheus-user --clusterrole=prometheus
```

Add the Prometheus metrics endpoint to the cluster:```
apiVersion: v1
kind: ServiceAccount
metadata:
name: prometheus-user
namespace: default
---
apiVersion: /v1
kind: ClusterRoleBinding
metadata:
labels:
k8s-app: prometheus
role: prometheus-default
name: prometheus-default
roleRef:
apiGroup:
kind: ClusterRole
name: system:metrics-server
subjects:
- kind: ServiceAccount
name: prometheus-user
namespace: default
```

Grafana Installation

Install Grafana using Helm:```bash
helm repo add grafana /helm-charts
helm install grafana grafana/grafana
```

Create a Grafana service account and cluster role:```bash
kubectl create serviceaccount grafana-user
kubectl create clusterrolebinding grafana-user --clusterrole=view
```

Add the Grafana dashboard to the cluster:```yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: grafana-user
namespace: default
---
apiVersion: /v1
kind: ClusterRoleBinding
metadata:
labels:
k8s-app: grafana
role: grafana-default
name: grafana-default
roleRef:
apiGroup:
kind: ClusterRole
name: view
subjects:
- kind: ServiceAccount
name: grafana-user
namespace: default
```

Fluentd Installation

Install Fluentd using Helm:```bash
helm repo add fluent /helm-charts
helm install fluentd fluent/fluentd
```

Create a Fluentd service account and cluster role:```bash
kubectl create serviceaccount fluentd-user
kubectl create clusterrolebinding fluentd-user --clusterrole=admin
```

Add the Fluentd logging endpoint to the cluster:```bash
apiVersion: v1
kind: ServiceAccount
metadata:
name: fluentd-user
namespace: default
---
apiVersion: /v1
kind: ClusterRoleBinding
metadata:
labels:
k8s-app: fluentd
role: fluentd-default
name: fluentd-default
roleRef:
apiGroup:
kind: ClusterRole
name: admin
subjects:
- kind: ServiceAccount
name: fluentd-user
namespace: default
```

Elasticsearch Installation

Install Elasticsearch using Helm:```bash
helm repo add elastic
helm install elasticsearch elastic/elasticsearch
```

Create an Elasticsearch service account and cluster role:```bash
kubectl create serviceaccount elasticsearch-user
kubectl create clusterrolebinding elasticsearch-user --clusterrole=elastic-user
```

Add the Elasticsearch endpoint to the cluster:```yaml
kind: ServiceAccount
apiVersion: v1
metadata:
name: elasticsearch-user
namespace: default
---
kind: ClusterRoleBinding
apiVersion: /v1
metadata:
name: elasticsearch-user
labels:
k8s-app: elasticsearch
subjects:
- kind: ServiceAccount
name: elasticsearch-user
namespace: default
roleRef:
kind: ClusterRole
name: elastic-user
apiGroup:
```

Kibana Installation

Install Kibana using Helm:```bash
helm repo add kibana
helm install kibana kibana/kibana
```

Create a Kibana service account and cluster role:```bash
kubectl create serviceaccount kibana-user
kubectl create clusterrolebinding kibana-user --clusterrole=kibana-user
```

Add the Kibana dashboard to the cluster:```yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: kibana-user
namespace: default
---
apiVersion: /v1
kind: ClusterRoleBinding
metadata:
labels:
k8s-app: kibana
role: kibana-default
name: kibana-default
roleRef:
apiGroup:
kind: ClusterRole
name: kibana-user
subjects:
- kind: ServiceAccount
name: kibana-user
namespace: default
```

Monitoring Configuration

Configure Prometheus to scrape metrics from Kubernetes:```yaml
scrape_configs:
- job_name: 'kubernetes-nodes'
kubernetes_sd_configs:
- role: node
relabel_configs:
- source_labels: [__meta_kubernetes_node_name]
target_label: node_name
- action: replace
source_labels: [__meta_kubernetes_node_label_kubernetes_io_hostname]
target_label: hostname
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_name]
target_label: pod_name
- source_labels: [__meta_kubernetes_pod_container_name]
target_label: container_name
- source_labels: [__meta_kubernetes_namespace]
target_label: namespace
```

Configure Grafana to display metrics and dashboards:```json
{
"annotations": {
"list": []
},
"dashboard": {
"panels": [
{
"id": 1,
"type": "graph",
"title": "CPU Usage",
"datasource": "Prometheus",
"targets": [
{
"expr": "avg(rate(container_cpu_usage_seconds_total{container!="POD"}[5m]))",
"format": "time_series",
"refId": "A"
}
],
"legend": {
"alignAsTable": false,
"avg": false,
"current": false,
"max": false,
"min": false,
"show": true,
"total": false,
"values": false
},
"options": {
"legend": {
"sort": null
}
}
}
],
"schemaVersion": 16,
"style": "dark",
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "now-5m",
"to": "now"
},
"title": "Kubernetes Monitoring",
"version": 1
}
}
```

Configure Fluentd to send logs to Elasticsearch:```yaml

@type stdout

json .



@type parser
key_name .pod_name
reserve_data true

2024-10-31


Previous:How to Install a Security Camera System in an Aging Building: A Comprehensive Guide for DIYers

Next:Xiaomi Security Camera: Step-by-Step Guide to Capture Videos