Steps To Create A POD With Labels Using KUBECTL And YAML

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

CREATING POD WITH LABELS USING KUBECTL

  • Log into the master server through putty.
Steps to create a POD with labels using KUBECTL and YAML
  • use the below command to create a pod with labels.

Syntax: kubectl run <pod name> --image=<container image name> --port=<container port> --labels="<value1>,<value2>" --generator=run-pod/v1

Example: kubectl run lab-pod --image=nginx --port=80 --labels="app=web,env=dev" --generator=run-pod/v1

Steps to create a POD with labels using KUBECTL and YAML
  • The command executed successfully.
Steps to create a POD with labels using KUBECTL and YAML
  • After few seconds, the POD will be in running state.
Steps to create a POD with labels using KUBECTL and YAML
  • you can verify the label details using the describe command.

kubectl describe pod lab-pod

Steps to create a POD with labels using KUBECTL and YAML
Steps to create a POD with labels using KUBECTL and YAML

CREATING POD WITH LABELS USING YAML

  • Create a YAML file and paste the below coding.
Steps to create a POD with labels using KUBECTL and YAML

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

Steps to create a POD with labels using KUBECTL and YAML
  • Save and close the file.
  • Use the below command to create a POD using the YAML file.
Steps to create a POD with labels using KUBECTL and YAML
  • Command executed successfully.
Steps to create a POD with labels using KUBECTL and YAML
  • After few seconds, the POD will be in running state.
Steps to create a POD with labels using KUBECTL and YAML

LISTING THE POD LABELS

  • We can list the all pod labels using this command.

kubectl get pods --show-labels

Steps to create a POD with labels using KUBECTL and YAML
  • We can also use the label selector to filter the required pods.

kubectl get pods -l env=test

Steps to create a POD with labels using KUBECTL and YAML

CREATE A NEW LABEL FOR A POD

  • use the below command to create a new label for a pod.

Syntax: kubectl label <object type> <object name> <label values>

Example: kubectl label pod new-pod version=0.1

Steps to create a POD with labels using KUBECTL and YAML
  • Verify the new label information.
Steps to create a POD with labels using KUBECTL and YAML

MODIFY THE EXISTING LABEL

  • Use the below command to update the existing label information.

Syntax: kubectl label <object type> <object name> <label values> --overwrite

Example: kubectl label pod new-pod version=2 --overwrite

Steps to create a POD with labels using KUBECTL and YAML
  • Verify the changes using show labels option.
Steps to create a POD with labels using KUBECTL and YAML

DELETE A LABEL FOR A POD

  • Use the below command to delete a label for a POD.

Syntax: kubectl label <object type> <object name> <label value> -

Example: kubectl label pod new-pod version-

Steps to create a POD with labels using KUBECTL and YAML
  • We can verify the settings use show labels option.
Steps to create a POD with labels using KUBECTL and YAML

EXTERNAL LINKS

https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/

VIDEO

Thanks for reading this blog. We hope it was useful for you to learn about labels functionality in the kubernetes environment.

Loges

Leave a Reply

Your email address will not be published. Required fields are marked *