Kubernetes monitoring — How to monitor using prometheus?

Arun Kumar Peddapalli
Fournine Cloud
Published in
6 min readOct 26, 2022

--

How to monitor kubernetes using promethues?

Why Monitor Kubernetes Containers in Production?

Running containers at production scale positions challenges for the DevOps Engineers who are responsible for the orchestration and versioning of its lifecycle. Kubernetes can be deployed as a public cloud or private on-premise software

When monitoring Kubernetes containers in production, there are multiple options that you have to choose from. They all won’t offer you the same level of features or quality and so you need to find good tools that suit your needs like

  1. Ability to handle a large number of containers
  2. Data visualisation is available
  3. 3rd party support before deploying new version

How Prometheus helps monitoring Kubernetes Containers in Production?

Prometheus is an open-source monitoring system and time series database. It collects metrics from configured targets at given intervals, evaluates rule expressions, displays the results, and can trigger alerts if some condition is observed to be true.

Prometheus and Grafana Kubernetes Cluster monitoring provides information on potential performance bottlenecks, cluster health, performance metrics. At the same time, visualize network usage, resource usage patterns of pods, and a high-level overview of what is going on in the cluster.

Prometheus became most used Kubernetes monitoring system because of these features:

  1. Multi-dimensional data model: it’s based on key-value pairs, much as how Kubernetes labels infrastructure metadata. It powers the Prometheus query language and enables flexible and precise time series data.
  2. Accessible format and protocols: Metrics are published utilising a common HTTP transport, are human readable, and have self-explanatory formats.
  3. Service discovery: Prometheus server is responsible for routinely scraping the targets (metrics are pulled, not pushed). It’s ideal for ephemeral Kubernetes workloads because they can be set to filter and match container metadata.
  4. Modular and high available components: Different composable services carry out tasks like metric gathering, alerting, graphic presentation, etc. These services are all made to facilitate sharding and redundancy.

Setting up Promotheus in kubernetes

The Best way to set up Prometheus is to use Helm charts to install it in a Kubernetes cluster. As a prerequisite, you need to have Helm installed on the machine.

Authenticate to your cluster and Perform below commands to install Prometheus

  • Add the Helm repo for Prometheus and Grafana.

To install Prometheus, you first need to add theprometheus-community Helm repository by using the helm repo addcommand followed by the helm repo updatecommand to pull in the latest metadata:

  • Create a namespace for monitoring
    Create a new namespace for monitoring to isolate the monitoring resources
  • Install the Prometheus chart in the monitoring namespace.

Before you install Prometheus, check out the configuration options you have, because there are a lot of them. you can install Prometheus with the default configuration using the helm install command:

helm install prometheus prometheus-community/prometheus \
--namespace monitoring \
--set alertmanager.persistentVolume.storageClass="default" \
--set server.persistentVolume.storageClass="default"

After installing the promotheus and configuring it, follow the below commands

  • Run the following command to confirm your Kube-Prometheus stack deployment.
kubectl get pods -n monitoring
  • Accessing the Prometheus Instance and Viewing the Internal State Metrics
    After successfully deployed Prometheus instances to Kubernetes cluster, the next step is to use them to monitor the cluster. To do this, we must enable traffic to the Prometheus pod to observe your cluster’s internal metrics. This will also enable you to access the Prometheus server from your browser.
  • Run the following command to obtain the name of the Prometheus server to which you will be forwarding traffic to
kubectl get svc -n monitoring
promotheus server fro which we forward traffic

Run the below kubectl port-forward command to forward the local port 9090 to your cluster via the Prometheus service

kubectl port-forward svc/prometehus-prometheus-server \
-n monitoring 9090

Visualising with Grafana

Until now you’re done with setting up promotheus to collect the data now we are going to use grafana to visualize the data that we’ve collected

  • Install the Grafana chart with the arguments below.

Please change the admin password for the login.

helm install grafana grafana/grafana \
--namespace monitoring \
--set persistence.storageClassName="default" \
--set persistence.enabled=true \
--set adminPassword='Yourfavpassword' \
--set service.type=LoadBalancer
  • Get the list of services in monitoring namespace
kubectl get svc -n monitoring
list of services in monitoring namespace
  • run the below kubectl port-forward command to forward the local port 3001 to your cluster via the grafana service

kubectl port-forward svc/grafana -n monitoring 3001:80
Grafana Login Page
  • Login with admin username admin and password
Grafan welcome Page
  • Add Prometheus datasource

If you mouse over the cogwheel on the left-hand side of the Grafana screen, you’ll be prompted with several configuration options. Choose “Data Sources,” followed by “Add data source.”

Here, you’ll see a long list of data sources that Grafana knows how to talk to automatically, and luckily Prometheus is on that list. Choose “Prometheus” and you’ll be brought to the configuration screen for your new data source.

Data source Dashboard

If you didn’t change the default configuration when installing Prometheus, you’ll only need to give it a name of your choosing, as well as the URL where Prometheus is running. Since both are running in the same cluster, you can connect Grafana to Prometheus using the internal DNS to Kubernetes by providing it the service name that Prometheus is connected to http://prometehus-prometheus-server:9090

  • Importing Dashboard

To import this dashboard, mouse over the “Dashboards” section on the left-hand side of the Grafana screen (the icon is four squares) and choose “Manage.” On the top right of the dashboard management screen, click “Import” and you’ll be prompted for the URL, ID, or JSON for the dashboard that you wish to import.

Import Dashboard
  • Under “Import via grafana.com,” enter “10000,” matching the ID of the dashboard that you wish to import.
  • Feel free to change the name or the unique identifier, but the one thing you must provide is the data source, which is asked for on the bottom of the configuration screen. Choose your Prometheus data source, click “Import,” and you will be greeted by your newly created, but fully populated, dashboard!
Grafana dashboard with data

And that’s a wrap, this is how we use promotheus to monitor kubernetes.If you’ve any queries do comment in the below and don’t forget to follow our publication to never miss out this type of content.

--

--