Manage Pods
In the previous tutorial, we saw what a Pod is and how it is different from a container. Now let us see how a Pod is created.
There a two ways a Pod can be created:
- Using a YAML file
- Using the
kubectl runcommand
Pod Specification (YAML File)
A Pod specification is defined in a YAML file that describes the desired state of the Pod. The YAML file includes information such as the name of the Pod, the containers it should run, and other configurations.
It is recommended to create any Kubernetes object using a manifest file (YAML). This way we can achieve version control and consistency in the configuration.
Create a Pod
To create a Pod, you can use the kubectl apply command with a YAML file that defines the Pod. Here's an example of a simple Pod definition:
apiVersion: v1
kind: Pod
metadata:
name: frontend-pod
spec:
containers:
- name: nginx-container
image: nginx:1.14.2
You can save this YAML content to a file named pod.yaml and then run the following command to create the Pod:
kubectl apply -f pod.yaml
Pods are generally not created directly and are created using workload resources.
Why don't we run Pods directly? If you remember from our initial tutorial, the reason we use Kubernetes container orchestration platform is that it provides features like scaling, self-healing, and rolling updates etc.
As we learnt, a Pod is ephemeral and the basic deployable unit in Kubernetes which does not provide those features when created directly. If you delete the pod that was created above, Kubernetes will not create a new one. Pod does not have that capability.
Instead, higher-level controllers like Deployments, StatefulSets, or DaemonSets are used to manage the lifecycle of Pods. These controllers provide features such as scaling, rolling updates, and self-healing capabilities that ensure the desired state of the application is maintained.
To see the existing pods and their statuses:
kubectl get pods
vagrant@vagrant:~/kubernetes-tutorial$ kubectl get pods NAME READY STATUS RESTARTS AGE frontend-pod 1/1 Running 0 2m53s
To get more details about the Pod like it's creation time, containers running inside the Pod, events, and other information:
kubectl describe pod frontend-pod
Update a Pod
To update a Pod, you can modify the YAML file and apply the changes again. For example, if you want to change the image of the container, you can update the image field in the YAML file:
apiVersion: v1
kind: Pod
metadata:
name: frontend-pod
spec:
containers:
- name: nginx-container
image: nginx:latest
After updating the YAML file, run the kubectl apply command again to apply the changes:
kubectl apply -f pod.yaml
Delete a Pod
To delete a Pod, you can use the kubectl delete command followed by the name of the Pod:
kubectl delete pod frontend-pod
Alternatively, you can also delete the Pod using the YAML file:
kubectl delete -f pod.yaml
Pod - Managing Imperatively
You can also manage Pods imperatively using the kubectl run command. For example, to create or delete a Pod named frontend-pod with the nginx image, you can run:
kubectl run frontend-pod --image=nginx
kubectl delete pod frontend-pod
Creating Kubernetes objects imperatively is not usually done in real time production environments.
Conclusion
Managing Pods in Kubernetes is straightforward with the kubectl command-line tool. You can create, update, and delete Pods using YAML files and the appropriate kubectl commands. Always remember to check the status of your Pods after making changes to ensure they are running as expected.
kubectl get pods
This command will list all the Pods in your cluster, allowing you to verify that your Pod is running correctly.