0
0
Kubernetesdevops~5 mins

Service selectors and labels in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run multiple parts of an app in Kubernetes, you need a way to connect them. Service selectors and labels help services find the right pods to send traffic to, like a mailman delivering letters to the right houses.
When you want a service to send traffic only to specific pods running your app.
When you have multiple versions of an app running and want to route traffic to one version.
When you want to organize pods by roles or features and connect services accordingly.
When you need to update or scale parts of your app without affecting others.
When you want to monitor or manage groups of pods easily by their labels.
Config File - service.yaml
service.yaml
apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector:
    app: my-app
    tier: frontend
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: ClusterIP
---
apiVersion: v1
kind: Pod
metadata:
  name: my-app-pod
  labels:
    app: my-app
    tier: frontend
spec:
  containers:
    - name: my-app-container
      image: nginx:1.23
      ports:
        - containerPort: 8080

This file creates two resources:

  • Service: Named my-app-service, it uses a selector to find pods with labels app: my-app and tier: frontend. It listens on port 80 and forwards traffic to port 8080 on matching pods.
  • Pod: Named my-app-pod, it has labels app: my-app and tier: frontend so the service can select it. It runs an nginx container exposing port 8080.
Commands
This command creates the service and pod defined in the YAML file. It sets up the connection between the service and the pod using labels and selectors.
Terminal
kubectl apply -f service.yaml
Expected OutputExpected
service/my-app-service created pod/my-app-pod created
This command lists all pods with their labels so you can verify the pod has the correct labels for the service selector.
Terminal
kubectl get pods --show-labels
Expected OutputExpected
NAME READY STATUS RESTARTS AGE LABELS my-app-pod 1/1 Running 0 10s app=my-app,tier=frontend
--show-labels - Displays the labels assigned to each pod
This command shows the service details, confirming it is created and ready to route traffic to pods matching its selector.
Terminal
kubectl get service my-app-service
Expected OutputExpected
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-app-service ClusterIP 10.96.0.1 <none> 80/TCP 15s
This command gives detailed information about the service, including the selector labels and endpoints (pods) it routes to.
Terminal
kubectl describe service my-app-service
Expected OutputExpected
Name: my-app-service Namespace: default Labels: <none> Annotations: <none> Selector: app=my-app,tier=frontend Type: ClusterIP IP: 10.96.0.1 Port: 80/TCP TargetPort: 8080/TCP Endpoints: 10.244.0.5:8080 Session Affinity: None Events: <none>
Key Concept

If you remember nothing else from this pattern, remember: services use selectors to find pods by their labels and route traffic only to those matching pods.

Common Mistakes
Not adding matching labels to pods that the service selector expects.
The service will not find any pods to send traffic to, so your app won't receive requests.
Always add the exact labels to pods that match the service selector keys and values.
Using different label keys or values in the service selector and pod labels.
Selectors must exactly match labels; any difference means no pods are selected.
Double-check that selector keys and values exactly match the pod labels.
Forgetting to expose the correct container port that the service targets.
The service forwards traffic to a target port; if the pod container does not expose it, traffic fails.
Ensure the containerPort in the pod matches the targetPort in the service.
Summary
Create pods with labels that describe their role or version.
Create services with selectors that match those pod labels to route traffic correctly.
Use kubectl commands to apply configs and verify pods and services are connected.