Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Using Python to Extract Prometheus Metrics

This lab guides the student through the use of a Python program to interface with the Prometheus API endpoint. The program will use PromQL examples to pull CPU and memory metrics and output them in a Comma Separated Value (CSV) file that may then be used for a Machine Learning program. This lab only covers the extract step and the Machine Learning part is covered in a subsequent lab.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 1h 0m
Published
Clock icon Apr 05, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Gain access to the master node with a terminal emulator.

    To use SSH to access the master node, enter:

    ssh cloud_user@[Master's Public IP Address]
    

    You will be prompted for the cloud_user password that is available on the lab startup page.

  2. Challenge

    Review the promql.py program in GitHub.

    To review the promql.py program, navigate to the following GitHub address:

    https://github.com/linuxacademy/content-aiops-essentials/blob/master/promql.py
    
  3. Challenge

    Start the promql.py program on the master node.

    From your terminal session on the master node, enter the following command to start the Python program:

    python3 promql.py > promql.out 2> promql.err &
    

    Note: Be sure you use the ampersand after the command so it will run in background on your server.

  4. Challenge

    As the promql.py program is running, stress the cluster.

    To stress the cluster, you may deploy the stress-test deployment with the following command:

    kubectl create -f stress-test.yaml
    
  5. Challenge

    Run the Prometheus dashboard as you vary cluster load.

    Navigate in your browser to the Prometheus dashboard:

    http://[Master Node IP]:9090
    

    While the Python program is gathering metrics, use the following command to vary the load by changing the number of replicas:

    kubectl scale deployment.v1.apps/stress-test --replicas=10
    

    You may then increase the number of replicas to 20, 30, 40, and so on.

    kubectl scale deployment.v1.apps/stress-test --replicas=[number here]
    

    If you want to use other PromQL in the Prometheus dashboard, here are the two examples we use in the Python promql.py program:

    100 - avg(irate(node_cpu_seconds_total{job="node",mode="idle"}[5m])) by (instance) * 100
    

    And:

    (node_memory_MemTotal_bytes - (node_memory_MemFree_bytes + node_memory_Cached_bytes + node_memory_Buffers_bytes)) / node_memory_MemTotal_bytes * 100
    
  6. Challenge

    Examine the Python program output.

    You may look at the promql.py output with any of the following commands:

    tail promql.out
    

    Or:

    more promql.out
    

    Or:

    tail -f promql.out
    

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans