Getting monitoring essentials for Kubernetes

Since monitoring is an important part of operating a service, the existing monitoring system in our infrastructure might already provide solutions for collecting metrics from common sources like well-known open source software and the operating system. As for applications run on Kubernetes, let's have a look at what Kubernetes and its ecosystem offer.

To collect metrics of containers managed by Kubernetes, we don't have to install any special controller on the Kubernetes master node, nor any metrics collector inside our containers. This is basically done by kubelet, which gathers various telemetries from a node, and exposes them in the following API endpoints (as of Kubernetes 1.13):

  • /metrics/cadvisor: This API endpoint is used for cAdvisor container metrics that are in Prometheus format
  • /spec/: This API endpoint exports machine specifications
  • /stats/: This API endpoint also exports cAdvisor container metrics but in JSON format
  • /stats/summary: This endpoint contains various data aggregated by kubelet. It's also known as the Summary API
The metrics under the bare path /metrics/ relate to kubelet's internal statistics.

The Prometheus format (https://prometheus.io/docs/instrumenting/exposition_formats/) is the predecessor of the OpenMetrics format, so it is also known as OpenMetrics v0.0.4 after OpenMetrics was published. If our monitoring system supports this kind of format, we can configure it to pull metrics from kubelet's Prometheus endpoint (/metrics/cadvisor).

To access those endpoints, kubelet has two TCP ports, 10250 and 10255. Port 10250 is the safer one and the one that it is recommended to use in production as it's an HTTPS endpoint and protected by Kubernetes' authentication and authorization system. 10255 is in plain HTTP, which should be used restrictively. 

cAdvisor (https://github.com/google/cadvisor) is a widely used container-level metrics collector. Put simply, cAdvisor aggregates the resource usage and performance statistics of every container running on a machine. Its code is currently sold inside kubelet, so we don't need to deploy it separately. However, since it focuses on certain container runtimes and Linux containers only, which may not suit future Kubernetes releases for different container runtimes, there won't be an integrated cAdvisor in future releases of Kubernetes. In addition to this, not all cAdvisor metrics are currently published by kubelet. Therefore, if we need that data, we'll need to deploy cAdvisor by ourselves. Notice that the deployment of cAdvisor is one per host instead of one per container, which is more reasonable for containerized applications, and we can use DaemonSet to deploy it.

Another important component in the monitoring pipeline is the metrics server (https://github.com/kubernetes-incubator/metrics-server). This aggregates monitoring statistics from the summary API by kubelet on each node and acts as an abstraction layer between Kubernetes' other components and the real metrics sources. To be more specific, the metrics server implements the resource metrics API under the aggregation layer, so other intra-cluster components can get the data from a unified API path (/api/metrics.k8s.io). In this instance, kubectl top and kube-dashboard get data from the resource metrics API.

The following diagram illustrates how the metrics server interacts with other components in a cluster:

If you're using an older version of Kubernetes, the role of the metrics server will be played by Heapster (https://github.com/kubernetes/heapster).

Most installations of Kubernetes deploy the metrics server by default. If we need to do this manually, we can download the manifest of the metrics server and apply them:

$ git clone https://github.com/kubernetes-incubator/metrics-server.git
$ kubectl apply -f metrics-server/deploy/1.8+/

While kubelet metrics are focused on system metrics, we also want to see the logical states of objects displayed on our monitoring dashboard. kube-state-metrics (https://github.com/kubernetes/kube-state-metrics) is the piece that completes our monitoring stack. It watches Kubernetes masters and transforms the object statuses we see from kubectl get or kubectl describe into metrics in the Prometheus format. We are therefore able to scrape the states into metrics storage and then be alerted on events such as unexplainable restart counts. Download the templates to install as follows:

$ git clone https://github.com/kubernetes/kube-state-metrics.git
$ kubectl apply -f kube-state-metrics/kubernetes

Afterward, we can view the state metrics from the kube-state-metrics service inside our cluster:

http://kube-state-metrics.kube-system:8080/metrics
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset