Table of Contents
In this tutorial, I will take you through 3 easy ways to deploy/create pods in Kubernetes. If you are using docker from long time then you might be aware that docker uses containers to deploy applications in a Server which is very specific to that Server and cannot be shared or migrate to another Server. If you are looking to create a container independent architecture where applications can be deployed without depending on a specific resource then Kubernetes pods are the most suitable. More can be checked on Kubernetes Documentation.
What are Pods
Pods are the most basic unit in Kubernetes Cluster where application can be deployed either in a single container environment or in multiple container environment.
Pods uses an overlaying network architecture in Kubernetes to communicate internally with each other and then it also has an external IP to communicate with outside world. A pod running on multiple containers can share same resources.
3 Easy Methods to Deploy/Create Pods in Kubernetes Cluster
Also Read: How to Create New Custom Namespaces in Kubernetes{3 Best Methods}
Method 1: Deploy/Create Pods in Kubernetes Cluster Using YAML File
One of the method which is used extensively by Kubernetes professional is through YAML File. You can specify all the resources like name of pod, label, image, ports etc required to create pod in a YAML File and then create it using a single line command. Here we are creating a pod web-app
in current namespace using nginx
image and container running on Port 80
.
[root@localhost ~]# vi pod.yaml apiVersion: v1 kind: Pod metadata: name: web-app labels: role: approle spec: containers: - name: web-app-cont image: nginx ports: - name: web-app-port containerPort: 80 protocol: TCP
After providing all the pod configuration on pod.yaml
file, we are creating the pod by using kubectl apply -f pod.yaml
command as shown below.
[root@localhost ~]# kubectl apply -f pod.yaml pod/web-app created
If everything goes well then you will be able to see web-app
pod in running state using kubectl get pods | grep -i web-app
command as shown below.
[root@localhost ~]# kubectl get pods | grep -i web-app web-app 1/1 Running 0 26s
If you want you can also delete the pod by using kubectl delete pod web-app
command. This will simply remove the pod along with its resources from Kubernetes Cluster.
[root@localhost ~]# kubectl delete pod web-app pod "web-app" deleted
Method 2: Deploy/Create Pods in Kubernetes Cluster Using JSON File
The second method that you might need to use is through JSON File. Ideally if you have YAML file then you can just rename that file to .json
and use it as JSON file like we are doing it here. We just have taken pod.yaml
file from above method and renamed it to pod.json
using mv pod.yaml pod.json
command as shown below.
[root@localhost ~]# mv pod.yaml pod.json
Then creating the pod from pod.json
file by simply using kubectl create -f pod.json
command as shown below.
[root@localhost ~]# kubectl create -f pod.json pod/web-app created
You can also verify the pod.json
contents by opening the file using our favorite vi
editor as shown below. Alternatively you can also use cat pod.json
command to view the contents.
[root@localhost ~]# vi pod.json apiVersion: v1 kind: Pod metadata: name: web-app labels: role: approle spec: containers: - name: web-app-cont image: nginx ports: - name: web-app-port containerPort: 80 protocol: TCP
Once the pod is created you can verify its status by running kubectl get pods | grep -i web-app
command.
[root@localhost ~]# kubectl get pods | grep -i web-app web-app 1/1 Running 0 34s
Method 3: Deploy/Create Pods in Kubernetes Cluster Using kubectl command
There is another method that you can use is through command line interface where you can just run a command to create pod in kubernetes cluster as shown below. Syntax for creating pod will be kubectl create deployment <pod_name> --image=<image_name>
. Here we are creating web-app
pod by using kubectl create deployment web-app --image=nginx
command.
[root@localhost ~]# kubectl create deployment web-app --image=nginx deployment.apps/web-app created
Above command will create a web-app deployment
which will further create web-app-854dfd94fc-5jd59
pod as shown below.
[root@localhost ~]# kubectl get pods | grep -i web-app web-app-854dfd94fc-5jd59 1/1 Running 0 20s
Since it is a deployment so it is important to note here that if you try to delete the web-app-854dfd94fc-5jd59
pod it will recreate the same pod with different name. So to completely remove the pod you need to delete the deployment itself by using kubectl delete deployment web-app
command as shown below.
[root@localhost ~]# kubectl delete deployment web-app deployment.apps "web-app" deleted
Now if you check the status you will see pod is now in Terminating
state.
[root@localhost ~]# kubectl get pods | grep -i web-app web-app-854dfd94fc-5jd59 0/1 Terminating 0 58s
After sometime it will get terminated and now no pod is in running state.
[root@localhost ~]# kubectl get pods | grep -i web-app
Popular Recommendations:-
Create a Service to Expose Your Apps on Kubernetes(v1.16)
How to Install and Configure Kubernetes on Redhat/CentOS 7 with Best Example
Best 15 Kubectl and Kubeadm Commands
How to Check Stateful and Stateless Pods in Kubernetes Cluster
22 Best Kubectl Command Examples
6 Easy Steps to Setup and Manage Log rotation Using logrotate in Linux
Migrate CentOS 8 to CentOS Stream 8 in 6 Easy Steps
26 iostat, vmstat and mpstat command examples to Monitor Linux Performance