Labels, Selectors, Replication, and Replicaset in Kubernetes

Labels, Selectors, Replication, and Replicaset in Kubernetes

All about Labels, Selectors, Replication, and replicaset in Kubernetes

ยท

5 min read

Labels

  • Labels are the mechanism you use to organize Kubernetes Objects.
  • A label is a key-value pair without any predefined meaning that can be attached to the objects.
  • Labels are similar to tags in AWS or git where you use a name for quick reference.
  • So we are free to choose labels as we need them to refer to an environment that is used for dev or testing or production, refer a product group like department A, or department B.
  • Multiple labels can be added to a single object.
  • # pod5.yml
    kind: Pod
    apiVersion: v1
    metadata:
      name: delhipod
      labels:
        env: development
        class: pods
    spec:
      containers:
        - name: c00
          image: ubuntu
          command:
            [
              "/bin/bash",
              "-c",
              "while true; do echo Hello DevOps; sleep 5 ; done",
            ]
    
    kubectl apply -f pod5.yml
    kubectl get pods --show-labels
    

  • Now if you want to add a label to an existing pod
  • kubectl label pods delhipod myname=tushar
    kubectl get pods --show-labels
    

  • Now, list pods matching a label
  • kubectl get pods -l env=development
    

  • Now, give a list, where 'development' label is not present
  • kubectl get pods -l env!=development
    

  • Now, if you want to delete pod using labels
  • kubectl delete pod -l env!=development
    kubectl get pods
    

    Label-selector

  • Unlike name/UIDs, labels do not provide uniqueness, as in general, we can expect many objects to carry the same labels.
  • Once labels are attached to an object, we would need filters to narrow down and these are called as label selectors.
  • The API currently supports two types of selectors - Equality based and set based.
  • A label selector can be made multiple requiremnents which are comma-separated.
  • Equality based:
  • (=,!=)
    name: tushar
    class: nodes
    project: development
    

  • Set based:
  • (in, notin and exists)
    
    env in (production, dev)
    env notin (team1,team2)
    

  • Kubernetes also supports set-based selectorrs i.e match multiple values.
  • kubectl get pods -l 'env in (development, testing)'
    
    kubectl get pods -l 'env notin (development, testing)'
    
    kubectl get pods -l class=pods,myname=tushar
    

    Node-Selector

  • One use case for selecting labels is to constrain the set of nodes onto which a pod can schedule i.e you can tell a pod can schedule i.e you can tell a pod to only be able to run a particular nodes.
  • Generally such constraints are unnecessary as the scheduler will automatically do a reasonable placement, but on certain circumstances we might need it.
  • We can use labels to tag nodes.
  • You the nodes are tagged, can use the label selectors to specify the pods run only of specific nodes.
  • first we give label to the node.
  • Then use node-selector to the pod configuration.
  • kind: Pod
    apiVersion: v1
    metadata:
      name: nodelabels
      labels:
        env: development
    spec:
      containers:
        - name: c00
          image: ubuntu
          command:
            [
              "/bin/bash",
              "-c",
              "while true; do echo Hello-Bhupinder; sleep 5 ; done",
            ]
      nodeSelector:
        hardware: t2-medium
    
    kubectl get nodes
    # get your nodes and add label to one of them
    kubectl label nodes nodename hardware=t2-medium
    

    Scaling and Replication

  • Kubernetes was designed to orchestrate multiple containers and replication.
  • Need for multiple containers/replication helps us with these:
    • ###
    • Replication:
      • if we add replicas=2 in config file then it will create two pods of same kind on one node.
      • if one pod fails second pod will continue as a backup pod.

    • Reliability:
      • By having multiple versions of an application, you prevent problems if one or more fails.

    • Load Balancing:
      • Having multiple versions of a container enables you to easily send traffic to different instances to prevent overloading of a single instance or node.

    • Scaling:
      • When load does become too much for the number of existing instances, kubernetes enables you to easily scale up your application, adding additional instances as needed.

    • Rolling updates:
      • Updates to a service by replacing pods one by one.

    Replication Controller

  • A replication controller is a object that enables you to easily create multiple pods, then make sure that number of pods always exist.
  • If a pod created using RC will be automatically replicated if they does crash, failed, or terminated.
  • RC is recommended if you just want to make sure 1 pod always running, even afte system reboots.
  • You can run the RC with 1 replica & the RC will make sure the pod is always running.
  • apiVersion: v1
    kind: ReplicationController # this defines to create the object of replication type
    metadata:
      name: nginx
    spec:
      replicas: 3 # the element defines the desired number of pods
      selector: # tells the controller which pods to watch/belong to this rc
        app: nginx # this must match the labels
      template: # template element defines a template to launch a new pod
        metadata:
          name: nginx
          labels: # selectors values need to match the labels values specified in the pod template
            app: nginx
        spec:
          containers:
            - name: nginx
              image: nginx
              ports:
                - containerPort: 80
            - name: C00
              image: ubuntu
              command: ["bin/bash", "-c", "echo Hi"]
    

    Replica Set

  • Replica Set is a next generation of replication controller.
  • The replication controller only supports equality-based selector whereas the replica set supports set-based selector i.e filtering according to set of values.
  • Replicaset rather than the replication controller is used by other objects like deployment.
  • apiVersion: apps/v1
    kind: ReplicaSet # this defines to create the object of replication type
    metadata:
      name: nginx
    spec:
      replicas: 3 # the element defines the desired number of pods
      selector: # tells the controller which pods to watch/belong to this rc
        matchExpressions:
          - {key: myname,operator: In,values: [Tushar,Tush,Tusha]}
          - {key: env,operator: NotIn,values: [production]}
        app: nginx # this must match the labels
      template: # template element defines a template to launch a new pod
        metadata:
          name: nginx
          labels: # selectors values need to match the labels values specified in the pod template
            app: nginx
            myname: Tush
        spec:
          containers:
          - name: nginx
            image: nginx
            ports:
            - containerPort: 80
          - name: C00
            image: ubuntu
            command: ["bin/bash","-c","echo Hi"]
    
    kubectl apply -f myrs.yml
    kubectl get rs
    kubectl scale --replicas=1 rs/myrs # for scaling down
    

    Did you find this article valuable?

    Support KubeKode Blogs by becoming a sponsor. Any amount is appreciated!

    ย