Kubernetes Pod Using Yaml

In Kubernetes, we have to describe the resources using YAML files. For example, if we want to perform a Kubernetes Create Pod operation, we have to describe a Pod resource using YAML.

You can create a YAML file using any editor of choice in your personal workspace. Below is what a Pod Template file looks like:

apiVersion: v1
kind: Pod
metadata:
  name: myfirsrpod
  labels:
    env: prod
spec:
  containers:
    - name: myfirstContainer
      image: coolgourav147/nginx-custom

Note- Above content of the YAML file can be seen using kubectl explain command

we can also create the YAML definition file using the below command

kubectl run myfirstpod --dry-run --image=testdockerimage -o yaml > myfirstyaml.yaml

Basically, here we describe how our Pod should be defined. An important thing to note here is the image. This image points to the name of our Docker image on Docker Hub.

Applying the YAML using Kubectl

Once we create the files, it's time to apply these files to our Kubernetes cluster. To do so, we use Kubectl. Basically, Kubectl is like a Command-Line Interface (CLI) to interact with the Kubernetes cluster.

To apply the Pod, we have to run the below command:

kubectl apply -f myfirstyaml.yaml

We can use create command as well to create the pod using the same YAML file

kubectl create -f myfirstyaml.yaml

Note- This create command will create all the Kubernetes resources like replica set, namespace, etc. It is not only applicable for pod creation

With this, we have successfully used Kubernetes Create Pod to create a new Pod.

let's list the created pods. using below command

C:\Users\Gaurav>kubectl get pods
NAME       READY   STATUS    RESTARTS   AGE
test-vol   1/1     Running   0          3s
C:\Users\Gaurav>