concept Service in category microservice

This is an excerpt from Manning's book Microservices Security in Action.
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.
The Service listens on port 443 and forwards the traffic to port 8443. If you look at listing 11.1, you’ll notice that when we create the Deployment, the container that carries the STS microservice is listening on port 8443. Even though it’s not 100% accurate to say that a Service listens on a given port, it’s a good way to simplify what’s happening underneath. As we discussed in appendix J, what really happens when we create a Service is that each node in the Kubernetes cluster updates the corresponding iptables, so any request destined to a Service IP address/name and port will be dispatched to one of the Pods it backs.
Because Pods are ephemeral, the IP address assigned to a Pod can change over time, and at the same time, we can’t exactly predict in an autoscaling environment how many Pods will be running at a given time and what those are. For these reasons, communications between a client application and a Pod, or communications among Pods, should not be using the IP address assigned to a Pod; instead, use a Service. A Service is an abstraction over a set of Pods (see figure J.3). You can create a Kubernetes Service pointing to a set of Pods, and you can think of a Service as a way to route requests to a Pod.
Figure J.3 A Service groups one or more Pods and exposes Pods outside the Kubernetes cluster. Not all the Services are accessible from outside a Kubernetes cluster, however.
![]()
If the Pod IP address changes over time and if a Pod can come and go at any time, how do we bind a Service to a Pod or to a set of Pods? When we create a Service against a set of Pods, we don’t create a static binding between them. We don’t tell the Service that these are the IP addresses of the Pods you need to work with. Instead, we use a label to define a filtering criterion so that Kubernetes can filter out the Pods that have to work with a given Service.
If you look at the sample YAML in listing J.2, you’ll find the label
app:order-processingis assigned to a Pod. A label is a key-value pair (in this example,appis the key, andorder-processingis the value). All the replicas created from this Pod definition will carry the same label. If we want to create a Service pointing to all the replicas of that Pod, we can use the corresponding label as a filter criterion when defining the Service.
This command creates a Kubernetes Service over all the Pods running in the
hello-serverDeployment. 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 thedefaultnamespace):\> kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello-server LoadBalancer 10.39.243.41 35.233.245.242 80:30648/TCP 6h8m kubernetes ClusterIP 10.39.240.1 <none> 443/TCP 8hThe output shows that the
hello-server Service has two IP addresses. When we access a Service from within a Kubernetes cluster, we use the cluster IP. And for accessing a Service from an external client, we use the external IP. Kubernetes can take a few minutes to assign an external IP address to a Service. If you don’t see an IP address assigned to your Service, repeat the command in a few minutes. We can use the followingcurlcommand to test thehello-serverService with its external IP address:\> curl http://35.233.245.242 Hello, world! Version: 1.0.0 Hostname: hello-server-5cdf4854df-q42c4If you run this sample in your local Kubernetes setup, either with Docker Desktop or Minikube, you won’t see an external IP address assigned to it. That is because there is no load balancer associated with your local Kubernetes environment. As you learned in section J.2.3, if we create a Service of NodePort type, we should be able to access it by using the IP address of a node and the nodePort. The following command shows how to create a Service of NodePort type:
\> kubectl expose deployment hello-server --name hello-server-local \ --type NodePort --port 80 --target-port 8080