Working With Pods, Labels and Selectors

Working With Pods, Labels and Selectors

Pods are the smallest unit of the Kubernetes cluster. container resides in these pods. There are multiple container engines in the market like docker, rkt, containerd etc. Kubernetes provides support for all of them. these engines create containers inside the pods. pods are just enveloped around the containers.

To create pods there are two ways one is imperative ( using commands ) way, another one is declarative ( using YAML/JSON files ). these files are called Kubernetes manifest files. In this article, we are going to learn how to create pods using YAML files.

Let's get started.

Create and open a file with the name mypod.yml using vi command.

$ vi mypod.yml

Write the following pod specifications in the file.

kind: Pod         # It is the what kind of k8s object will get created
apiVersion: v1    # version of the kubernetes API
metadata:
  name: httppod            # name of the pod
spec:
  containers:
    - name: container      # Name of the container
      image: httpd         # Image for the container
      ports:
       - containerPort: 80 # Contaier's exposed port

# to exit from the file press esc then shift colon + wq + Enter [:wq]

Now to create a pod run the following command.

$ kubectl apply -f mypod.yml
# You can use create instead of apply

Check is pod created or not using the kubectl get pod command.

If the pod is created and the container inside is running then the status will be Running.

To get more information about the pod -o wide is used.

$ kubectl get pod -o wide

To get all the details of the pod and even of the container the following command is used

$ kubectl describe pod <pod-name>

Labels and Selectors in Kubernetes:

Labels are given to the Kubernetes objects like Pods, Deployments etc. We can label objects through commands which is a imperative way or can be defined in the manifest file which is declarative way. in the manifest file, they are written in key-value pair format.

Now let's see how labels are given with an example of the pod.

kind: Pod
apiVersion: v1
metadata:
  name: pod-label
  labels:
    environment: production
    app: nginx
  spec:
    containers:
    - name: nginx
      image: nginx:latest
      ports:
      - containerPort: 80

Now to get the labels of the pod.

$ kubectl get pods --show-labels

If the pod is running already then we can use an imperative way to label them.

$ kubectl label pod <pod-name> key=value

Let's talk about label selectors. Selectors are nothing different, They are just used to select objects using labels. selectors can be of two types, one is equality based and another one is set based

Equality Based:

For convenience i'm using pods that are created above. let's select them using labels

# command to select pods which have the specifies label
$ kubectl get pods -l <key>=<value>

# command to select pods which don't have specified label
$ kubectl get pods -l <key>!=<value>

Set Based:

For set-based selectors [ in, notin, exist ] these keywords are used.

# command to select pods which have the specifies label
$ kubectl get pods -l '<key> in ( value1,value2 )'

# command to select pods which don't have specified label
$ kubectl get pods -l '<key> notin ( value )'

You can also delete the pods using label-selectors

$ kubectl delete pods -l <key>=<value>

Conclusion:

The article was about, how pods are created using yaml manifest files, how to get detailed data about the pods, how to label them, how to select them using that labels etc etc etc....

Thank You for reading!!!

Did you find this article valuable?

Support Pranav Thorve by becoming a sponsor. Any amount is appreciated!