1
1
---
2
- title : Connect a Front End to a Back End Using a Service
2
+ title : Connect a Frontend to a Backend Using Services
3
3
content_type : tutorial
4
4
weight : 70
5
5
---
6
6
7
7
<!-- overview -->
8
8
9
- This task shows how to create a frontend and a backend
10
- microservice. The backend microservice is a hello greeter. The
11
- frontend and backend are connected using a Kubernetes
12
- {{< glossary_tooltip term_id="service" >}} object.
9
+ This task shows how to create a _ frontend_ and a _ backend_ microservice. The backend
10
+ microservice is a hello greeter. The frontend exposes the backend using nginx and a
11
+ Kubernetes {{< glossary_tooltip term_id="service" >}} object.
13
12
14
13
## {{% heading "objectives" %}}
15
14
16
- * Create and run a microservice using a {{< glossary_tooltip term_id="deployment" >}} object.
17
- * Route traffic to the backend using a frontend.
18
- * Use a Service object to connect the frontend application to the
19
- backend application.
15
+ * Create and run a sample ` hello ` backend microservice using a
16
+ {{< glossary_tooltip term_id="deployment" >}} object.
17
+ * Use a Service object to send traffic to the backend microservice's multiple replicas.
18
+ * Create and run a ` nginx ` frontend microservice, also using a Deployment object.
19
+ * Configure the frontend microservice to send traffic to the backend microservice.
20
+ * Use a Service object of ` type=LoadBalancer ` to expose the frontend microservice
21
+ outside the cluster.
20
22
21
23
## {{% heading "prerequisites" %}}
22
24
@@ -34,32 +36,32 @@ require a supported environment. If your environment does not support this, you
34
36
The backend is a simple hello greeter microservice. Here is the configuration
35
37
file for the backend Deployment:
36
38
37
- {{< codenew file="service/access/hello .yaml" >}}
39
+ {{< codenew file="service/access/backend-deployment .yaml" >}}
38
40
39
41
Create the backend Deployment:
40
42
41
43
``` shell
42
- kubectl apply -f https://k8s.io/examples/service/access/hello .yaml
44
+ kubectl apply -f https://k8s.io/examples/service/access/backend-deployment .yaml
43
45
```
44
46
45
47
View information about the backend Deployment:
46
48
47
49
``` shell
48
- kubectl describe deployment hello
50
+ kubectl describe deployment backend
49
51
```
50
52
51
53
The output is similar to this:
52
54
53
55
```
54
- Name: hello
56
+ Name: backend
55
57
Namespace: default
56
58
CreationTimestamp: Mon, 24 Oct 2016 14:21:02 -0700
57
59
Labels: app=hello
58
60
tier=backend
59
61
track=stable
60
62
Annotations: deployment.kubernetes.io/revision=1
61
63
Selector: app=hello,tier=backend,track=stable
62
- Replicas: 7 desired | 7 updated | 7 total | 7 available | 0 unavailable
64
+ Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
63
65
StrategyType: RollingUpdate
64
66
MinReadySeconds: 0
65
67
RollingUpdateStrategy: 1 max unavailable, 1 max surge
@@ -80,57 +82,66 @@ Conditions:
80
82
Available True MinimumReplicasAvailable
81
83
Progressing True NewReplicaSetAvailable
82
84
OldReplicaSets: <none>
83
- NewReplicaSet: hello-3621623197 (7/7 replicas created)
85
+ NewReplicaSet: hello-3621623197 (3/3 replicas created)
84
86
Events:
85
87
...
86
88
```
87
89
88
- ## Creating the backend Service object
90
+ ## Creating the ` hello ` Service object
89
91
90
- The key to connecting a frontend to a backend is the backend
92
+ The key to sending requests from a frontend to a backend is the backend
91
93
Service. A Service creates a persistent IP address and DNS name entry
92
94
so that the backend microservice can always be reached. A Service uses
93
95
{{< glossary_tooltip text="selectors" term_id="selector" >}} to find
94
96
the Pods that it routes traffic to.
95
97
96
98
First, explore the Service configuration file:
97
99
98
- {{< codenew file="service/access/hello -service.yaml" >}}
100
+ {{< codenew file="service/access/backend -service.yaml" >}}
99
101
100
- In the configuration file, you can see that the Service routes traffic to Pods
101
- that have the labels ` app: hello ` and ` tier: backend ` .
102
+ In the configuration file, you can see that the Service, named ` hello ` routes
103
+ traffic to Pods that have the labels ` app: hello ` and ` tier: backend ` .
102
104
103
- Create the ` hello ` Service:
105
+ Create the backend Service:
104
106
105
107
``` shell
106
- kubectl apply -f https://k8s.io/examples/service/access/hello -service.yaml
108
+ kubectl apply -f https://k8s.io/examples/service/access/backend -service.yaml
107
109
```
108
110
109
- At this point, you have a backend Deployment running, and you have a
110
- Service that can route traffic to it.
111
+ At this point, you have a ` backend ` Deployment running three replicas of your ` hello `
112
+ application, and you have a Service that can route traffic to them. However, this
113
+ service is neither available nor resolvable outside the cluster.
111
114
112
115
## Creating the frontend
113
116
114
- Now that you have your backend, you can create a frontend that connects to the backend.
115
- The frontend connects to the backend worker Pods by using the DNS name
116
- given to the backend Service. The DNS name is "hello", which is the value
117
- of the ` name ` field in the preceding Service configuration file.
117
+ Now that you have your backend running, you can create a frontend that is accessible
118
+ outside the cluster, and connects to the backend by proxying requests to it.
118
119
119
- The Pods in the frontend Deployment run an nginx image that is configured
120
- to find the hello backend Service. Here is the nginx configuration file:
120
+ The frontend sends requests to the backend worker Pods by using the DNS name
121
+ given to the backend Service. The DNS name is ` hello ` , which is the value
122
+ of the ` name ` field in the ` examples/service/access/backend-service.yaml `
123
+ configuration file.
121
124
122
- {{< codenew file="service/access/frontend.conf" >}}
125
+ The Pods in the frontend Deployment run a nginx image that is configured
126
+ to proxy requests to the ` hello ` backend Service. Here is the nginx configuration file:
123
127
124
- Similar to the backend, the frontend has a Deployment and a Service. The
125
- configuration for the Service has ` type: LoadBalancer ` , which means that
126
- the Service uses the default load balancer of your cloud provider.
128
+ {{< codenew file="service/access/frontend-nginx.conf" >}}
127
129
128
- {{< codenew file="service/access/frontend.yaml" >}}
130
+ Similar to the backend, the frontend has a Deployment and a Service. An important
131
+ difference to notice between the backend and frontend services, is that the
132
+ configuration for the frontend Service has ` type: LoadBalancer ` , which means that
133
+ the Service uses a load balancer provisioned by your cloud provider and will be
134
+ accessible from outside the cluster.
135
+
136
+ {{< codenew file="service/access/frontend-service.yaml" >}}
137
+
138
+ {{< codenew file="service/access/frontend-deployment.yaml" >}}
129
139
130
140
Create the frontend Deployment and Service:
131
141
132
142
``` shell
133
- kubectl apply -f https://k8s.io/examples/service/access/frontend.yaml
143
+ kubectl apply -f https://k8s.io/examples/service/access/frontend-deployment.yaml
144
+ kubectl apply -f https://k8s.io/examples/service/access/frontend-service.yaml
134
145
```
135
146
136
147
The output verifies that both resources were created:
@@ -178,7 +189,7 @@ cluster.
178
189
179
190
## Send traffic through the frontend
180
191
181
- The frontend and backends are now connected. You can hit the endpoint
192
+ The frontend and backend are now connected. You can hit the endpoint
182
193
by using the curl command on the external IP of your frontend Service.
183
194
184
195
``` shell
@@ -196,17 +207,17 @@ The output shows the message generated by the backend:
196
207
To delete the Services, enter this command:
197
208
198
209
``` shell
199
- kubectl delete services frontend hello
210
+ kubectl delete services frontend backend
200
211
```
201
212
202
213
To delete the Deployments, the ReplicaSets and the Pods that are running the backend and frontend applications, enter this command:
203
214
204
215
``` shell
205
- kubectl delete deployment frontend hello
216
+ kubectl delete deployment frontend backend
206
217
```
207
218
208
219
## {{% heading "whatsnext" %}}
209
220
210
221
* Learn more about [ Services] ( /docs/concepts/services-networking/service/ )
211
222
* Learn more about [ ConfigMaps] ( /docs/tasks/configure-pod-container/configure-pod-configmap/ )
212
-
223
+ * Learn more about [ DNS for Service and Pods ] ( /docs/concepts/services-networking/dns-pod-service/ )
0 commit comments