Understanding Kubernetes API - Part 2 : Pods API
Kubernetes is a REST API driven system. All the operations in the system are executed by making the API requests to it’s API server. This makes kubernetes easy to interact from external systems.
Most of the initial interactions with the API is done using kubectl. kubectl is a command line tool which converts the user commands to a API call.But once the user is comfortable with kubectl, for advance operations it will be beneficial to know how to interact with API directly. This gives more power to the user to express the operations more effectively.
So in this series of blog posts, I will be discussing about how to use various kubernetes API’s. This is the second post in the series which discusses about pods API. You can access all the posts in the series here.
Pods API
Pod is a kubernetes abstraction that runs one or more containers.
All pods in kubernetes run in a namespace. All the system related pods run in a namespace called kube-system. By default all the user pods run in default namespace.
Pods API is the part of Kubernetes API which allows user to run CRUD operations on pods.
List Pods
We can list the pods using GET API call to /api/v1/namespaces/{namespace}/pods.
The below curl request lists all the pods in kube-system namespace.
The output contains these important fields for each pod
• metadata - The labels , name etc
• spec - The spec of the pod. This contains the image, resource requirements etc
• status - Status of the pod.
For example, the output looks as below for etcd pod
Get Details about Single Pod
We can access the details of individual pod using api/v1/namespaces/kube-system/pods/{podname}.
Example for etcd pod look as below
Create Pod
Creating Pod requires defining the spec and metadata for the same. Usually user writes a yaml file to define the spec.
The below is the YAML definition for creating ngnix pod.
Kubernetes API accepts the json rather than YAML. The respective json looks as below
User needs to send PUT API call to /api/v1/namespaces/default/pods.
Delete Pod
We can delete individual pod using DELETE API call api/v1/namespaces/kube-system/pods/{podname}.
Conclusion
Pod is a basic abstraction in kubernetes to run one or more containers. In this post, we discussed about how to create, delete and list pods using kubernetes API.