Monitor Kubernetes deployments using Prometheus & Grafana
Setting up a monitoring solution for your Kubernetes deployments is a must, but not necessarily complicated. This post will get you started with a working Prometheus & Grafana set up in no time which will be used to monitor important metrics of your Kubernetes deployments, such as CPU/RAM/Disk I/O.
In my previous post, I explained how to deploy a WordPress blog to Kubernetes using Minikube.
This post is a continuation of where we had left off. Any deployment on Kubernetes which can’t be monitored is of no use. That is why it is important to have an insight into how your pods are behaving. So, in this post, we will see how we can put monitoring on our WordPress deployments using Prometheus & Grafana.
Requirements.
- Minikube — v1.9.2
- VirtualBox — v6.0.18
- Kubectl — v1.18.2
- Helm — v3.2.0
This tutorial is based on the versions I mentioned above. It should work just fine with any version (as long as it is not too old).
Steps to perform.
Before doing anything else, make sure that you have started your Minikube cluster. If not, then please start it by entering…
admin@shashank-mbp ~/wordpress-minikube> minikube start --vm-driver=virtualbox
😄 minikube v1.9.2 on Darwin 10.14.6
✨ Using the virtualbox driver based on existing profile
👍 Starting control plane node m01 in cluster minikube
🔄 Restarting existing virtualbox VM for "minikube" ...
🐳 Preparing Kubernetes v1.18.0 on Docker 19.03.8 ...
🌟 Enabling addons: default-storageclass, storage-provisioner
🏄 Done! kubectl is now configured to use "minikube"
1. Add a Helm repository.
The first step is to add a Helm repository. Helm is a package manager for Kubernetes. It makes installation, up-gradation & management of Kubernetes applications easy.
A Helm repository is similar to your package repository — Like the ones you have on Linux-based systems.
To add it, enter the below command.
admin@shashank-mbp ~/wordpress-minikube> helm repo add stable https://kubernetes-charts.storage.googleapis.com/"stable" has been added to your repositories
Once it is added, time to update the repository. This is similar to yum update
or apt update
.
For this, enter the below command.
admin@shashank-mbp ~/wordpress-minikube> helm repo upHang tight while we grab the latest from your chart repositories......Successfully got an update from the "stable" chart repositoryUpdate Complete. ⎈ Happy Helming!⎈
2. Create a Kubernetes namespace.
I am creating a different namespace to keep things tidy.
admin@shashank-mbp ~/wordpress-minikube> kubectl create namespace monitoring
namespace/monitoring created
3. Install prometheus-operator
prometheus-operator creates & configures Prometheus monitoring instances.
We will install it using the Helm chart under the monitoring namespace.
admin@shashank-mbp ~/wordpress-minikube> helm install monitoring stable/prometheus-operator --namespace=monitoringmanifest_sorter.go:192: info: skipping unknown hook: "crd-install"manifest_sorter.go:192: info: skipping unknown hook: "crd-install"manifest_sorter.go:192: info: skipping unknown hook: "crd-install"manifest_sorter.go:192: info: skipping unknown hook: "crd-install"manifest_sorter.go:192: info: skipping unknown hook: "crd-install"manifest_sorter.go:192: info: skipping unknown hook: "crd-install"NAME: monitoringLAST DEPLOYED: Fri May 8 15:07:32 2020NAMESPACE: monitoringSTATUS: deployedREVISION: 1NOTES:The Prometheus Operator has been installed. Check its status by running:kubectl --namespace monitoring get pods -l "release=monitoring"Visit https://github.com/coreos/prometheus-operator for instructions on howto create & configure Alertmanager and Prometheus instances using the Operator.
The above step will pull a few Docker images, so wait for a few minutes before all the resources are deployed. You can check the status by entering…
admin@shashank-mbp ~/wordpress-minikube> kubectl get po --namespace=monitoringNAME READY STATUS RESTARTS AGEalertmanager-monitoring-prometheus-oper-alertmanager-0 0/2 ContainerCreating 0 38smonitoring-grafana-5c4dbdb56b-npdbd 0/2 Init:0/1 0 3m33smonitoring-kube-state-metrics-96f87d848-ljwwt 1/1 Running 0 3m33smonitoring-prometheus-node-exporter-flzwc 1/1 Running 0 3m33smonitoring-prometheus-oper-operator-79b7b785f7-j2x7k 0/2 ContainerCreating 0 3m33sprometheus-monitoring-prometheus-oper-prometheus-0 0/3 ContainerCreating 0 28s
Once all the pods are running, we’re ready to start monitoring our WordPress deployment (or any other applications, for that matter).
4. Open Grafana’s dashboard.
To open Grafana’s dashboard, we’ll first need to forward its pod’s port to a different port. For this, note the pod name from the output of the above command & then enter the below command. I have exposed it to port 3000. You can choose any port.
admin@shashank-mbp ~/wordpress-minikube> kubectl port-forward -n monitoring monitoring-grafana-5c4dbdb56b-npdbd 3000Forwarding from 127.0.0.1:3000 -> 3000Forwarding from [::1]:3000 -> 3000
Open your browser & enter http://localhost:3000.
The username & password is: —
username:- admin
password:- prometheus-operator
Important: Please note that the recent versions (I now have Grafana v7.0.3 (00ee734baf)) have a different password — prom-operator. Thanks to Noah for pointing this out.
5. Explore different dashboards.
To check the graphs for compute metrics, select the options available under Kubernetes/Compute Resources.
Feel free to explore various dashboards. Prometheus Operator also installs Alert Manager & Prometheus dashboards, but I am not covering these topics in this post.
I’ll leave you with a few screenshots of various graphs drawn by Grafana, including the graphs for the WordPress UI & MySQL database.
In the next post, I will show how to ship Kubernetes logs from this Minikube cluster to Elasticsearch on AWS.
I hope you liked this article.