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

    • Install kubectl.
    • Use a cloud provider like Google Kubernetes Engine or Amazon Web Services to create a Kubernetes cluster. This tutorial creates an , which requires a cloud provider.
    • Configure to communicate with your Kubernetes API server. For instructions, see the documentation for your cloud provider.
    • Run five instances of a Hello World application.
    • Create a Service object that exposes an external IP address.
    • Use the Service object to access the running application.
    1. Run a Hello World application in your cluster:

      service/load-balancer-example.yaml

      1. kubectl apply -f https://k8s.io/examples/service/load-balancer-example.yaml

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

    2. Display information about the Deployment:

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

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

      1. kubectl expose deployment hello-world --type=LoadBalancer --name=my-service
    5. The output is similar to:

      1. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
      2. my-service LoadBalancer 10.3.245.137 104.198.205.71 8080/TCP 54s

      Note: The type=LoadBalancer service is backed by external cloud providers, which is not covered in this example, please refer to this page for the details.

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

    6. Display detailed information about the Service:

      1. kubectl describe services my-service

      The output is similar to:

      1. Name: my-service
      2. Namespace: default
      3. Selector: app.kubernetes.io/name=load-balancer-example
      4. Type: LoadBalancer
      5. IP: 10.3.245.137
      6. LoadBalancer Ingress: 104.198.205.71
      7. Port: <unset> 8080/TCP
      8. NodePort: <unset> 32377/TCP
      9. Endpoints: 10.0.0.6:8080,10.0.1.6:8080,10.0.1.7:8080 + 2 more...
      10. Session Affinity: None
      11. Events: <none>

      Make a note of the external IP address (LoadBalancer Ingress) exposed by your service. In this example, the external IP address is 104.198.205.71. Also note the value of Port and . In this example, the Port is 8080 and the NodePort is 32377.

      1. kubectl get pods --output=wide

      The output is similar to:

    7. Use the external IP address (LoadBalancer Ingress) to access the Hello World application:

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

      where <external-ip> is the external IP address (LoadBalancer Ingress) of your Service, and <port> is the value of Port in your Service description. If you are using minikube, typing minikube service my-service will automatically open the Hello World application in a browser.

      The response to a successful request is a hello message:

      1. Hello Kubernetes!

    To delete the Service, enter this command:

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