Prometheus Query to Get Previous Deployment Image Tag: A Step-by-Step Guide
Image by Frederica - hkhazo.biz.id

Prometheus Query to Get Previous Deployment Image Tag: A Step-by-Step Guide

Posted on

Are you tired of digging through your deployment history to find the previous image tag? Look no further! In this article, we’ll show you how to craft a Prometheus query to get the previous deployment image tag with ease. Whether you’re a seasoned DevOps engineer or a Kubernetes newbie, this guide has got you covered.

What You’ll Need

To get started, make sure you have:

  • A Prometheus instance with Kubernetes metrics enabled
  • A Kubernetes cluster with deployments and image tags
  • A basic understanding of Prometheus queries (don’t worry, we’ll cover the basics)

Understanding Prometheus Queries

Prometheus queries are used to extract specific data from your metrics database. They’re written in a language called PromQL (Prometheus Query Language). Don’t worry if you’re new to PromQL; we’ll break it down into simple terms.

PromQL Basics

A Prometheus query typically consists of:

  • metric_name: The name of the metric you want to query (e.g., kube_deployment_statusedesired_replicas)
  • label: A key-value pair that filters the metric (e.g., deployment="my-app")
  • function: An optional function that manipulates the metric (e.g., max, sum)
  • aggregation: An optional aggregation function that groups the results (e.g., group_by, summarize)

Our Goal: Getting the Previous Deployment Image Tag

Our goal is to write a Prometheus query that returns the previous image tag used in a deployment. We’ll use the following metrics:

  • kube_deployment_status_history: A metric that stores the deployment history
  • kube_deployment_status_image: A metric that stores the image tag used in a deployment

Crafting the Prometheus Query

Now that we have the basics covered, let’s dive into crafting the Prometheus query.

Step 1: Get the Current Deployment Image Tag

kube_deployment_status_image{deployment="my-app", namespace="default"}

This query gets the current image tag used in the my-app deployment in the default namespace.

Step 2: Get the Previous Deployment Image Tag

kube_deployment_status_history{deployment="my-app", namespace="default", revision!="latest"}
  |> sort(revision)
  |> limit(2)
  |> offset(1)
  |> kube_deployment_status_image

This query gets the previous image tag used in the my-app deployment in the default namespace. Here’s how it works:

  • kube_deployment_status_history: We query the deployment history metric.
  • revision!="latest": We exclude the latest revision to get the previous image tag.
  • sort(revision): We sort the results by revision in ascending order.
  • limit(2): We limit the results to the top 2 revisions (the latest and the previous).
  • offset(1): We skip the latest revision and get the previous one.
  • kube_deployment_status_image: We extract the image tag from the previous revision.

Putting it All Together

Now that we have our Prometheus query, let’s put it all together:

kube_deployment_status_image{deployment="my-app", namespace="default"}
  =
  (
    kube_deployment_status_history{deployment="my-app", namespace="default", revision!="latest"}
    |> sort(revision)
    |> limit(2)
    |> offset(1)
    |> kube_deployment_status_image
  )

This query gets the current image tag and the previous image tag used in the my-app deployment in the default namespace.

Visualizing the Results

To visualize the results, you can use a Prometheus visualization tool like Grafana or a Prometheus web interface.

Current Image Tag Previous Image Tag
my-app:1.2.3 my-app:1.2.2

In this example, the current image tag is my-app:1.2.3, and the previous image tag is my-app:1.2.2.

Conclusion

In this article, we’ve shown you how to craft a Prometheus query to get the previous deployment image tag. With this knowledge, you can easily track changes to your deployment image tags and troubleshoot issues in your Kubernetes cluster.

Remember to adjust the query to fit your specific use case, and don’t hesitate to reach out if you have any questions or need further assistance.

Additional Resources

For more information on Prometheus queries and Kubernetes metrics, check out the following resources:

Happy querying!

Frequently Asked Question

Get to know the secrets of Prometheus query to get previous deployment image tag!

What is the Prometheus query to get the previous deployment image tag?

The Prometheus query to get the previous deployment image tag is: `kube_deployment_status_image{namespace=~’$namespace’,deployment=~’$deployment’}`. This query will give you the previous image tag used in the deployment.

How do I specify the namespace and deployment in the Prometheus query?

You can specify the namespace and deployment by replacing `$namespace` and `$deployment` with the actual values. For example: `kube_deployment_status_image{namespace=’default’,deployment=’my-deployment’}`.

Can I use this query to get the current deployment image tag as well?

Yes, you can! The `kube_deployment_status_image` metric gives you the current image tag used in the deployment. If you want to get the current image tag, simply use the same query without modifications.

What if I have multiple deployments in the same namespace?

No worries! You can use the `deployment` label to filter the results for a specific deployment. For example: `kube_deployment_status_image{namespace=’default’,deployment=’my-deployment-1′}` will give you the previous image tag for `my-deployment-1` in the `default` namespace.

Can I use this query in a pipeline or automation script?

Absolutely! You can use the Prometheus query in a pipeline or automation script to automate image tag retrieval. Just be sure to replace the namespace and deployment placeholders with the actual values using your preferred scripting language.

Leave a Reply

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