Sep 23, 2022

k8s Services

A Kubernetes service enables network access to a set of pods. A service listens on a local port (services.spec.ports.port) and forward them to the selector (services.spec.selector) pods at the target port (services.spec.ports.targetPort). Since pods are ephemeral, a service enables a group of pods, which provide specific functions like web service to be assigned a name and unique IP address (clusterIP)

ClusterIP – The service is only accessible from within the Kubernetes cluster.

NodePort – This makes the service accessible on a static port (high port) on each Node in the cluster. Each cluster node opens a port on the node itself and redirects traffic received on that port to the underlying service. 

LoadBalancer – The service becomes accessible externally through a cloud provider's load balancer functionality. If you inspect the load balancer, you will notice that nodes will be instances where the traffic will be redirected at a specific Instance Port, so you should be able to access the application using the node port

Get Instance Port - kubectl get svc <svc name> -o jsonpath="{.spec.ports[0].nodePort}" 

Get Node IP - kubectl get node -o wide

Ingress

Loadbalancer is the default method for many k8s installations in the cloud, but it adds cost and complexity as every service needs to have its own cloud-native load balancer and hence increased the cost. Along with this, you may need to handle SSL for each application, which can be configured at different levels- like application level, load balancer level, etc and also configure firewall rule. This is where an ingress helps. You can consider this as a layer 7 load balancer that is built in a k8s cluster which can be configured as a k8s object using YAML just like any object. Now even with this you still need to expose this to the outside world load balancer (or may be node port), but that's just going to be a single cloud-native load balancer and all the routing will be configured through the ingress controller.

K8s cluster does not come with an ingress controller by default. There is multiple ingress controller available like AWS Load Balancer Controller, GLBC, and Nginx which are currently being supported and maintained by the k8s project. Along with this Isio is also a popular one that provides a lot of service mesh capabilities. An ingress resource is a set of rules and configurations to be applied to the ingress controller.