Kubernetes
REST API
Pod Status
DevOps
Monitoring

How to get running pod status via Rest API

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Accessing and managing Kubernetes resources programmatically can greatly enhance your ability to monitor and automate operations within your containerized environments. One crucial aspect of Kubernetes management is the ability to check the status of running pods. The Kubernetes API allows you to accomplish this programmatically via REST API, which is a powerful tool for developers and system administrators alike.

In this article, we will explore how to retrieve the status of running pods using the Kubernetes REST API. We will provide technical explanations, examples, and best practices to ensure you can efficiently access this information.

Prerequisites

Before diving into the specifics, ensure you have a few prerequisites in place:

  1. Kubernetes Cluster: You must have access to a running Kubernetes cluster.
  2. Kubectl: The Kubernetes command-line tool should be installed and configured to interact with your cluster.
  3. Authentication: Ensure you have appropriate permissions and authentication details to access the API server.

Understanding the Kubernetes API

Kubernetes exposes functionality through a RESTful API. All resource operations (such as those pertaining to pods) are made available via HTTP requests. The API server acts as a central management entity for processing these requests.

API Endpoint for Pods

To retrieve information about pods, you can target the `/api/v1/namespaces/{namespace}/pods/{pod-name}` endpoint. The endpoint structure is as follows:

• `api/v1`: Specifies the API version. • `namespaces`: Refers to the contextual grouping of resources. • `{namespace}`: Replace this with the actual namespace in which your pod resides. • `pods`: The resource type. • `{pod-name}`: The name of the specific pod you're querying.

Fetching Pod Status via REST API

Here's a step-by-step guide to fetching the status of running pods via a REST API call:

Step 1: Determine the API Server URL

First, you need the URL for the Kubernetes API server. This typically looks like `https://``<api-server-ip>``:``<port>```. You can find this URL in your kubeconfig file, usually located at `$HOME/.kube/config`.

Step 2: Authenticate with the API Server

When making an API request, you must authenticate using a token. This token can be obtained from a ServiceAccount or directly from kubeconfig.

• `-X GET`: Specifies the HTTP method. • `-H "Authorization: Bearer ``<YOUR_TOKEN>``"`: Passes the authentication token in the header. • `-H "Accept: application/json"`: Ensures the response is JSON formatted. • `-k`: (Optional) Ignores SSL certificate validation; use with caution. • status.phase: Indicates the pod's current state (`Pending`, `Running`, `Succeeded`, `Failed`, `Unknown`). • containerStatuses: Provides information on each container within the pod, including states such as `waiting`, `running`, `terminated`. • conditions: Lists the conditions the pod is subjected to, like `Initialized`, `Ready`, `ContainersReady`.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.