I tried being more explicit when naming labels in a Kubernetes configuration file, thinking that deployments use the pod labels and services use the deployment labels.
apiVersion: apps/v1
kind: Deployment
metadata:
name: auth-depl
labels:
app: auth-depl-label
spec:
replicas: 1
selector:
matchLabels:
app: auth-pod-label
template:
metadata:
labels:
app: auth-pod-label
spec:
containers:
- name: auth-pod
image: bogdan-pechounov/auth
ports:
- containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: auth-srv
spec:
selector:
app: auth-depl-label
type: LoadBalancer
ports:
- protocol: TCP
port: 3000
targetPort: 3000
However, minikube service auth-srv
yields a This site can’t be reached
error in Chrome. In the minikube dashboard
, the service is pending and there are no pods associated with the service.
Using the pod labels instead does work, which makes me wonder what the deployment labels are for.
apiVersion: v1
kind: Service
metadata:
name: auth-srv
spec:
selector:
app: auth-pod-label
type: LoadBalancer
ports:
- protocol: TCP
port: 3000
targetPort: 3000
full code (skaffold dev
to start)
Every label is just a way to refer to a collection of resources without knowing their names. However, as you pointed out, kubernetes itself uses labels in a couple of ways related to your question:
Deployments
use labels for tracking the health and cardinality of their managed Pods (since the names of Pods are mostly random)Services
use labels for tracking the Pods inReady
state to which traffic should be sentWith
Deployments
, one also needs to exercise caution that theDeployment
object itself has labels, but those are not related to thetemplate: { metadata: { labels: {} } }
that are stamped onto newly created Pods and those are the ones which must be a subset of thematchLabels:
for aDeployment
to manage themSame story with the
Service
, which again can have its own labels but are unrelated to the labels it uses forService
membership. Be aware that while it is super common to have the labels inmatchLabels:
be the same as those in theService
'sselector:
, they otherwise have no relationship to one anotherThis is a perfectly fine setup, since the
Deployment
cares only about the Pods beingawesome
and theService
cares only about those Pods beingv1
, and both are fulfilled by newly spawned Pods from the Deployment because of the template's labels applied to them