Kubernetes and Cloud Native Associate (KCNA)Kubernetes FundamentalsMedium
A cluster operator is troubleshooting network issues with a Pod that is part of a Deployment. The Pod's containers are running, but other Pods cannot reach it by its Service name. The operator suspects an issue with the Pod's labels not matching the Service's selector. Which section of the Pod's YAML manifest is responsible for defining the labels that a Service uses to identify its target Pods?
- A`spec.selector`
- B`metadata.labels`
- C`spec.containers.labels`
- D`spec.template.metadata.labels`
Show answer & explanationAnswer & explanation
Correct answer: B. `metadata.labels`
The `metadata.labels` field directly under the top-level `metadata` section of a Pod defines the key-value pairs that a Service's `selector` uses to identify which Pods it should route traffic to.
Why the other options are wrong
- A. `spec.selector` is used in Service and Deployment objects to select Pods, not within a Pod to define its own labels.
- C. There is no `spec.containers.labels` field; labels apply to the Pod itself, not individual containers.
- D. `spec.template.metadata.labels` is for Pods created by a Deployment or ReplicaSet, but for a standalone Pod, it's just `metadata.labels`.
Pod `metadata.labels`
A field within a Kubernetes Pod's `metadata` section that defines a set of key-value pairs. These labels are used by Services and other controllers to identify and select specific Pods.
- Used for identifying and grouping Kubernetes objects.
- Crucial for Service-to-Pod mapping via selectors.
- Part of `metadata` at the top level of the resource.
Memory trick: Metadata Makes Labels Link.