Monday, March 29, 2021

Kubernetes for Developers #11: Pod Organization using Labels

In Microservice architecture, The number of deployed Pods will be replicated more than ones and multiple releases (i.e. stable, beta, canary) will run concurrently. This can lead to hundreds of pods within no time. So, organizing pods are crucial in microservice architecture.


Pods without Labels:
As per below image, we are running multiple microservices with multiple replicas and multiple releases without Pod labels. It is evident that we need to find a way to organize the Pods into smaller groups based on arbitrary criteria. Here, Pod Labels help us to organizing Pods into different groups.

Pods with Labels:
As per below image, we are running multiple microservices with multiple replicas and multiple releases with Pod labels. Each Pod is labeled with two labels
  • app, it specifies Pod belongs to which app, component, or service
  • release, it specifies Pod running under stable, beta or a canary release

Labels

Label is a key-value pair which is attached to pods, deployments, etc.

  • It acts an identifier on K8 object (Ex: Pod or Deployment). So, the other Kubernetes objects (Ex: Service, DaemonSet) can communicate by matching same label names.
  • It can be attached to Kubernetes objects at creation time or directly on live objects by using Imperative way
  • Same label key/value can be assigned to multiple Kubernetes objects
  • Each object in the label can have set of key/value and each key must be unique for a given object
  • Label key should not exceed 63 characters and allowed characters are alphanumeric, dash(-), underscore(_), dot(.)
  • Label value should not exceed 63 characters and allowed characters are alphanumeric, dash(-), underscore(_), dot(.)
metadata:
  namepod-label-demo
  labels:
    releasestable
    appui


Example: Creating labels on Pod
apiVersionv1
kindPod
metadata:
  namepod-label-demo
  labels:
    releasestable
    appui
spec:
  containers:
    - namenginx
      imagenginx:1.14.2
      ports:
        - containerPort80


There are two labels i.e. app: ui, release: stable are assigned to Pod

Use following kubectl commands to find each k8 object and respective labels

// show all labels on each Pod
> kubectl get pods --show-labels

// show all labels on each Deployment
> kubectl get deployments --show-labels

// show all Pods where label app: ui
> Kubectl get pods -l app=ui

// add label to running Pod
// syntax
kubectl label Pod <podname> <key>=<value>
// example
> kubectl label Pod pod-label-demo release=stable

// delete Pods using label
> kubectl delete pod -l app=ui


Kubernetes for Developers Journey.

Happy Coding :)


No comments:

Post a Comment