2016-12-12 19:31:31 +00:00
|
|
|
|
---
|
2017-06-08 20:29:52 +00:00
|
|
|
|
title: Connect a Front End to a Back End Using a Service
|
2018-05-05 16:00:51 +00:00
|
|
|
|
content_template: templates/tutorial
|
2018-05-20 03:40:51 +00:00
|
|
|
|
weight: 70
|
2016-12-12 19:31:31 +00:00
|
|
|
|
---
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
|
{{% capture overview %}}
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
2017-05-04 23:48:27 +00:00
|
|
|
|
This task shows how to create a frontend and a backend
|
2016-12-12 19:31:31 +00:00
|
|
|
|
microservice. The backend microservice is a hello greeter. The
|
|
|
|
|
frontend and backend are connected using a Kubernetes Service object.
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
|
{{% /capture %}}
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
|
{{% capture objectives %}}
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
|
|
|
|
* Create and run a microservice using a Deployment object.
|
|
|
|
|
* Route traffic to the backend using a frontend.
|
|
|
|
|
* Use a Service object to connect the frontend application to the
|
|
|
|
|
backend application.
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
|
{{% /capture %}}
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
|
{{% capture prerequisites %}}
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
|
* {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
2017-05-04 23:48:27 +00:00
|
|
|
|
* This task uses
|
2017-04-19 17:56:47 +00:00
|
|
|
|
[Services with external load balancers](/docs/tasks/access-application-cluster/create-external-load-balancer/), which
|
2016-12-12 19:31:31 +00:00
|
|
|
|
require a supported environment. If your environment does not
|
|
|
|
|
support this, you can use a Service of type
|
2017-09-24 18:06:23 +00:00
|
|
|
|
[NodePort](/docs/concepts/services-networking/service/#type-nodeport) instead.
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
|
{{% /capture %}}
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
|
{{% capture lessoncontent %}}
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
|
|
|
|
### Creating the backend using a Deployment
|
|
|
|
|
|
|
|
|
|
The backend is a simple hello greeter microservice. Here is the configuration
|
|
|
|
|
file for the backend Deployment:
|
|
|
|
|
|
2018-07-04 06:19:23 +00:00
|
|
|
|
{{< codenew file="service/access/hello.yaml" >}}
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
|
|
|
|
Create the backend Deployment:
|
|
|
|
|
|
|
|
|
|
```
|
2018-07-04 06:19:23 +00:00
|
|
|
|
kubectl create -f https://k8s.io/examples/service/access/hello.yaml
|
2016-12-12 19:31:31 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
View information about the backend Deployment:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
kubectl describe deployment hello
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The output is similar to this:
|
|
|
|
|
|
|
|
|
|
```
|
2017-03-23 07:10:00 +00:00
|
|
|
|
Name: hello
|
|
|
|
|
Namespace: default
|
|
|
|
|
CreationTimestamp: Mon, 24 Oct 2016 14:21:02 -0700
|
|
|
|
|
Labels: app=hello
|
|
|
|
|
tier=backend
|
|
|
|
|
track=stable
|
2017-08-28 03:07:42 +00:00
|
|
|
|
Annotations: deployment.kubernetes.io/revision=1
|
2017-03-23 07:10:00 +00:00
|
|
|
|
Selector: app=hello,tier=backend,track=stable
|
2017-08-28 03:07:42 +00:00
|
|
|
|
Replicas: 7 desired | 7 updated | 7 total | 7 available | 0 unavailable
|
2017-03-23 07:10:00 +00:00
|
|
|
|
StrategyType: RollingUpdate
|
|
|
|
|
MinReadySeconds: 0
|
|
|
|
|
RollingUpdateStrategy: 1 max unavailable, 1 max surge
|
2017-08-28 03:07:42 +00:00
|
|
|
|
Pod Template:
|
|
|
|
|
Labels: app=hello
|
|
|
|
|
tier=backend
|
|
|
|
|
track=stable
|
|
|
|
|
Containers:
|
|
|
|
|
hello:
|
|
|
|
|
Image: "gcr.io/google-samples/hello-go-gke:1.0"
|
|
|
|
|
Port: 80/TCP
|
|
|
|
|
Environment: <none>
|
|
|
|
|
Mounts: <none>
|
|
|
|
|
Volumes: <none>
|
|
|
|
|
Conditions:
|
|
|
|
|
Type Status Reason
|
|
|
|
|
---- ------ ------
|
|
|
|
|
Available True MinimumReplicasAvailable
|
|
|
|
|
Progressing True NewReplicaSetAvailable
|
2017-03-23 07:10:00 +00:00
|
|
|
|
OldReplicaSets: <none>
|
|
|
|
|
NewReplicaSet: hello-3621623197 (7/7 replicas created)
|
2016-12-12 19:31:31 +00:00
|
|
|
|
Events:
|
|
|
|
|
...
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Creating the backend Service object
|
|
|
|
|
|
|
|
|
|
The key to connecting a frontend to a backend is the backend
|
|
|
|
|
Service. A Service creates a persistent IP address and DNS name entry
|
|
|
|
|
so that the backend microservice can always be reached. A Service uses
|
|
|
|
|
selector labels to find the Pods that it routes traffic to.
|
|
|
|
|
|
|
|
|
|
First, explore the Service configuration file:
|
|
|
|
|
|
2018-07-04 06:19:23 +00:00
|
|
|
|
{{< codenew file="service/access/hello-service.yaml" >}}
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
|
|
|
|
In the configuration file, you can see that the Service routes traffic to Pods
|
|
|
|
|
that have the labels `app: hello` and `tier: backend`.
|
|
|
|
|
|
|
|
|
|
Create the `hello` Service:
|
|
|
|
|
|
|
|
|
|
```
|
2018-07-04 06:19:23 +00:00
|
|
|
|
kubectl create -f https://k8s.io/examples/service/access/hello-service.yaml
|
2016-12-12 19:31:31 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
At this point, you have a backend Deployment running, and you have a
|
|
|
|
|
Service that can route traffic to it.
|
|
|
|
|
|
|
|
|
|
### Creating the frontend
|
|
|
|
|
|
|
|
|
|
Now that you have your backend, you can create a frontend that connects to the backend.
|
|
|
|
|
The frontend connects to the backend worker Pods by using the DNS name
|
|
|
|
|
given to the backend Service. The DNS name is "hello", which is the value
|
|
|
|
|
of the `name` field in the preceding Service configuration file.
|
|
|
|
|
|
|
|
|
|
The Pods in the frontend Deployment run an nginx image that is configured
|
|
|
|
|
to find the hello backend Service. Here is the nginx configuration file:
|
|
|
|
|
|
2018-07-04 06:19:23 +00:00
|
|
|
|
{{< codenew file="service/access/frontend.conf" >}}
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
|
|
|
|
Similar to the backend, the frontend has a Deployment and a Service. The
|
|
|
|
|
configuration for the Service has `type: LoadBalancer`, which means that
|
|
|
|
|
the Service uses the default load balancer of your cloud provider.
|
|
|
|
|
|
2018-07-04 06:19:23 +00:00
|
|
|
|
{{< codenew file="service/access/frontend.yaml" >}}
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
|
|
|
|
Create the frontend Deployment and Service:
|
|
|
|
|
|
|
|
|
|
```
|
2018-07-04 06:19:23 +00:00
|
|
|
|
kubectl create -f https://k8s.io/examples/service/access/frontend.yaml
|
2016-12-12 19:31:31 +00:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The output verifies that both resources were created:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
deployment "frontend" created
|
|
|
|
|
service "frontend" created
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**Note**: The nginx configuration is baked into the
|
2018-07-04 06:19:23 +00:00
|
|
|
|
[container image](/examples/service/access/Dockerfile).
|
2016-12-12 19:31:31 +00:00
|
|
|
|
A better way to do this would be to use a
|
2018-01-05 02:05:27 +00:00
|
|
|
|
[ConfigMap](/docs/tasks/configure-pod-container/configure-pod-configmap/), so
|
2016-12-12 19:31:31 +00:00
|
|
|
|
that you can change the configuration more easily.
|
|
|
|
|
|
|
|
|
|
### Interact with the frontend Service
|
|
|
|
|
|
|
|
|
|
Once you’ve created a Service of type LoadBalancer, you can use this
|
|
|
|
|
command to find the external IP:
|
|
|
|
|
|
|
|
|
|
```
|
2018-07-19 00:34:42 +00:00
|
|
|
|
kubectl get service frontend --watch
|
2016-12-12 19:31:31 +00:00
|
|
|
|
```
|
|
|
|
|
|
2018-07-19 00:34:42 +00:00
|
|
|
|
This displays the configuration for the `frontend` Service and watches for
|
|
|
|
|
changes. Initially, the external IP is listed as `<pending>`:
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
|
|
|
|
|
frontend 10.51.252.116 <pending> 80/TCP 10s
|
|
|
|
|
```
|
|
|
|
|
|
2018-07-19 00:34:42 +00:00
|
|
|
|
As soon as an external IP is provisioned, however, the configuration updates
|
|
|
|
|
to include the new IP under the `EXTERNAL-IP` heading:
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
|
|
|
|
|
frontend 10.51.252.116 XXX.XXX.XXX.XXX 80/TCP 1m
|
|
|
|
|
```
|
|
|
|
|
|
2018-07-19 00:34:42 +00:00
|
|
|
|
That IP can now be used to interact with the `frontend` service from outside the
|
|
|
|
|
cluster.
|
|
|
|
|
|
2016-12-12 19:31:31 +00:00
|
|
|
|
### Send traffic through the frontend
|
|
|
|
|
|
|
|
|
|
The frontend and backends are now connected. You can hit the endpoint
|
|
|
|
|
by using the curl command on the external IP of your frontend Service.
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
curl http://<EXTERNAL-IP>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The output shows the message generated by the backend:
|
|
|
|
|
|
2018-07-19 00:34:42 +00:00
|
|
|
|
```json
|
2016-12-12 19:31:31 +00:00
|
|
|
|
{"message":"Hello"}
|
|
|
|
|
```
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
|
{{% /capture %}}
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
|
{{% capture whatsnext %}}
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
2017-04-19 17:56:47 +00:00
|
|
|
|
* Learn more about [Services](/docs/concepts/services-networking/service/)
|
2018-01-05 02:05:27 +00:00
|
|
|
|
* Learn more about [ConfigMaps](/docs/tasks/configure-pod-container/configure-pod-configmap/)
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
|
{{% /capture %}}
|
|
|
|
|
|
2016-12-12 19:31:31 +00:00
|
|
|
|
|