Steps to create a POD with labels using KUBECTL and YAML
In this blog, we will show you the steps to create a POD with labels using KUBECTL and YAML file in your environment.REQUIREMENTS
- 2 Node Cluster ( 1 Master VM with 2 Nodes)
- Kubernetes Components
INFRASTRUCTURE OVERVIEW
- We have already installed and configured the 2 Node cluster in our demo environment.
- Please check the URL https://www.assistanz.com/steps-to-install-kubernetes-cluster-manually-using-centos-7/ for more information.
CREATING POD WITH LABELS USING KUBECTL
- Log into the master server through putty.

- use the below command to create a pod with labels.
Syntax: kubectl run --image= --port= --labels="," --generator=run-pod/v1
Example: kubectl run lab-pod --image=nginx --port=80 --labels="app=web,env=dev" --generator=run-pod/v1

- The command executed successfully.

- After few seconds, the POD will be in running state.

- you can verify the label details using the describe command.
kubectl describe pod lab-pod


CREATING POD WITH LABELS USING YAML
- Create a YAML file and paste the below coding.

apiVersion: v1
kind: Pod
metadata:
   name: new-pod
   labels:
      app: web
      env: test
spec:
    containers:
       - name: test-cont
         image: nginx

- Save and close the file.
- Use the below command to create a POD using the YAML file.

- Command executed successfully.

- After few seconds, the POD will be in running state.

LISTING THE POD LABELS
- We can list the all pod labels using this command.
kubectl get pods --show-labels

- We can also use the label selector to filter the required pods.
kubectl get pods -l env=test


CREATE A NEW LABEL FOR A POD
- use the below command to create a new label for a pod.
Syntax: kubectl label