concept Kubernetes Service in category microservice

appears as: Kubernetes Service, The Kubernetes Service, Kubernetes Service, A Kubernetes Service
Microservices Security in Action

This is an excerpt from Manning's book Microservices Security in Action.

In this section, we create a Kubernetes Service that exposes the STS outside the Kubernetes cluster. If you’re new to Kubernetes Services, remember to check appendix J.

Here, we use a Kubernetes Service of LoadBalancer type. If there are multiple replicas of a given Pod, the Service of LoadBalancer type acts as a load balancer. Usually, it’s an external load balancer provided by the Kubernetes hosting environment, and in our case, it’s the GKE. Let’s have a look at the YAML file to create the Service (listing 11.2). The same YAML file is available at chapter11/sample01/sts.service.yaml.

When you create a Service in Kubernetes without specifying any type (or setting the type as ClusterIP), Kubernetes creates a Service of ClusterIP type; ClusterIP is the default Service type in Kubernetes. A Service of ClusterIP type is reachable only within a Kubernetes cluster. The following listing shows a sample YAML of a Kubernetes Service of ClusterIP type.

In a typical Kubernetes environment for a given microservice (or a container), we run multiple instances of the same Pod to address scalability requirements; each Pod in a Kubernetes cluster has multiple replicas. This helps Kubernetes distribute the requests coming to a given microservice among all the corresponding Pods, or to do load balancing. Remember, Pods in Kubernetes are short-lived. They can come and go at any time, so even the internal IP address assigned to a Pod can change from time to time. This is one requirement for having a Kubernetes Service (see section J.2.3).

A Kubernetes Service provides an abstraction over a set of Pods that matches the given criteria. You don’t talk to a Pod directly, but always go through a Service. Let’s use the following command to create a Kubernetes Service:

\> kubectl expose deployment hello-server --type LoadBalancer \
--port 80 --target-port 8080

This command creates a Kubernetes Service over all the Pods running in the hello-server Deployment. Here, we create a Service of the type LoadBalancer. Let’s use the following command to discover all the Services running in our Kubernetes cluster (under the default namespace):