Expose our app

As mentioned in the previous chapter, Pods are by default not visible from outside the cluster. This can be changed by adding a Service to the picture, as Services allow applications to receive traffic.

Lets first verify our Pod from Lab 2 is still up and running by

kubectl get pods

Still there? Great! Lets see if we already have services in our cluster by

kubectl get services

We can see the Service kubernetes which was automatically created by Docker Desktop.

Lets create our own Service and expose it to external traffic so we are able to check on our app:

kubectl expose deployment/hello-world --type="NodePort" --port 3000

Let’s check in again on our services:

kubectl get services

You should receive something similar to:

Services

We will find another service called hello-world. Copy the port after the colon of your Service (in the example above port 32590) and check your browser again with:

http://localhost:<port from your terminal>

Great - we can now reach our application from the outside. 🎉

But it is still a bit unhandy to check for the right port in our terminal, right? Wouldn’t it be easier to be able to just use localhost:3000? Let’s go for it :)

We first need to delete our current service via

kubectl delete service hello-world

Then let’s add a new service, but this time not with type NodePort, but LoadBalancer:

kubectl expose deployment/hello-world --type="LoadBalancer" --port 3000

Lets check the browser on http://localhost:3000.

Awesome, it worked 🎉

Don’t close the browser yet, as we will need it later again :)