Thursday, February 4, 2021

Kubernetes for Developers #8: Kubernetes Object Name, Labels, Selectors and Namespace

Object Name

Every Kubernetes object has a Name that is unique for that type of resource within the same namespace. It means, we can only have one Pod name called “mynginx” within the same namespace. however, we can use same name for another Kubernetes object like Deployment, Service, etc.

So, we can set same name i.e., “mynginx” to Pod, Deployment, Service, etc.

Kubernetes Object Name, Labels, Selectors and Namespace

The following characteristics need to be followed while setting name to Kubernetes object.

  • Name should not exceed 253 characters
  • It contains only lowercase alphanumeric characters (a to z), hyphen (-), period(.)
  • It should start with an alphanumeric character
  • It should end with an alphanumeric character

apiVersionv1
kindPod
metadata:
  namemynginx
spec:

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:
    environmentproduction
    appnginx

 

Example: Creating labels on Pod

apiVersionv1
kindPod
metadata:
  namepod-label-demo
  labels:
    environmentproduction
    appnginx
spec:
  containers:
    - namenginx
      imagenginx:1.14.2
      ports:
        - containerPort80


There are two labels i.e. app:nginx, environment:production are assigned to Pod


Example: Creating labels on Deployment
apiVersionv1
kindDeployment
metadata:
  namedeployment-label-demo
  labels:
    environmentproduction
    appnginx
spec:

There are two labels i.e. app:nginx, environment:production are assigned to Deployment

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:nginx
> Kubectl get pods -l app=nginx
// add label to running Pod
// syntax
kubectl label Pod <podname> <key>=<value>

// example
> kubectl label Pod pod-label-demo tier=frontend


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

Selector

It is used for grouping Kubernetes objects and perform actions accordingly. 

a) It is used by Kubernetes Deployment object to talk to all Pods with particular label key/value

b) It is used by Kubernetes Service object to expose all Pods with particular label key/value

Example: Adding labels on Pod template and Selector tag for identifying all Pod labels in the Deployment

apiVersionapps/v1
kindDeployment
metadata:
  namemy-nginx-deployment 
spec:
  replicas2
  selector:
    matchLabels:
      appnginx
  template:
    metadata:
      labels:
        appnginx
    spec:
      containers:
        - namenginx
          imagenginx:1.7.9
          ports:
            - containerPort80

Here, we have added selector tag with matchLabels(app:nginx) used by deployment for identifying all the Pods where label equal to app:nginx and apply Deployment rules

Example: Create Service and communicate with Pods using labels

apiVersionv1
kindService
metadata:
  namemy-nginx-service
spec:
  typeLoadBalancer
  ports:
    - port80
  selector:
    appnginx


Here, we have added selector tag used by Service for identifying all the Pods where label name equal to app:nginx and apply Service rules

Note: matchLabels are not supported by Service. It only supported by Deployment, Replica Set, Daemon Set and Job.

Namespace

Kubernetes supports multiple virtual clusters by using Namespaces. It helps when multiple teams using same cluster and create separate Roles, Binding and Environments for each team. 

  • Kubernetes resource name should be unique within a namespace, but not across namespaces
  • Namespaces cannot be nested
  • Each Kubernetes resource can only be in one namespace
  • Don’t create namespace with prefix “kube-“. Because, it is reserved for K8 system
  • By default, each resource created under “default” namespace
// create namespace using Imperative way
> kubectl create namespace my-namespace


# create namespace using Declarative way
apiVersionv1
kindNamespace
metadata:
  namemy-namespace


> kubectl create -f my-namespace.yaml

# create Pod under my-namespace
apiVersionv1
kindPod
metadata:
  namemy-nginx-pod
  namespacemy-namespace


// get all namespaces
> kubectl get namespace


// get all Pods with specific namespace
> kubectl get pods –namespace=my-namespace


// delete namespace
> kubectl delete namespace my-namespace



// setting namespace preference for kubectl subsequent commands in the current session
> kubectl config set-context --current --namespace=my-name


Kubernetes for Developers Journey.
Happy Coding :)

1 comment:

  1. Looks good. Could you please share how the IP address assigning new and re-create pods.

    ReplyDelete