Rachel Whitmore:
<#CDR4R9Z7E|ckad-certified-kub-app-dev> Hi Team, I have a question. I’m currently doing a lab. I had to create a nodeport service and then I had to create a deployment. I did these via imperative commands. I had to then link the deployment with the service I created so I was going to go in to edit the label/selector but realised it already had the needed label/selector to link the two. My question is how does kubernetes know that the service I just created should be linked with the deployment without me editing any labels/selectors?
Rob Thorne:
It depends on how you created the service, and the order of creating the service and the deployment.
The service and the deployment are linked through the labels specified in pods that you want linked to the service; they’re at svc.spec.selector. If you use kubectl create service nodeport
you’ll need to edit that field in some way to get it to match your deployment. This is why people tend to use kubectl expose deployment…
instead, since this will automatically fill in that field by looking at the deployment or pod you’re exposing.