/    /  Kubernetes – Labels & Selectors

Kubernetes – Labels & Selectors

 

Labels

Labels are key-value pairs attached to pods, replication controllers, and services. They’re used as identifiers for things like pods and replication controllers. Objects can have them at creation time and at run time.

Selectors

Objects can carry the same labels. Label selectors are core grouping primitives in Kubernetes. They’re used by users to select a set of objects.

Currently, Kubernetes API supports two types of selectors

  • Equality-based selectors
  • Set-based selectors

Equality-based Selectors

Keys and values can be filtered, and matching objects should match all labels.

Set-based Selectors

A set-based selector lets you filter keys based on values.

apiVersion: v1
kind: Service
metadata:
   name: redis-app
spec:
   ports:
      - port: 6379
      name: redis
   type: NodePort
   selector:
      app: myapp ---------> 1
      component: redis -----------> 2

Redis is used as the component and the label & selector are used as the app.

Using the kubectl command, the file will create a service called Redis-app that will communicate on port 6379. The type is NodePort with the new label selector app: myapp  and component: Redis.