Sobes.tech
Junior

What are labels?

sobes.tech AI

Answer from AI

Labels in Kubernetes are key/value pairs that are attached to objects (such as Pods, Services, Deployments). They are used for organizing and selecting subsets of objects. Labels do not carry semantic meaning for Kubernetes itself, but are important for users to identify and group objects.

Examples of usage:

  • Defining environment: environment: production, environment: development
  • Identifying an application: app: my-web-app
  • Defining version: version: v1.2.3
  • Identifying service level: tier: frontend, tier: backend

Label Selectors are used in API queries to select groups of objects based on their labels. For example, a Service can use a label selector to route traffic only to Pods with certain labels.

Examples of selectors:

  • environment=production: Selects all objects where the label environment has the value production.
  • app in (my-web-app, another-app): Selects objects where the label app has the value my-web-app or another-app.
  • !environment: Selects objects that do not have the label environment.
apiVersion: v1
kind: Pod
metadata:
  name: my-app-pod
  labels: # Labels applied to the Pod
    app: my-app
    tier: frontend
    environment: development
spec:
  containers:
  - name: my-app-container
    image: my-app-image:latest
apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector: # Label selector for choosing Pods
    app: my-app
    tier: frontend
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080