The Hamburger of Kubernetes: Service Types
Backend and frontend services should interoperate easily. To do that, Kubernetes
has three
service types:
ClusterIP
, NodePort
, and LoadBalancer
. But which should you use?
ClusterIP
and LoadBalancer
seem self-explanatory, but what would you use
NodePort
for? Even though there are only three options, it can get a little
confusing!
Imagine service types like a hamburger, piled high with your favorite toppings.
You can think of the default service type (ClusterIP
) like the meat (or
veggie) patty. It’s essential, and without it you don’t have either a hamburger
or a service. This service type gives you what you’d think of as “normal”
internal cluster communication by allocating a virtual IP which you can connect
to talk to the backend service.
Now it’s not pleasant to eat a hamburger patty with your just bare hands, you
want something to hold on to. And it’s impossible to access a service
externally using ClusterIP
, which is only available internally. So, let’s get
a bun! I mean, a NodePort
. NodePort
builds on ClusterIP
in that it still
uses the allocated Cluster IP, but it also allocates a fixed port on every
node (hence the name.) So why would you want that? Apart from being able to
access your services externally, say you have an AWS Elastic Load Balancer set
up in front of your cluster and want to connect it to a specific service. If you
use NodePort
, you can forward that ELB’s traffic on to your service by telling
it to connect to every node on the same port.
But you don’t want to eat just a burger plain, and you don’t always want to set
your load balancer up yourself, right? That’s where you’d use the LoadBalancer
type. You can think of this like the condiments and toppings on your hamburger:
there are many flavors and ways to do it, and they make the experience a lot
better. LoadBalancer
will talk whatever flavor of cloud provider you have to
set up a load balancer in front of your service. On AWS it’s an ELB, on Google
Cloud Platform it’s the Load Balancing feature, and there’s probably an
implementation for your cloud provider too, so check the docs. This takes care
of the entire lifecycle of the load balancer, from creating it when you start
your service to destroying it when you destroy your service.
Nifty!
To recap: ClusterIP
is internal only, NodePort
gives you a fixed port on all
your nodes, and LoadBalancer
sets up an external load balancer. Now that you know
what service type to use, take this cheat sheet to remember:
