How to Expose Your App Through a Service in Kubernetes

Once you have deployed an application into Kubernetes , it will not be available to the outside world.

For this to be possible, you need to expose it via a Service.

Services can be exposed in 4 different ways:

  1. ClusterIP (default) – Exposes the Service via an internal IP to the cluster (only available internally)
  2. NodePort – Exposes the Service via the same port on all Nodes
  3. LoadBalancer – Creates an external LoadBalancer in the current cloud
  4. ExternalName – Maps the Service to the externalName, and returns a CNAME record with it’s value

How to Expose your App via a Service in Kubernetes

kubectl get pods to list your available pods.

kubectl get services to list your existing Services.

kubectl get deployments to list your active deployments.

Once you have this information, you can run the following command:

kubectl expose deployment/<your-deployment-name> --type="NodePort" --port 8080

This will expose your deployment on port 8080.

Verify that the App has been exposed

We can verify that the deployment has been exposed by running the following:

kubectl get services

This will show the new exposed Service we requested.

$ kubectl get services
NAME                  TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes            ClusterIP        <none>        443/TCP          28s
kubernetes-bootcamp   NodePort   <none>        8080:30062/TCP   3s