How to Create and Edit a pod in Kubernetes

We can create pods and other objects (like deployments, services, etc.) using the imperative or declarative method.

Check here to learn more about the Declarative & Imperative ways of creating Kubernetes objects

Let’s see a quick example of creating a pod

Create a Pod using imperative commands

kubectl run nginx --image=nginx

Create a YAML file with pod definition: filename: pod.yaml

apiVersion: v1
kind: Pod
  name: nginx
  - name: nginx
    image: nginx
    - containerPort: 80

Kubectl create -f pod.yaml

Above are the different ways to create a pod.

Edit a pod:

There are scenarios, where we need to do some changes in the pod definition. For example: if we want to change the image of a container or label

There are 2 ways to edit a pod and other Kubernetes objects

1) Using “kubectl edit” command

kubectl edit pod <pod name> 

This will open a editor, where you can find the pod definition, and can be edited directly and saved (same as of VI editor), Once it is edited and saved, This will recreate the object with the latest changes

With the edit command, You can only edit the below fields





2) By extracting the pod definition as a YAML file and do the necessary changes and creating the object again.

kubectl get pod webapp -o yaml > my-new-pod.yaml

Then make the changes to the exported file using an editor (vi editor). Save the changes

vi my-new-pod.yaml

Then delete the existing pod

kubectl delete pod webapp

Then create a new pod with the edited file

kubectl create -f my-new-pod.yaml


We can run a describe command to get the latest definition of the running objects

kubectl describe pod <pod-name>

Check here to learn more about services and their type

Good Luck with your learning. Give a like if you find this post is useful

Similar Posts