Sequential Breakdown of the Process
Learn the sequential processes kicked off by a Service creation.
We'll cover the following
The sequence#
The processes that were initiated with the creation of the Service are as follows:
-
Kubernetes client (
kubectl
) sent a request to the API server requesting the creation of the Service based on Pods created through the go-demo-2 ReplicaSet. -
Endpoint controller is watching the API server for new service events. It detected that there is a new Service object.
-
Endpoint controller created endpoint objects with the same name as the Service, and it used Service selector to identify endpoints (in this case the IP and the port of go-demo-2 Pods).
-
kube-proxy is watching for service and endpoint objects. It detected that there is a new Service and a new endpoint object.
-
kube-proxy added iptables rules which capture traffic to the Service port and redirect it to endpoints. For each endpoint object, it adds iptables rule which selects a Pod.
-
The kube-dns add-on is watching for Service. It detected that there is a new service.
-
The kube-dns added
db
's record to the dns server (skydns).
The sequence we described is useful when we want to understand everything that happened in the cluster from the moment we requested the creation of a new Service. However, it might be too confusing so we’ll try to explain the same process through a diagram that more closely represents the cluster.
Let’s take a look at our new Service.
-
Line 1-2: We can see the name and the namespace. We did not yet explore namespaces (coming up later) and, since we didn’t specify any, it is set to
default
. -
Line 6: The selector matches the one from the ReplicaSet. The Service is not directly associated with the ReplicaSet (or any other controller) but with Pods through matching labels.
-
Line 9-13: Next is the
NodePort
type which exposes ports to all the nodes. SinceNodePort
automatically createdClusterIP
type as well, all the Pods in the cluster can access theTargetPort
. ThePort
is set to28017
. That is the port that the Pods can use to access the Service. Since we did not specify it explicitly when we executed the command, its value is the same as the value of theTargetPort
, which is the port of the associated Pod that will receive all the requests.NodePort
was generated automatically since we did not set it explicitly. It is the port which we can use to access the Service and, therefore, the Pods from outside the cluster. In most cases, it should be randomly generated, that way we avoid any clashes.
Let’s see whether the Service indeed works.
After running the above command click on the link beside the run button to see the UI of mongodb opened in the browser. Make sure that the pods are ready before running the this command.
As it has been already mentioned in the previous chapters, creating Kubernetes objects using imperative commands is not a good idea unless we’re trying some quick hack.
The same applies to Services. Even though kubectl expose
did the work, we should try to use a documented approach through YAML files. In that spirit, we’ll destroy the service we created and start over.
Try it yourself#
A list of commands used in this lesson are also provided below.
You can practice the commands in the following code playground by pressing the Run button and waiting for the cluster and the appropriate services to set up.
/
- go-demo-2-rs.yml