How to Setup Prometheus on Minikube

Prometheus on Minikube

Prometheus is quickly becoming the most popular monitoring tool to use with Kubernetes. Many engineers (DevOps, Systems, Software, etc) however don’t feel completely comfortable playing around with their production Prometheus setups. This is where local environments come in handy. This short step by step guide will help you get a local version of Prometheus up and running on your local system within minutes, so let’s jump in.

Prerequisites

$ minikube config set cpus 4
❗  These changes will take effect upon a minikube delete and then a minikube start
$ minikube config set memory 6144
❗  These changes will take effect upon a minikube delete and then a minikube start
$ minikube delete && minikube start
🔥  Deleting "minikube" in virtualbox ...
💀  Removed all traces of the "minikube" cluster.
😄  minikube v1.16.0 on Ubuntu 18.04
✨  Using the virtualbox driver based on user configuration
👍  Starting control plane node minikube in cluster minikube
💾  Downloading Kubernetes v1.20.0 preload ...
    > preloaded-images-k8s-v8-v1....: 491.00 MiB / 491.00 MiB  100.00% 10.51 Mi
🔥  Creating virtualbox VM (CPUs=4, Memory=6144MB, Disk=20000MB) ...

Step by Step Instructions

1.) The first step is to add the Prometheus helm repository.

$ helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
"prometheus-community" has been added to your repositories

2.) Next, we need to update our helm repository list to ensure when we run helm install commands that it searches the repository we just added.

$ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "prometheus-community" chart repository
...Successfully got an update from the "stable" chart repository
Update Complete. ⎈Happy Helming!⎈

3.) Before we install the Prometheus helm chart we’ll first want to create a new namespace. This step isn’t required, but it’s good practice separate your resources.

$ kubectl create namespace prometheus
namespace/prometheus created

4.) Now that we have a namespace we can go ahead and install the Prometheus helm chart into the namespace (-n flag).

$ helm install prometheus prometheus-community/prometheus -n prometheus
NAME: prometheus
LAST DEPLOYED: Sun Dec 27 16:59:06 2020
NAMESPACE: prometheus
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
The Prometheus server can be accessed via port 80 on the following DNS name from within your cluster:
prometheus-server.prometheus.svc.cluster.local

Get the Prometheus server URL by running these commands in the same shell:
  export POD_NAME=$(kubectl get pods --namespace prometheus -l "app=prometheus,component=server" -o jsonpath="{.items[0].metadata.name}")
  kubectl --namespace prometheus port-forward $POD_NAME 9090

The Prometheus alertmanager can be accessed via port 80 on the following DNS name from within your cluster:
prometheus-alertmanager.prometheus.svc.cluster.local

Get the Alertmanager URL by running these commands in the same shell:
  export POD_NAME=$(kubectl get pods --namespace prometheus -l "app=prometheus,component=alertmanager" -o jsonpath="{.items[0].metadata.name}")
  kubectl --namespace prometheus port-forward $POD_NAME 9093
#################################################################################
######   WARNING: Pod Security Policy has been moved to a global property.  #####
######            use .Values.podSecurityPolicy.enabled with pod-based      #####
######            annotations                                               #####
######            (e.g. .Values.nodeExporter.podSecurityPolicy.annotations) #####
#################################################################################

The Prometheus PushGateway can be accessed via port 9091 on the following DNS name from within your cluster:
prometheus-pushgateway.prometheus.svc.cluster.local

Get the PushGateway URL by running these commands in the same shell:
  export POD_NAME=$(kubectl get pods --namespace prometheus -l "app=prometheus,component=pushgateway" -o jsonpath="{.items[0].metadata.name}")
  kubectl --namespace prometheus port-forward $POD_NAME 9091

For more information on running Prometheus, visit:
https://prometheus.io/

5.) Prometheus should be up and running at this point! In order to access the Prometheus UI we will need to forward our local port 9090 to the pod running Prometheus.

$ kubectl -n prometheus port-forward $(kubectl get pods --namespace prometheus -l "app=prometheus,component=server" -o jsonpath="{.items[0].metadata.name}") 9090
Forwarding from 127.0.0.1:9090 -> 9090
Forwarding from [::1]:9090 -> 9090

You should be able to go to `http://localhost:9090` in a web browser and see the UI.

And run some queries.

6.) We can also setup some temporary port forwarding to access the alert manager as well.

$ kubectl -n prometheus port-forward $(kubectl get pods --namespace prometheus -l "app=prometheus,component=alertmanager" -o jsonpath="{.items[0].metadata.name}") 9093
Forwarding from 127.0.0.1:9093 -> 9093
Forwarding from [::1]:9093 -> 9093

Popping open a web browser and hitting `http://localhost:9093` should show you something similar to the image below.

Wrap Up

Thanks for following this guide to on how to setup Prometheus on Minikube. Be sure to bookmark this page for future use and have fun playing around with Prometheus in a local Minikube environment where you can’t cause harm!

1 thought on “How to Setup Prometheus on Minikube”

  1. Pingback: (Hands on) Setup a Kubernetes Infrastructure Dashboard with Grafana + Prometheus - Swiss Army DevOps

Leave a Reply

Your email address will not be published. Required fields are marked *