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.
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.
It is possible for a service to create endpoints for pods that belong to multiple deployments or even out of template pods.
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
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.
selector.matchLabels is what you declare in the yaml file.
spec: selector: matchLables: label: labelvalue
-l is an extra argument you can use with the
Okay, thanks for your explanation @Fernando Jimenez !
Yeah, in a service you only need to declare it under a selector property.
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