(Elastic monitoring - 3/5) Collect metrics with Elastic Metricbeat for monitoring Kubernetes
Greg Jeanmart
Metricbeat is a lightweight shipper installed on a server to periodically collect metrics from the host and services running. This represents the first pillar of observability to monitor our stack.
Metricbeat captures by default system metrics but also includes a large list of modules to capture specific metrics about services such as proxy (NGINX), message bus (RabbitMQ, Kafka), Databases (MongoDB, MySQL, Redis) and many others (find the full list here)
Prerequisite - kube-state-metrics
First, we need to install kube-state-metrics which is a service listening the Kubernetes API to exposes a set of useful metrics about the state of each Object.
To install kube-state-metrics, simply run the following command:
clusterrolebinding.rbac.authorization.k8s.io/kube-state-metrics created clusterrole.rbac.authorization.k8s.io/kube-state-metrics created deployment.apps/kube-state-metrics created serviceaccount/kube-state-metrics created service/kube-state-metrics created
Configuration
In order to install Metricbeat on our Kubernetes environment, we need to install a DaemonSet (agent installed on every nodes) and configure the settings.
First of all, we are writing the metricbeat configuration into the file metricbeat.yml which will be located in /etc/metricbeat.yml of the DaemonSet pod container.
This file contains our metricbeat settings. We configure the ElasticSearch connection (endpoint, username, password) as output, the Kibana connection (to import pre-existing dashboards), the modules we want to enable with the period of pulling and the indice lifecycle file (rollup, retention), etc.
# Configure modules metricbeat.modules: -module:system period:${PERIOD} metricsets: ["cpu", "load", "memory", "network", "process", "process_summary", "core", "diskio", "socket"] processes: ['.*'] process.include_top_n: by_cpu:5# include top 5 processes by CPU by_memory:5# include top 5 processes by memory
ElasticSearch indice lifecycle represents a set of rules you want to apply to your indices based on the size or the age of the indice. So for example, it’s possible to rollover the indice (create a new file) every day or every time it exceed 1GB, we can also configure different phases based on rule (hot for active read/write indice, cold for read-only and delete to delete the indice). Monitoring can generate a large amount of data, perhaps more than 10GB a day, so to prevent spending to much money on cloud storage, we can use the indice lifecycle to configure data retention easily.
In the file below, we configure to rollover the indice every day or every time it exceeds 5GB and delete all indice files older than 30 days. We only keep 30 days of monitoring data
The next part is the DaemonSet describing a Metricbeat agent deployed on each node of the k8s cluster. We can especially noticed the environment variables and the volumes to access the ConfigMap
configmap/metricbeat-config configured configmap/metricbeat-indice-lifecycle configured daemonset.extensions/metricbeat created clusterrolebinding.rbac.authorization.k8s.io/metricbeat created clusterrole.rbac.authorization.k8s.io/metricbeat created serviceaccount/metricbeat created
Wait until the metricbeat pod is Running and you should be able to observe metrics in Kibana.
1 2 3 4 5 6 7
$ kubectl get all -n monitoring -l app=metricbeat
NAME READY STATUS RESTARTS AGE pod/metricbeat-hczk7 1/1 Running 0 60m
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE daemonset.apps/metricbeat 1 1 1 1 1 <none> 60m
Firstly, from the left menu, click on “Infrastructure” and you should see a schema of your infrastructure with:
Host: Nodes of Cluster
Kubernetes: Visualise each pods
Docker: Visualise each container
On each view, we can observe different metrics (CPU, Memory, Network, etc.) or group by attribute value (namespace, host, etc.).
In the settings, we set the property setup.dashboards.enabled to true to import pre-existing dashboards. From the left menu, go to “Dashboards” and you should see a list of about 50 Metricbeat dashboards.
We enabled the module kubernetes, so the dashboard [Metricbeat Kubernetes] Overview ECS should be populated:
We also enabled the module mongodb, have a look now on the dashboard [Metricbeat MongoDB] Overview ECS