Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

service selector vs deployment selector matchlabels

Tags:

kubernetes

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

like image 633
Tobias Avatar asked Sep 13 '20 21:09

Tobias


People also ask

What is Matchlabels in deployment?

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.

What is Selector Matchlabels in Kubernetes?

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 is difference between service and deployment in Kubernetes?

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.

What is selector in Kubernetes deployment?

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.


1 Answers

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 :)

like image 182
tpaz1 Avatar answered Oct 14 '22 18:10

tpaz1