Kubernetes is a powerful container orchestration platform that allows you to manage and deploy containerized applications. We all know that two of the most fundamental concepts in Kubernetes are Kubernetes Node and Pods. In this article, we are discussing about Kubernetes Nodes and Pods with the basic commands to work with them.
As assumptions for this article, we have to consider that our applications is already developed and built into Docker images. Also, we have to have Kubernetes cluster up and running. So now our goal is to deploy our application as a container on a set of machines which are configured as worker nodes in a cluster. Kubernetes does not directly deploy containers directly on the worker nodes. It encapsulates the application into a Kubernetes object known as a pod.
Table of Contents
What is a Kubernetes Pod?
Simply we can say: A pod is the smallest object which you can create in Kubernetes. In another word, A pod is a single instance of an application. Pods are the basic building blocks of Kubernetes.
What is a Kubernetes Node?
Nodes are the worker machines that run containerized applications. When you deploy applications as Pods, it will be deployed on one or more nodes which is the actual machine your application is running on. A Pod always runs on a Node. A node can be a physical machine or a virtual machine. The control plane manages each node.
How to create a new Kubernetes pod?
The following command will create a new Kubernetes pod, For example, let’s create a new pod with the nginx image. This will pull the nginx pod (application container) from the public repository
controlplane ~ ➜ kubectl run nginx --image=nginx
pod/redis created
How to check pods and their Status in Kubernetes?
When we create a pod, it will take a small amount of time to create the pod and start it. We can check the current status of the pods by the below commands. here we can find the pod name, status, age and a few other details.
controlplane ~ ➜ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 40s
newpods-dttrd 0/1 ContainerCreating 0 4s
newpods-jgrfb 0/1 ContainerCreating 0 4s
newpods-qhq6f 0/1 ContainerCreating 0 4s
controlplane ~ ➜ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 50s
newpods-dttrd 1/1 Running 0 14s
newpods-jgrfb 1/1 Running 0 14s
newpods-qhq6f 1/1 Running 0 14s
How to delete a Kubernetes pod
You can use the following command to delete a pod
kubectl delete pod <name>
controlplane ~ ➜ kubectl delete pod webapp
pod "webapp" deleted
How to check all the details of a pod including events?
We can use the below command to check all the details of pods, it will return large output including all the details of the pod.
kubectl describe pod
describe a specific pod by name.
kubectl describe pod webapp
Use a pod-definition YAML file to create a new pod
01. We use kubectl run
the command with –dry-run=client -o yaml the option to create a manifest file:
root@controlplane:~# kubectl run redis --image=redis123 --dry-run=client -o yaml > redis-definition.yaml
02. After that, use kubectl create -f
command to create a resource from the manifest file:
root@controlplane:~# kubectl create -f redis-definition.yaml
pod/redis created
03. Verify the work by running kubectl get
command:
root@controlplane:~# kubectl get pods
How to change the image of a pod
We need to specify the container image that will be used for each container to deploy an application in a pod, This can be done via the Pod specification file, which is a YAML or JSON. It defines the desired state of the Pod. In the Pod specification, you can specify the name of the container image, the version, and other configuration parameters, such as environment variables, commands, and arguments. In case if we need to change this image in a pod, we can you below method.
Use the kubectl edit
command to update the image of the pod to redis
.
root@controlplane:~# kubectl edit pod redis
Above command will get you to the definition file where you can see the current config.
Make changes and save & exit with the Esc :wq command. If you used a pod
definition file then update the image name in the definition file via Vi
or Nano
editor and then run kubectl apply
command to update the image:
updating the image
root@controlplane:~# kubectl apply -f redis-definition.yaml
pod/redis configured
How to check Kubernetes nodes status?
controlplane ~ kubectl get nodes
NAME STATUS ROLES AGE VERSION
controlplane Ready control-plane,master 15m v1.26.0+k3s1
retrieve more details on nodes
controlplane ~ kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
controlplane Ready control-plane,master 13m v1.26.0+k3s1 172.25.0.41 <none> Alpine Linux v3.16 5.4.0-1102-gcp containerd://1.6.12-k3s1
We hope you get a basic understanding of Kubernetes Nodes and Pods from this article.