{{announcement.body}}
{{announcement.title}}

Kubernetes Monitoring Using Prometheus In Less Than 5 Minutes

DZone 's Guide to

Kubernetes Monitoring Using Prometheus In Less Than 5 Minutes

Monitoring is a crucial aspect of any Ops pipeline and for Kubernetes. Let's deploy a Production grade Prometheus base monitoring system in less than 5 minutes.

· Cloud Zone ·
Free Resource

Monitoring is a crucial aspect of any Ops pipeline and for technologies like Kubernetes which is a rage right now, a robust monitoring setup can bolster your confidence to migrate production workloads from VMs to Containers.

Today we will deploy a Production grade Prometheus based monitoring system, in less than 5 minutes.

Pre-Requisites:

  1. Running a Kubernetes cluster with at least 6 cores and 8 GB of available memory. I will be using a 6 node GKE for this tutorial.
  2. Working knowledge of Kubernetes Deployments and Services.

Setup:

  1. Prometheus server with persistent volume. This will be our metric storage (TSDB).
  2. Alertmanager server which will trigger alerts to Slack/Hipchat and/or Pagerduty/Victorops etc.
  3. Kube-state-metrics server to expose container and pod metrics other than those exposed by cadvisor on the nodes.
  4. Grafana server to create dashboards based on Prometheus data.

Note: All the manifests being used are present in this Github Repo. I recommend cloning it before you start.
PS: Leave a star if you like it.

Monitoring Setup Overview

Deploying Alertmanager

Before deploying, please update “<your_slack_hook>” , “<your_victorops_hook>” , ‘<YOUR_API_KEY>’ . If you use a notification channel other than these, please follow this documentation and update the config

kubectl apply -f k8s/monitoring/alertmanager/

This will create the following:

  1. A monitoring namespace.
  2. Config-map to be used by alertmanager to manage channels for alerting.
  3. Alertmanager deployment with 1 replica running.
  4. Service with Google Internal Loadbalancer IP which can be accessed from the VPC (using VPN)
Shell
 




x


 
1
root$ kubectl get pods -l app=alertmanager
2
NAME                            READY     STATUS    RESTARTS   AGE
3
alertmanager-42s7s25467-b2vqb   1/1       Running   0          2m
4
root$ kubectl get svc -l name=alertmanager
5
NAME           TYPE           CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
6
alertmanager   LoadBalancer   10.12.8.110   10.0.0.6     9093:32634/TCP   2m
7
root$ kubectl get configmap
8
NAME                     DATA      AGE
9
alertmanager             1         2m



In your browser, navigate to http://<Alertmanager-Svc-Ext-Ip>:9093 and you should see the alertmanager console.

Alertmanager Status Page

Deploying Prometheus

Before deploying, please create an EBS volume (AWS) or pd-ssd disk (GCP) and name it as prometheus-volume (This is important because the PVC will look for a volume in this name).

kubectl apply -f k8s/monitoring/prometheus/

This will create the following:

  1. Service account, cluster-role and cluster-role-binding needed for Prometheus.
  2. Prometheus config map which details the scrape configs and alertmanager endpoint. It should be noted that we can directly use the alertmanager service name instead of the IP. If you want to scrape metrics from a specific pod or service, then it is mandatory to apply the Prometheus scrape annotations to it. For example:
Shell
 




xxxxxxxxxx
1
12


1
spec:
2
  replicas: 1
3
  template:
4
    metadata:
5
      annotations:
6
        prometheus.io/path: <path_to_scrape>
7
        prometheus.io/port: "80"
8
        prometheus.io/scrape: "true"
9
      labels:
10
        app: myapp
11
  spec:
12
...



  1. Prometheus config map for the alerting rules. Some basic alerts are already configured in it (Such as High CPU and Mem usage for Containers and Nodes etc). Feel free to add more rules according to your use case.
  2. Storage class, persistent volume and persistent volume claim for the prometheus server data directory. This ensures data persistence in case the pod restarts.
  3. Prometheus deployment with 1 replica running.
  4. Service with Google Internal Loadbalancer IP which can be accessed from the VPC (using VPN).
Shell
 




xxxxxxxxxx
1
11


 
1
root$ kubectl get pods -l app=prometheus-server
2
NAME                                     READY     STATUS    RESTARTS   AGE
3
prometheus-deployment-69d6cfb5b7-l7xjj   1/1       Running   0          2m
4
root$ kubectl get svc -l name=prometheus
5
NAME                 TYPE           CLUSTER-IP    EXTERNAL-IP   PORT(S)             AGE
6
prometheus-service   LoadBalancer   10.12.8.124   10.0.0.7    8080:32731/TCP      2m
7
root$ kubectl get configmap
8
NAME                     DATA      AGE
9
alertmanager             1         5m
10
prometheus-rules         1         2m
11
prometheus-server-conf   1         2m



In your browser, navigate to http://<Prometheus-Svc-Ext-Ip>:8080 and you should see the Prometheus console. It should be noticed that under the Status->Targets section all the scraped endpoints are visible and under Alerts section all the configured alerts can be seen.

Prometheus Targets Status
Prometheus Graph section depicting all metrics

Deploying Kube-State-Metrics

kubectl apply -f k8s/monitoring/kube-state-metrics/

This will create the following:

  1. Service account, cluster-role and cluster-role-binding needed for kube-state-metrics.
  2. Kube-state-metrics deployment with 1 replica running.
  3. In-cluster service which will be scraped by Prometheus for metrics (Note the annotation attached to it).
Shell
 




xxxxxxxxxx
1


 
1
root$ kubectl get pods -l k8s-app=kube-state-metrics
2
NAME                                  READY     STATUS    RESTARTS   AGE
3
kube-state-metrics-255m1wq876-fk2q6   2/2       Running   0          2m
4
root$ kubectl get svc  -l k8s-app=kube-state-metrics
5
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)             AGE
6
kube-state-metrics   ClusterIP   10.12.8.130   <none>        8080/TCP,8081/TCP   2m



Deploying Grafana

By now, we have deployed the core of our monitoring system (metric scrape and storage), it is time too put it all together and create dashboards

kubectl apply -f k8s/monitoring/grafana

This will create the following:

  1. Grafana deployment with 1 replica running.
  2. Service with Google Internal Loadbalancer IP, which can be accessed from the VPC (using VPN).
Shell
 




xxxxxxxxxx
1
12


 
1
root$ kubectl get pods
2
NAME                                    READY     STATUS    RESTARTS   AGE
3
grafana-7x23qlkj3n-vb3er                1/1       Running   0          2m
4
kube-state-metrics-255m1wq876-fk2q6     2/2       Running   0          5m
5
prometheus-deployment-69d6cfb5b7-l7xjj  1/1       Running   0          5m
6
alertmanager-42s7s25467-b2vqb           1/1       Running   0          2m
7
root$ kubectl get svc
8
NAME                 TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)             AGE
9
grafana              LoadBalancer   10.12.8.132   10.0.0.8   3000:32262/TCP      2m
10
kube-state-metrics   ClusterIP      10.12.8.130   <none>        8080/TCP,8081/TCP   5m
11
prometheus-service   LoadBalancer   10.12.8.124   10.0.0.7   8080:30698/TCP      5m
12
alertmanager         LoadBalancer   10.12.8.110   10.0.0.6     9093:32634/TCP      5m



All you need to do now is to add the Prometheus server as the data source in Grafana and start creating dashboards. Use the following config:

Name: DS_Prometheus

Type: Prometheus

URL: http://prometheus-service:8080

Note: We are using the Prometheus service name in the URL section as both Grafana and Prometheus servers are deployed in the same cluster. In case the Grafana server is outside the cluster, then you should use the Prometheus service’s external IP in the URL.

Adding Prometheus as a data source in Grafana


Kubernetes Cluster Monitoring Dashboard


All the dashboards can be found here. You can import the json files directly and you are all set.

Note:

  1. No Need to add separate dashboards whenever deploying new service. All the dashboards are generic and templatized.
  2. Prometheus offers hot reloads. So if you need to update the config or rules file, just update the config map and make a HTTP POST request to the Prometheus endpoint. Eg:
Shell
 




xxxxxxxxxx
1


 
1
curl -XPOST http://<Prometheus-Svc-Ext-Ip>:8080>/-/reload 
2
#In the prometheus logs it can be seen as
3
level=info ts=2019-01-17T03:37:50.433940468Z caller=main.go:624 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
4
level=info ts=2019-01-17T03:37:50.439047381Z caller=kubernetes.go:187 component="discovery manager scrape" discovery=k8s msg="Using pod service account via in-cluster config"
5
level=info ts=2019-01-17T03:37:50.439987243Z caller=kubernetes.go:187 component="discovery manager scrape" discovery=k8s msg="Using pod service account via in-cluster config"
6
level=info ts=2019-01-17T03:37:50.440631225Z caller=kubernetes.go:187 component="discovery manager scrape" discovery=k8s msg="Using pod service account via in-cluster config"
7
level=info ts=2019-01-17T03:37:50.444566424Z caller=main.go:650 msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml



  1. Alertmanager config can be reloaded by a similar api call.

curl -XPOST http://<Alertmanager-Svc-Ext-Ip>:9093>/-/reload

I hope this helps you get insights into your Kubernetes cluster and effectively monitor workloads. This set-up should be enough to get you started on monitoring your workloads. In the next post, we will learn about scaling Prometheus horizontally and ensuring High Availability. Stay tuned :)
Feel free to reach out should you have any questions.


This article was originally published on https://appfleet.com/blog/kubernetes-monitoring-using-prometheus/.

Topics:
cloud native, kubernetes, prometheus, tutorial

Published at DZone with permission of Sudip Sengupta . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}