Connect a Front End to a Back End Using a Service

This task shows how to create a frontend and a backend microservice. The backend microservice is a hello greeter. The frontend and backend are connected using a Kubernetes Service object.

Objectives

  • 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.

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

To check the version, enter kubectl version.

This task uses Services with external load balancers, which require a supported environment. If your environment does not support this, you can use a Service of 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:

service/access/hello.yaml Connect a Front End to a Back End Using a Service - 图1

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: hello
  5. spec:
  6. selector:
  7. matchLabels:
  8. app: hello
  9. tier: backend
  10. track: stable
  11. replicas: 7
  12. template:
  13. metadata:
  14. labels:
  15. app: hello
  16. tier: backend
  17. track: stable
  18. spec:
  19. containers:
  20. - name: hello
  21. image: "gcr.io/google-samples/hello-go-gke:1.0"
  22. ports:
  23. - name: http
  24. containerPort: 80

Create the backend Deployment:

  1. kubectl apply -f https://k8s.io/examples/service/access/hello.yaml

View information about the backend Deployment:

  1. kubectl describe deployment hello

The output is similar to this:

  1. Name: hello
  2. Namespace: default
  3. CreationTimestamp: Mon, 24 Oct 2016 14:21:02 -0700
  4. Labels: app=hello
  5. tier=backend
  6. track=stable
  7. Annotations: deployment.kubernetes.io/revision=1
  8. Selector: app=hello,tier=backend,track=stable
  9. Replicas: 7 desired | 7 updated | 7 total | 7 available | 0 unavailable
  10. StrategyType: RollingUpdate
  11. MinReadySeconds: 0
  12. RollingUpdateStrategy: 1 max unavailable, 1 max surge
  13. Pod Template:
  14. Labels: app=hello
  15. tier=backend
  16. track=stable
  17. Containers:
  18. hello:
  19. Image: "gcr.io/google-samples/hello-go-gke:1.0"
  20. Port: 80/TCP
  21. Environment: <none>
  22. Mounts: <none>
  23. Volumes: <none>
  24. Conditions:
  25. Type Status Reason
  26. ---- ------ ------
  27. Available True MinimumReplicasAvailable
  28. Progressing True NewReplicaSetAvailable
  29. OldReplicaSets: <none>
  30. NewReplicaSet: hello-3621623197 (7/7 replicas created)
  31. Events:
  32. ...

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 selectors to find the Pods that it routes traffic to.

First, explore the Service configuration file:

service/access/hello-service.yaml Connect a Front End to a Back End Using a Service - 图2

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: hello
  5. spec:
  6. selector:
  7. app: hello
  8. tier: backend
  9. ports:
  10. - protocol: TCP
  11. port: 80
  12. targetPort: http

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:

  1. kubectl apply -f https://k8s.io/examples/service/access/hello-service.yaml

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:

service/access/frontend.conf Connect a Front End to a Back End Using a Service - 图3

  1. upstream hello {
  2. server hello;
  3. }
  4. server {
  5. listen 80;
  6. location / {
  7. proxy_pass http://hello;
  8. }
  9. }

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.

service/access/frontend.yaml Connect a Front End to a Back End Using a Service - 图4

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: frontend
  5. spec:
  6. selector:
  7. app: hello
  8. tier: frontend
  9. ports:
  10. - protocol: "TCP"
  11. port: 80
  12. targetPort: 80
  13. type: LoadBalancer
  14. ---
  15. apiVersion: apps/v1
  16. kind: Deployment
  17. metadata:
  18. name: frontend
  19. spec:
  20. selector:
  21. matchLabels:
  22. app: hello
  23. tier: frontend
  24. track: stable
  25. replicas: 1
  26. template:
  27. metadata:
  28. labels:
  29. app: hello
  30. tier: frontend
  31. track: stable
  32. spec:
  33. containers:
  34. - name: nginx
  35. image: "gcr.io/google-samples/hello-frontend:1.0"
  36. lifecycle:
  37. preStop:
  38. exec:
  39. command: ["/usr/sbin/nginx","-s","quit"]

Create the frontend Deployment and Service:

  1. kubectl apply -f https://k8s.io/examples/service/access/frontend.yaml

The output verifies that both resources were created:

  1. deployment.apps/frontend created
  2. service/frontend created

Note: The nginx configuration is baked into the container image. A better way to do this would be to use a ConfigMap, so 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:

  1. kubectl get service frontend --watch

This displays the configuration for the frontend Service and watches for changes. Initially, the external IP is listed as <pending>:

  1. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  2. frontend LoadBalancer 10.51.252.116 <pending> 80/TCP 10s

As soon as an external IP is provisioned, however, the configuration updates to include the new IP under the EXTERNAL-IP heading:

  1. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  2. frontend LoadBalancer 10.51.252.116 XXX.XXX.XXX.XXX 80/TCP 1m

That IP can now be used to interact with the frontend service from outside the cluster.

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.

  1. curl http://${EXTERNAL_IP} # replace this with the EXTERNAL-IP you saw earlier

The output shows the message generated by the backend:

  1. {"message":"Hello"}

Cleaning up

To delete the Services, enter this command:

  1. kubectl delete services frontend hello

To delete the Deployments, the ReplicaSets and the Pods that are running the backend and frontend applications, enter this command:

  1. kubectl delete deployment frontend hello

What’s next