Learn about Service in Kubernetes

As I said in the previous tutorial, after creating the Deployment for our application, this application will only be accessible inside the Nodes of the cluster. For the outside of the cluster to be accessible, we need to use the Service object by running the “expose” command.

For example, I have a Deployment object created with the following spec:

Learn about Service in Kubernetes

Now I will expose this Deployment with the “expose” command as follows:

with:

  • node-hello is the name of Deployment in Kubernetes,
  • –type is a Deployment type will be exposed. There are 4 types that we can expose, that is: ClusterIp (the application can only access within the cluster, this is the default type in case we do not pass the –type parameter in the above statement), NodePort (application can access from outside Node via Node’s IP address), LoadBalancer (application can access from outside the cluster through Cloud Provider Endpoint) and ExternalName (if you want to set a domain pointing to the application)
  • — port is the port of the application inside the cluster
  • and –target-port is the port that will be exposed to the outside.

My results after running the above command will be as follows:

Learn about Service in Kubernetes

Since I am using Kubernetes locally, if I request to http://localhost:8080/ on my machine at this time, you will see the following result:

Learn about Service in Kubernetes

So we have already successfully expose the application!

If you now run the command to describe our newly service:

you will see the following result:

Learn about Service in Kubernetes

We can also create the Service object directly from the Pod.

In this case, the Service will use Label and Selector to select the Pod to expose. For example, I have a Pod named “huongdanjava-app” created by spec as follows:

Learn about Service in Kubernetes

Then, I can define the following spec to create a new Service which exposing “huongdanjava-app” Pod to outside:

The result after creating a new Service from this spec file:

Learn about Service in Kubernetes

Go to http://localhost:8080/, you also see the same results as when I exposed from the Deployment object.

We can delete any Service using the kubectl delete statement as follows:

For example, I delete Service named “kubernetes”, the result will be as follows:

Learn about Service in Kubernetes

Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment