2016-10-15 22:04:23 +00:00
|
|
|
---
|
2016-12-15 20:16:54 +00:00
|
|
|
title: Exposing an External IP Address to Access an Application in a Cluster
|
2018-05-05 16:00:51 +00:00
|
|
|
content_template: templates/tutorial
|
2018-05-15 22:29:27 +00:00
|
|
|
weight: 10
|
2016-10-15 22:04:23 +00:00
|
|
|
---
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture overview %}}
|
2016-10-15 22:04:23 +00:00
|
|
|
|
2016-12-22 09:14:39 +00:00
|
|
|
This page shows how to create a Kubernetes Service object that exposes an
|
2016-10-15 22:04:23 +00:00
|
|
|
external IP address.
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture prerequisites %}}
|
2016-10-15 22:04:23 +00:00
|
|
|
|
2017-07-10 22:13:19 +00:00
|
|
|
* Install [kubectl](/docs/tasks/tools/install-kubectl/).
|
2016-12-20 22:11:35 +00:00
|
|
|
|
2017-11-13 20:02:31 +00:00
|
|
|
* Use a cloud provider like Google Kubernetes Engine or Amazon Web Services to
|
2016-12-20 22:11:35 +00:00
|
|
|
create a Kubernetes cluster. This tutorial creates an
|
2017-04-19 17:56:47 +00:00
|
|
|
[external load balancer](/docs/tasks/access-application-cluster/create-external-load-balancer/),
|
2016-12-20 22:11:35 +00:00
|
|
|
which requires a cloud provider.
|
|
|
|
|
|
|
|
* Configure `kubectl` to communicate with your Kubernetes API server. For
|
|
|
|
instructions, see the documentation for your cloud provider.
|
2016-10-15 22:04:23 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture objectives %}}
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
* 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.
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture lessoncontent %}}
|
2016-10-15 22:04:23 +00:00
|
|
|
|
2017-01-18 18:18:37 +00:00
|
|
|
## Creating a service for an application running in five pods
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
1. Run a Hello World application in your cluster:
|
|
|
|
|
2018-05-10 18:00:53 +00:00
|
|
|
kubectl run hello-world --replicas=5 --labels="run=load-balancer-example" --image=gcr.io/google-samples/node-hello:1.0 --port=8080
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
The preceding command creates a
|
2017-04-19 17:56:47 +00:00
|
|
|
[Deployment](/docs/concepts/workloads/controllers/deployment/)
|
2016-10-15 22:04:23 +00:00
|
|
|
object and an associated
|
2017-04-19 17:56:47 +00:00
|
|
|
[ReplicaSet](/docs/concepts/workloads/controllers/replicaset/)
|
2016-10-15 22:04:23 +00:00
|
|
|
object. The ReplicaSet has five
|
2017-04-19 17:56:47 +00:00
|
|
|
[Pods](/docs/concepts/workloads/pods/pod/),
|
2016-10-15 22:04:23 +00:00
|
|
|
each of which runs the Hello World application.
|
|
|
|
|
|
|
|
1. Display information about the Deployment:
|
|
|
|
|
2018-05-10 18:00:53 +00:00
|
|
|
kubectl get deployments hello-world
|
|
|
|
kubectl describe deployments hello-world
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
1. Display information about your ReplicaSet objects:
|
|
|
|
|
2018-05-10 18:00:53 +00:00
|
|
|
kubectl get replicasets
|
|
|
|
kubectl describe replicasets
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
1. Create a Service object that exposes the deployment:
|
|
|
|
|
2018-05-10 18:00:53 +00:00
|
|
|
kubectl expose deployment hello-world --type=LoadBalancer --name=my-service
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
1. Display information about the Service:
|
|
|
|
|
2018-05-10 18:00:53 +00:00
|
|
|
kubectl get services my-service
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
The output is similar to this:
|
|
|
|
|
|
|
|
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
|
|
|
|
my-service 10.3.245.137 104.198.205.71 8080/TCP 54s
|
|
|
|
|
2016-12-06 09:12:23 +00:00
|
|
|
Note: If the external IP address is shown as \<pending\>, wait for a minute
|
2016-10-15 22:04:23 +00:00
|
|
|
and enter the same command again.
|
|
|
|
|
|
|
|
1. Display detailed information about the Service:
|
|
|
|
|
2018-05-10 18:00:53 +00:00
|
|
|
kubectl describe services my-service
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
The output is similar to this:
|
|
|
|
|
|
|
|
Name: my-service
|
|
|
|
Namespace: default
|
|
|
|
Labels: run=load-balancer-example
|
2017-09-12 03:07:16 +00:00
|
|
|
Annotations: <none>
|
2016-10-15 22:04:23 +00:00
|
|
|
Selector: run=load-balancer-example
|
|
|
|
Type: LoadBalancer
|
|
|
|
IP: 10.3.245.137
|
|
|
|
LoadBalancer Ingress: 104.198.205.71
|
|
|
|
Port: <unset> 8080/TCP
|
|
|
|
NodePort: <unset> 32377/TCP
|
|
|
|
Endpoints: 10.0.0.6:8080,10.0.1.6:8080,10.0.1.7:8080 + 2 more...
|
|
|
|
Session Affinity: None
|
2017-09-12 03:07:16 +00:00
|
|
|
Events: <none>
|
2016-10-15 22:04:23 +00:00
|
|
|
|
2017-12-25 03:58:07 +00:00
|
|
|
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.
|
2018-03-27 22:03:04 +00:00
|
|
|
Also note the value of `Port` and `NodePort`. In this example, the `Port`
|
2017-12-25 03:58:07 +00:00
|
|
|
is 8080 and the `NodePort` is 32377.
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
1. In the preceding output, you can see that the service has several endpoints:
|
|
|
|
10.0.0.6:8080,10.0.1.6:8080,10.0.1.7:8080 + 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:
|
|
|
|
|
2017-08-06 04:22:56 +00:00
|
|
|
kubectl get pods --output=wide
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
The output is similar to this:
|
|
|
|
|
|
|
|
NAME ... IP NODE
|
|
|
|
hello-world-2895499144-1jaz9 ... 10.0.1.6 gke-cluster-1-default-pool-e0b8d269-1afc
|
2017-08-05 18:54:35 +00:00
|
|
|
hello-world-2895499144-2e5uh ... 10.0.1.8 gke-cluster-1-default-pool-e0b8d269-1afc
|
2016-10-15 22:04:23 +00:00
|
|
|
hello-world-2895499144-9m4h1 ... 10.0.0.6 gke-cluster-1-default-pool-e0b8d269-5v7a
|
|
|
|
hello-world-2895499144-o4z13 ... 10.0.1.7 gke-cluster-1-default-pool-e0b8d269-1afc
|
|
|
|
hello-world-2895499144-segjf ... 10.0.2.5 gke-cluster-1-default-pool-e0b8d269-cpuc
|
|
|
|
|
2017-12-25 03:58:07 +00:00
|
|
|
1. Use the external IP address (`LoadBalancer Ingress`) to access the Hello
|
|
|
|
World application:
|
2016-10-15 22:04:23 +00:00
|
|
|
|
2018-05-10 18:00:53 +00:00
|
|
|
curl http://<external-ip>:<port>
|
2016-10-15 22:04:23 +00:00
|
|
|
|
2017-12-25 03:58:07 +00:00
|
|
|
where `<external-ip>` is the external IP address (`LoadBalancer Ingress`)
|
2018-04-17 05:13:58 +00:00
|
|
|
of your Service, and `<port>` is the value of `Port` in your Service
|
2017-12-25 03:58:07 +00:00
|
|
|
description.
|
|
|
|
If you are using minikube, typing `minikube service my-service` will
|
|
|
|
automatically open the Hello World application in a browser.
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
The response to a successful request is a hello message:
|
|
|
|
|
|
|
|
Hello Kubernetes!
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture cleanup %}}
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
To delete the Service, enter this command:
|
|
|
|
|
2018-05-10 18:00:53 +00:00
|
|
|
kubectl delete services my-service
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
To delete the Deployment, the ReplicaSet, and the Pods that are running
|
|
|
|
the Hello World application, enter this command:
|
|
|
|
|
2018-05-10 18:00:53 +00:00
|
|
|
kubectl delete deployment hello-world
|
2016-10-15 22:04:23 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture whatsnext %}}
|
2016-10-15 22:04:23 +00:00
|
|
|
|
|
|
|
Learn more about
|
2017-04-19 17:56:47 +00:00
|
|
|
[connecting applications with services](/docs/concepts/services-networking/connect-applications-service/).
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|