Edit This Page

Exposing an External IP Address to Access an Application in a Cluster

This page shows how to create a Kubernetes Service object that exposees an external IP address.


Before you begin

Creating a service for an application running in five pods

  1. Run a Hello World application in your cluster:

     kubectl run hello-world --replicas=5 --labels="run=load-balancer-example" --image=gcr.io/google-samples/node-hello:1.0  --port=8080

    The preceding command creates a Deployment object and an associated ReplicaSet object. The ReplicaSet has five Pods, each of which runs the Hello World application.

  2. Display information about the Deployment:

     kubectl get deployments hello-world
     kubectl describe deployments hello-world
  3. Display information about your ReplicaSet objects:

     kubectl get replicasets
     kubectl describe replicasets
  4. Create a Service object that exposes the deployment:

     kubectl expose deployment hello-world --type=LoadBalancer --name=my-service
  5. Display information about the Service:

     kubectl get services my-service

    The output is similar to this:

     NAME         CLUSTER-IP     EXTERNAL-IP      PORT(S)    AGE
     my-service   8080/TCP   54s

    Note: If the external IP address is shown as , wait for a minute and enter the same command again.

  6. Display detailed information about the Service:

     kubectl describe services my-service

    The output is similar to this:

     Name:           my-service
     Namespace:      default
     Labels:         run=load-balancer-example
     Selector:       run=load-balancer-example
     Type:           LoadBalancer
     LoadBalancer Ingress:
     Port:           <unset> 8080/TCP
     NodePort:       <unset> 32377/TCP
     Endpoints:,, + 2 more...
     Session Affinity:   None

    Make a note of the external IP address exposed by your service. In this example, the external IP address is Also note the value of Port. In this example, the port is 8080.

  7. In the preceding output, you can see that the service has several endpoints:,, + 2 more. These are internal addresses of the pods that are running the Hello World application. To verify these are pod addresses, enter this command:

     kubectl get pods --output=wide

    The output is similar to this:

     NAME                         ...  IP         NODE
     hello-world-2895499144-1jaz9 ...   gke-cluster-1-default-pool-e0b8d269-1afc
     hello-world-2895499144-2e5uh ...    gke-cluster-1-default-pool-e0b8d269-1afc
     hello-world-2895499144-9m4h1 ...   gke-cluster-1-default-pool-e0b8d269-5v7a
     hello-world-2895499144-o4z13 ...   gke-cluster-1-default-pool-e0b8d269-1afc
     hello-world-2895499144-segjf ...   gke-cluster-1-default-pool-e0b8d269-cpuc
  8. Use the external IP address to access the Hello World application:

     curl http://<external-ip>:<port>

    where <external-ip> us the external IP address of your Service, and <port> is the value of Port in your Service description.

    The response to a successful request is a hello message:

     Hello Kubernetes!

Cleaning up

To delete the Service, enter this command:

kubectl delete services my-service

To delete the Deployment, the ReplicaSet, and the Pods that are running the Hello World application, enter this command:

kubectl delete deployment hello-world

What’s next

Learn more about connecting applications with services.


Create Issue Edit This Page