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.
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:
- Kubernetes Cluster: You must have access to a running Kubernetes cluster.
- Kubectl: The Kubernetes command-line tool should be installed and configured to interact with your cluster.
- 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
- How to get self pod with kubernetes client-go
- How to get specific namespace for log fluentd
- How to get the current namespace of current context using kubectl
- How to get the namespace from inside a pod in OpenShift?
- How to get the cpu usage per thread on windows win32
- How to get the IP address of the docker host from inside a docker container
- How to get status code from webclient?
- How to get token from service account?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.