Skip to main content

Kubernetes: 8. Labels & Selectors

Labels

  • Labels are a way of grouping the objects
  • While Kubernetes understands the objects it create, it is easier to identify the objects by using custom labels
  • With labels you group the objects by types (Pods, Services, ReplicaSet etc) or by Applications
  • For a pod, labels are defined under the metadata section
Selectors
  • Selectors are used to filter the objects using labels defined on them
  • Using kubectl and selector pods can be listed by filtering on the labels attached to them
  • If a Selector has multiple labels, they are understood as logical AND, which means pods must match all labels.
pod-definition.yaml
apiVersion: v1
kind: Pod
metadata:
    name: myapp-pod
    labels:
        app: myapp
        location: IN

spec:
    containers:
    - name: nginx-container
      image: nginx

kubectl get pods --selector app=myapp 
-> Get pods filtered by labels

kubectl get pods --selector app=myapp,tier=frontend 
-> Get pods filtered by labels app=myapp and tier=frontend

Replica Set
  • Kubernetes also uses labels to build objects.
  • For example take a case of replica set, 
    • Labels under metadata are replicaSet labels
    • labels under template.metadata are pod labels
    • labels under selector.matchLabels are selector labels
  • Kubernetes uses the selector.matchLabels to identify which pods have to be created as defined in spec.template by matching it with spec.template.metadata.labels

replica-set-definition.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
    name: myapp-rs
    labels:
        app: myapp
        type: front-end

spec:
    template:
        metadata:
            name: myapp-pod
            labels:
                app: myapp
                type: front-end
            spec:
                containers:
                - name: nginx-container
                  image: nginx


    replicas: 3
    selector:
        matchLabels:
            type: front-end

Service
  • For a Service object Kubernetes matches the labels defined under spec.selector to metadata.labels as defined under pod-definition file
apiVersion: v1
kind: Service
metadata:
    name: myapp-rs
    labels:
        app: my-svc-app

spec:
    selector:
        app: myapp
    ports:
    - protocol: TCP
      port: 8080
      targetPort: 9376

Annotations
  • Annotations though sound similar to labels are to be used to store the custom information specific to object
  • With labels, you can use them to group objects or filter them with selectors
  • Annotations are custom information about the object
pod-definition.yaml
apiVersion: v1
kind: Pod
metadata:
    name: myapp-pod
    labels:
        app: myapp
        location: IN
    annotations:
        buildVersion: 1.34
        contact: am@memories.com

spec:
    containers:
    - name: nginx-container
      image: nginx

Comments

Popular posts from this blog

Kubernetes: 15. Multiple Schedulers

Custom Scheduler Kubernetes allows to create custom schedulers There can be multiple schedulers running at a same time apart from the default scheduler or A custom scheduler can replace the default kube-scheduler to become the default one So a few pods that requires additional checks apart from taints and toleration, node affinity can go through the custom scheduler before getting scheduled on the node Whereas the rest of the pods can go through the default kube-scheduler Create Custom Scheduler We can either download the kube-scheduler and run it as a service or alternatively create it using a static pod Below here we are downloading the binaries to run it The property scheduler-name is used to define the name of the scheduler, if not set then it will be defaulted to default-scheduler For your custom schedulers, update this property name to set a custom name for your scheduler For Static pods, the name can be updated directly in the pod-definition file Use kubectl create -f <pod-de

Kubernetes: 5. Services

A  service  is a stable endpoint to connect to "something" An abstract way to expose an application running on a set of pods as a network service. Services enable communication between various components within and outside of the application With Kubernetes Services there is no need to configure the application for service discovery Kubernetes Service is an object just like Pod, ReplicaSet etc There is always a service running when Kubernetes is installed, Kubernetes API itself When a service is created, kubernetes creates the endpoints (kubectl get endpoints) The endpoints has all the pods associated with that service Headless Service A headless service is obtained by setting clusterIP  field to None Since there is no virtual IP address, there is no load balancer either The DNS service will return the pods' IP addresses as multiple A records This gives us an easy way to discover all the replicas for a deployment This is useful for creating stateful services like DB, Elas