Pods can be deleted and recreated at any time, making them a so-called nonpermanent resource in Kubernetes. Therefore, the IP address of a pod will likely change regularly. This behavior will cause problems when you have a resource, for example, a DNS record, pointing to your application. It will be impossible to route to the application since the IP address of the pods will always change.
This is why Kubernetes introduced the Service resource.
A Service provides a permanent endpoint for your application. This means that its IP address never changes. Therefore, you can point a DNS record or another application to the Service. The Service then redirects the traffic to a pod of the associated application. The Service can access the pods because it uses selectors and not an IP address.
Let’s have a look at a simple Service definition:
This service publishes port 80 and redirects the request to port 8080 of a pod with the label “app=my-application” The targetPort property is optional. The Service redirects the request to the same value as port if targetPort is not set.
Services can expose multiple ports, for example, one for HTTP and one for HTTPS. When using more than one port, you have to provide a name for each port, as shown in the following example:
Currently, there are four service types in Kubernetes:
ClusterIP is the default type of a Kubernetes service. This type exposes the service on a Kubernetes internal IP Address and port. This means that only applications inside the Kubernetes cluster can access this service.
This type exposes the service externally using a cloud provider’s load balancer. For example, if you use the LoadBalancer Service in an Azure Kubernetes Service cluster, Azure will create a load balancer in Azure and route the requests to a NodePort and ClusterIP Service. These services are created automatically.
NodePort is a service type that I have only used in on-premises clusters. This service type exposes the service to the outside and also creates a ClusterIP Service for internal requests. The default range of available ports for NodePort is 30000-32767. This property can be configured by using the –service-node-port-range flag.
This type maps the service to external fields like (foo.bar.com) and returns a CNAME record. I have never used this service, therefore I can’t talk much about it. All I know is that you need at least kube-dns version 1.7 or CoreDNS 0.0.8 or higher to use the type ExternalName.
This post gave a very simple introduction to Kubernetes Services and the four available types. For more details about Services, see the official Kubernetes documentation.
If you want to get started with Kubernetes, I would recommend you to use a managed service like Azure Kubernetes Service (AKS).