Kubernetes: Services Types
How your requests are routed to the pod is the question we are going to solve now. Perhaps, instead of “how” maybe we should ask “who” first. Who is actually trying to connect to your services? And since that question would really help us answering the one and only question — what is my service type? Who is it going to serve?
ClusterIP
If your clients are always going to be internal — aka pods inside the kubernetes cluster, then you need to create a service of type ClusterIP. As the official doc states, it exposes the Service on a cluster-internal IP. Choosing this value makes the Service only reachable from within the cluster. This is the default ServiceType
.
NodePort
If your clients are outside of the cluster, then you need to use a NodePort type. As the official doc states, it exposes the Service on each Node’s IP at a static port (the NodePort
). A ClusterIP
Service, to which the NodePort
Service routes, is automatically created. You'll be able to contact the NodePort
Service, from outside the cluster, by requesting <NodeIP>:<NodePort>
.
LoadBalancer
If your clients sending requests from external load balancer sitting in a cloud provider, say AWS, then you need to use LoadBalancer type. This type actually automated the configuration process of setting the configs of the AWS load balancer — aka Kubernetes displaying how it can manage to automatically configure that for you on your cloud! As the official doc states, it exposes the Service externally using a cloud provider’s load balancer. NodePort
and ClusterIP
Services, to which the external load balancer routes, are automatically created. To be precise, the cloud provider load balancer actually sends requests to <NodeIP>:<NodePort>
which was all set automatically.
The above diagram depicts the overall request routing structure. We can see for all the clients above, the other two separate K8 clusters, your laptop, and your cloud load balancer, all the requests are finally being routed from ClusterIP.
Now that we have simplified the the routing structure a bit the only part of the puzzle left is the communication from ClusterIP and the pods — which pod gets the request? 🤔