I understand that services use a selector to identify which pods to route traffic to by thier labels.
apiVersion: v1
kind: Service
metadata:
name: svc
spec:
ports:
- name: tcp
protocol: TCP
port: 443
targetPort: 443
selector:
app: nginx
Thats all well and good.
Now what is the difference between this selector and the one of the spec.selector
from the deployment. I understand that it is used so that the deployment can match and manage its pods.
I dont understand however why i need the extra matchLabels
declaration and cant just do it like in the service. Whats the use of this semantically?
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
Thanks in advance
matchLabel: tells what pods the deployment will apply to. So, in your yaml description file for the deployment, it might look something like this: kind: Deployment. ... metadata: name: nginx.
Matchlabels are a type of key-value pair map. A single key value pair in the matchLabels map corresponds to an element of matchExpressions with the key field “key,” the operator “In,” and only “value” in the values array. A collection of pod selector requirements is called matchExpressions.
What's the difference between a Service and a Deployment in Kubernetes? A deployment is responsible for keeping a set of pods running. A service is responsible for enabling network access to a set of pods. We could use a deployment without a service to keep a set of identical pods running in the Kubernetes cluster.
The label selector is the core grouping primitive in Kubernetes. The API currently supports two types of selectors: equality-based and set-based. A label selector can be made of multiple requirements which are comma-separated.
Simple as that - in the service spec.selector
you can identify which pods to route traffic to only by their labels.
On the other hand, in the Deployment
spec.selector
you have two options to decide on which node the pods will be scheduled on, which are: matchExpressions
, matchLabels
.
let me know if it answers your question :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With