Up until now, our app has been fully isolated in our test environment. With real programs, you’ll almost always need to have your app send web requests to other outside web apps.
To do this, we’ll use the expose command which will create a new service instance with the same name as our deployment and will automatically define the port configuration to allow a connection. As part of the command, we define which port the service should listen on. In this case, we’ll use port 8080.
To try this yourself, enter:
kubectl expose deployment hello-node --type=LoadBalancer --port=8080
To see this service in action, we can once again enter:
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-node LoadBalancer 10.108.188.234 localhost 8080:32505/TCP 7s
kubernetes ClusterIP 10.96.0.1
Here we see that where we once had only one service, we now have two, the original kubernetes and new service hello-node. Notice how the latter differs in both the EXTERNAL-IP and the PORT(S) field due to it being public. NodePorts are the published IP addresses for external users to access the services.
Now that our app is exposed, we can access the web application running inside the hello-node pod to print a message.
To do this, enter:
Hello World!
And like that, we’ve called an external IP to execute our pod and had it print!