⎈ k8s knowledge compiler

Connect a Frontend to a Backend Using Services [page]deterministic

tasksnetworking

This task shows how to create a _frontend_ and a _backend_ microservice. The backend microservice is a hello greeter. The frontend exposes the backend using nginx and a Kubernetes [service](#gloss:service) object.

##

* Create and run a sample `hello` backend microservice using a [deployment](#gloss:deployment) object. * Use a Service object to send traffic to the backend microservice's multiple replicas. * Create and run a `nginx` frontend microservice, also using a Deployment object. * Configure the frontend microservice to send traffic to the backend microservice. * Use a Service object of `type=LoadBalancer` to expose the frontend microservice outside the cluster.

##

This task uses [Services with external load balancers](/docs/tasks/access-application-cluster/create-external-load-balancer/), which require a supported environment. If your environment does not support this, you can use a Service of type [NodePort](/docs/concepts/services-networking/service/#type-nodeport) instead.

## Creating the backend using a Deployment

The backend is a simple hello greeter microservice. Here is the configuration file for the backend Deployment:

Create the backend Deployment:

```shell kubectl apply -f https://k8s.io/examples/service/access/backend-deployment.yaml ```

View information about the backend Deployment:

```shell kubectl describe deployment backend ```

The output is similar to this:

``` Name: backend Namespace: default CreationTimestamp: Mon, 24 Oct 2016 14:21:02 -0700 Labels: app=hello tier=backend track=stable Annotations: deployment.kubernetes.io/revision=1 Selector: app=hello,tier=backend,track=stable Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 1 max unavailable, 1 max surge 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 OldReplicaSets: <none> NewReplicaSet: hello-3621623197 (3/3 replicas created) Events: ... ```

## Creating the `hello` Service object

The key to sending requests from 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 [selectors](#gloss:selector) to find the Pods that it routes traffic to.

First, explore the Service configuration file:

In the configuration file, you can see that the Service, named `hello` routes traffic to Pods that have the labels `app: hello` and `tier: backend`.

Create the backend Service:

```shell kubectl apply -f https://k8s.io/examples/service/access/backend-service.yaml ```

At this point, you have a `backend` Deployment running three replicas of your `hello` application, and you have a Service that can route traffic to them. However, this service is neither available nor resolvable outside the cluster.

## Creating the frontend

Now that you have your backend running, you can create a frontend that is accessible outside the cluster, and connects to the backend by proxying requests to it.

The frontend sends requests 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 `examples/service/access/backend-service.yaml` confi …(trimmed)

Sources

tasks/access-application-cluster/connecting-frontend-backend.md · docConnect a Frontend to a Backend Using Services

Related (13)

references Serviceservice conf=1
references Deploymentdeployment conf=1
references Selectorselectors conf=1
part_of {{% heading "objectives" %}}describes conf=1
part_of {{% heading "prerequisites" %}}describes conf=1
part_of Creating the backend using a Deploymentdescribes conf=1
part_of Creating the `hello` Service objectdescribes conf=1
part_of Creating the frontenddescribes conf=1
part_of Interact with the frontend Servicedescribes conf=1
part_of Send traffic through the frontenddescribes conf=1
part_of {{% heading "cleanup" %}}describes conf=1
part_of {{% heading "whatsnext" %}}describes conf=1
api_for Servicedocuments API object conf=1

← all Docs