Hi, I have a question about using label selector in deployment and service. Can . . .

Seung Park:
Hi, I have a question about using label selector in deployment and service. Can Service use matchLabel to connect pods and service?
When creating a deployment, selector.matchLabel is used to create link to pods. In this case, deployment creates pods that match the label. For Service, it looks like all Labels have to match in order to create link between pods and the service.

Fernando Jimenez:
Hi @Seung Park The kubernetes object service needs to match all the labels given in matchLabels. An endpoint will be created for every pod IP that matches all the labels criteria.

Fernando Jimenez:
It is possible for a service to create endpoints for pods that belong to multiple deployments or even out of template pods.

Fernando Jimenez:

When creating a deployment, selector.matchLabel is used to create link to pods. In this case, deployment creates pods that match the label.

Actually, the deployment does not create any pods. The replicaset spec is the kubernetes object that has the job of manteining (keeping the count) of the state of pods running with the labels declared in the selector.matchLabels

Seung Park:
Thanks for the quick response @Fernando Jimenez. That makes sense for Service object. I do have one more question; what is the difference between using selector vs selector.matchLabels? In the CKA lecture videos, only selector was used for creating Service object whereas for creating deployment, selector.matchLabels was used.

Fernando Jimenez:
selector.matchLabels is what you declare in the yaml file.

spec:
  selector:
    matchLables:
      label: labelvalue

--selector or -l is an extra argument you can use with the kubectl client.

Seung Park:
Okay, thanks for your explanation @Fernando Jimenez !

Fernando Jimenez:
Yeah, in a service you only need to declare it under a selector property.

unnivkn:
Hi… always use kubectl expose deployment <deployment-name> Or kubectl expose pod <pod-name> imperative command while creating a service. In this way you don’t need to worry about the Pod or Deployment selector/label. It will create a service with matching labels of Pod or Deployment. More over you can save your time by using imperative commands