Below is a Yaml file for a deployment with three replicas of the "nginx" pod:
To access the "nginx" pods we have the below service ( Yaml file):
The selector (app: nginx) of the service matches the label of the deployment above (app: nginx).
That is is how the service "attaches" itself to the deployment.
We can create the service using the below command:
The service's selector "attaches" itself to the pod's labels ( through the deployment ), that way the service knows which pods it needs to direct its requests to.
The service doesn't have an external IP, only an internal IP which makes it accessible only from inside the kubernetes cluster.
Below, we can see the details of the service "service_1":
An endpoint in created automatically for each pod that has a label that matches the service's selector.
Above we can see one of the three endpoints.
We can check the endpoint's IP address of the service against one of the pod's IP address, to see if the service points to one of the three pods:
The name of the pod starts with "nginx-deployment" because it is managed by our "nginx-deployment" deployment mentioned at the beginning.
The IP address "172.16.0.2" of the pod matches the IP address of one of our endpoints.