@@ -3,31 +3,40 @@ title: 使用 Service 把前端连接到后端
3
3
content_type : tutorial
4
4
weight : 70
5
5
---
6
+ <!--
7
+ title: Connect a Frontend to a Backend Using Services
8
+ content_type: tutorial
9
+ weight: 70
10
+ -->
6
11
7
12
<!-- overview -->
8
13
9
14
<!--
10
- This task shows how to create a frontend and a backend
11
- microservice. The backend microservice is a hello greeter. The
12
- frontend and backend are connected using a Kubernetes
13
- {{< glossary_tooltip term_id="service" >}} object.
15
+ This task shows how to create a _frontend_ and a _backend_ microservice. The backend
16
+ microservice is a hello greeter. The frontend exposes the backend using nginx and a
17
+ Kubernetes {{< glossary_tooltip term_id="service" >}} object.
14
18
-->
15
19
16
- 本任务会描述如何创建前端微服务和后端微服务 。后端微服务是一个 hello 欢迎程序。
17
- 前端和后端的连接是通过 Kubernetes {{< glossary_tooltip term_id="service" text="服务" >}}
18
- 完成的 。
20
+ 本任务会描述如何创建前端(Frontend)微服务和后端(Backend)微服务 。后端微服务是一个 hello 欢迎程序。
21
+ 前端通过 nginx 和一个 Kubernetes {{< glossary_tooltip term_id="service" text="服务" >}}
22
+ 暴露后端所提供的服务 。
19
23
20
24
## {{% heading "objectives" %}}
21
25
22
26
<!--
23
- * Create and run a microservice using a {{< glossary_tooltip term_id="deployment" >}} object.
24
- * Route traffic to the backend using a frontend.
25
- * Use a Service object to connect the frontend application to the
26
- backend application.
27
+ * Create and run a sample `hello` backend microservice using a
28
+ {{< glossary_tooltip term_id="deployment" >}} object.
29
+ * Use a Service object to send traffic to the backend microservice's multiple replicas.
30
+ * Create and run a `nginx` frontend microservice, also using a Deployment object.
31
+ * Configure the frontend microservice to send traffic to the backend microservice.
32
+ * Use a Service object of `type=LoadBalancer` to expose the frontend microservice
33
+ outside the cluster.
27
34
-->
28
- * 使用部署对象(Deployment object)创建并运行一个微服务
29
- * 从后端将流量路由到前端
30
- * 使用服务对象把前端应用连接到后端应用
35
+ * 使用部署对象(Deployment object)创建并运行一个 ` hello ` 后端微服务
36
+ * 使用一个 Service 对象将请求流量发送到后端微服务的多个副本
37
+ * 同样使用一个 Deployment 对象创建并运行一个 ` nginx ` 前端微服务
38
+ * 配置前端微服务将请求流量发送到后端微服务
39
+ * 使用 ` type=LoadBalancer ` 的 Service 对象将全段微服务暴露到集群外部
31
40
32
41
## {{% heading "prerequisites" %}}
33
42
@@ -39,8 +48,7 @@ This task uses
39
48
require a supported environment. If your environment does not support this, you can use a Service of type
40
49
[NodePort](/docs/concepts/services-networking/service/#nodeport) instead.
41
50
-->
42
-
43
- 本任务使用 [ 外部负载均衡服务] ( /zh/docs/tasks/access-application-cluster/create-external-load-balancer/ ) ,
51
+ 本任务使用[ 外部负载均衡服务] ( /zh/docs/tasks/access-application-cluster/create-external-load-balancer/ ) ,
44
52
所以需要对应的可支持此功能的环境。如果你的环境不能支持,你可以使用
45
53
[ NodePort] ( /zh/docs/concepts/services-networking/service/#nodeport )
46
54
类型的服务代替。
@@ -57,23 +65,23 @@ file for the backend Deployment:
57
65
58
66
后端是一个简单的 hello 欢迎微服务应用。这是后端应用的 Deployment 配置文件:
59
67
60
- {{< codenew file="service/access/hello .yaml" >}}
68
+ {{< codenew file="service/access/backend-deployment .yaml" >}}
61
69
62
70
<!--
63
71
Create the backend Deployment:
64
72
-->
65
73
创建后端 Deployment:
66
74
67
75
``` shell
68
- kubectl apply -f https://k8s.io/examples/service/access/hello .yaml
76
+ kubectl apply -f https://k8s.io/examples/service/access/backend-deployment .yaml
69
77
```
70
78
71
79
<!--
72
80
View information about the backend Deployment:
73
81
-->
74
82
查看后端的 Deployment 信息:
75
83
76
- ```
84
+ ``` shell
77
85
kubectl describe deployment hello
78
86
```
79
87
@@ -83,15 +91,15 @@ The output is similar to this:
83
91
输出类似于:
84
92
85
93
```
86
- Name: hello
94
+ Name: backend
87
95
Namespace: default
88
96
CreationTimestamp: Mon, 24 Oct 2016 14:21:02 -0700
89
97
Labels: app=hello
90
98
tier=backend
91
99
track=stable
92
100
Annotations: deployment.kubernetes.io/revision=1
93
101
Selector: app=hello,tier=backend,track=stable
94
- Replicas: 7 desired | 7 updated | 7 total | 7 available | 0 unavailable
102
+ Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
95
103
StrategyType: RollingUpdate
96
104
MinReadySeconds: 0
97
105
RollingUpdateStrategy: 1 max unavailable, 1 max surge
@@ -112,92 +120,109 @@ Conditions:
112
120
Available True MinimumReplicasAvailable
113
121
Progressing True NewReplicaSetAvailable
114
122
OldReplicaSets: <none>
115
- NewReplicaSet: hello-3621623197 (7/7 replicas created)
123
+ NewReplicaSet: hello-3621623197 (3/3 replicas created)
116
124
Events:
117
125
...
118
126
```
119
127
120
128
<!--
121
- ## Creating the backend Service object
129
+ ## Creating the `hello` Service object
122
130
123
- The key to connecting a frontend to a backend is the backend
131
+ The key to sending requests from a frontend to a backend is the backend
124
132
Service. A Service creates a persistent IP address and DNS name entry
125
133
so that the backend microservice can always be reached. A Service uses
126
134
{{< glossary_tooltip text="selectors" term_id="selector" >}} to find
127
135
the Pods that it routes traffic to.
128
136
129
137
First, explore the Service configuration file:
130
138
-->
131
- ### 创建后端服务对象
139
+ ### 创建 ` hello ` Service 对象
132
140
133
- 前端连接到后端的关键是 Service(服务) 。Service 创建一个固定 IP 和 DNS 解析名入口,
134
- 使得后端微服务可达 。Service 使用
141
+ 将请求从前端发送到到后端的关键是后端 Service。Service 创建一个固定 IP 和 DNS 解析名入口,
142
+ 使得后端微服务总是可达 。Service 使用
135
143
{{< glossary_tooltip text="选择算符" term_id="selector" >}}
136
144
来寻找目标 Pod。
137
145
138
146
首先,浏览 Service 的配置文件:
139
147
140
- {{< codenew file="service/access/hello -service.yaml" >}}
148
+ {{< codenew file="service/access/backend -service.yaml" >}}
141
149
142
150
<!--
143
- In the configuration file, you can see that the Service routes traffic to Pods
144
- that have the labels `app: hello` and `tier: backend`.
151
+ In the configuration file, you can see that the Service named `hello` routes
152
+ traffic to Pods that have the labels `app: hello` and `tier: backend`.
145
153
-->
146
- 配置文件中,你可以看到 Service 将流量路由到包含 ` app: hello ` 和 ` tier: backend ` 标签的 Pod。
154
+ 配置文件中,你可以看到名为 ` hello ` 的 Service 将流量路由到包含 ` app: hello `
155
+ 和 ` tier: backend ` 标签的 Pod。
147
156
148
157
<!--
149
- Create the `hello` Service:
158
+ Create the backend Service:
150
159
-->
151
- 创建 ` hello ` Service:
160
+ 创建后端 Service:
152
161
153
162
``` shell
154
- kubectl apply -f https://k8s.io/examples/service/access/hello -service.yaml
163
+ kubectl apply -f https://k8s.io/examples/service/access/backend -service.yaml
155
164
```
156
165
157
166
<!--
158
- At this point, you have a backend Deployment running, and you have a
159
- Service that can route traffic to it.
167
+ At this point, you have a `backend` Deployment running three replicas of your `hello`
168
+ application, and you have a Service that can route traffic to them. However, this
169
+ service is neither available nor resolvable outside the cluster.
160
170
-->
161
- 此时,你已经有了一个在运行的后端 Deployment,你也有了一个 Service 用于路由网络流量。
171
+ 此时,你已经有了一个运行着 ` hello ` 应用的三个副本的 ` backend ` Deployment,你也有了
172
+ 一个 Service 用于路由网络流量。不过,这个服务在集群外部无法访问也无法解析。
162
173
163
174
<!--
164
175
## Creating the frontend
165
176
166
- Now that you have your backend, you can create a frontend that connects to the backend.
167
- The frontend connects to the backend worker Pods by using the DNS name
168
- given to the backend Service. The DNS name is "hello", which is the value
169
- of the `name` field in the preceding Service configuration file.
177
+ Now that you have your backend running, you can create a frontend that is accessible
178
+ outside the cluster, and connects to the backend by proxying requests to it.
179
+
180
+ The frontend sends requests to the backend worker Pods by using the DNS name
181
+ given to the backend Service. The DNS name is `hello`, which is the value
182
+ of the `name` field in the `examples/service/access/backend-service.yaml`
183
+ configuration file.
170
184
171
185
The Pods in the frontend Deployment run an nginx image that is configured
172
- to find the hello backend Service. Here is the nginx configuration file:
186
+ to proxy requests to the hello backend Service. Here is the nginx configuration file:
173
187
-->
174
188
### 创建前端应用
175
189
176
- 既然你已经有了后端应用,你可以创建一个前端应用连接到后端。前端应用通过 DNS 名连接到后端的工作 Pods。
177
- DNS 名是 "hello",也就是 Service 配置文件中 ` name ` 字段的值。
190
+ 现在你已经有了运行中的后端应用,你可以创建一个可在集群外部访问的前端,并通过代理
191
+ 前端的请求连接到后端。
192
+
193
+ 前端使用被赋予后端 Service 的 DNS 名称将请求发送到后端工作 Pods。这一 DNS
194
+ 名称为 ` hello ` ,也就是 ` examples/service/access/backend-service.yaml ` 配置
195
+ 文件中 ` name ` 字段的取值。
178
196
179
- 前端 Deployment 中的 Pods 运行一个 nginx 镜像,这个已经配置好镜像去寻找后端的 hello Service。
180
- 只是 nginx 的配置文件:
197
+ 前端 Deployment 中的 Pods 运行一个 nginx 镜像,这个已经配置好的镜像会将请求转发
198
+ 给后端的 hello Service。下面是 nginx 的配置文件:
181
199
182
- {{< codenew file="service/access/frontend.conf" >}}
200
+ {{< codenew file="service/access/frontend-nginx .conf" >}}
183
201
184
202
<!--
185
- Similar to the backend, the frontend has a Deployment and a Service. The
186
- configuration for the Service has `type: LoadBalancer`, which means that
187
- the Service uses the default load balancer of your cloud provider.
203
+ Similar to the backend, the frontend has a Deployment and a Service. An important
204
+ difference to notice between the backend and frontend services, is that the
205
+ configuration for the frontend Service has `type: LoadBalancer`, which means that
206
+ the Service uses a load balancer provisioned by your cloud provider and will be
207
+ accessible from outside the cluster.
188
208
-->
189
- 与后端类似,前端用包含一个 Deployment 和一个 Service。Service 的配置文件包含了 ` type: LoadBalancer ` ,
190
- 也就是说,Service 会使用你的云服务商的默认负载均衡设备。
209
+ 与后端类似,前端用包含一个 Deployment 和一个 Service。后端与前端服务之间的一个
210
+ 重要区别是前端 Service 的配置文件包含了 ` type: LoadBalancer ` ,也就是说,Service
211
+ 会使用你的云服务商的默认负载均衡设备,从而实现从集群外访问的目的。
212
+
213
+ {{< codenew file="service/access/frontend-service.yaml" >}}
214
+
215
+ {{< codenew file="service/access/frontend-deployment.yaml" >}}
191
216
192
- {{< codenew file="service/access/frontend.yaml" >}}
193
217
194
218
<!--
195
219
Create the frontend Deployment and Service:
196
220
-->
197
221
创建前端 Deployment 和 Service:
198
222
199
223
``` shell
200
- kubectl apply -f https://k8s.io/examples/service/access/frontend.yaml
224
+ kubectl apply -f https://k8s.io/examples/service/access/frontend-deployment.yaml
225
+ kubectl apply -f https://k8s.io/examples/service/access/frontend-service.yaml
201
226
```
202
227
203
228
<!--
@@ -271,21 +296,22 @@ cluster.
271
296
<!--
272
297
## Send traffic through the frontend
273
298
274
- The frontend and backends are now connected. You can hit the endpoint
299
+ The frontend and backend are now connected. You can hit the endpoint
275
300
by using the curl command on the external IP of your frontend Service.
276
301
-->
277
302
### 通过前端发送流量
278
303
279
- 前端和后端已经完成连接了。你可以使用 curl 命令通过你的前端 Service 的外部 IP 访问服务端点。
304
+ 前端和后端已经完成连接了。你可以使用 curl 命令通过你的前端 Service 的外部
305
+ IP 访问服务端点。
280
306
281
307
``` shell
282
- curl http://< EXTERNAL-IP >
308
+ curl http://${EXTERNAL_IP} # 将 EXTERNAL_P 替换为你之前看到的外部 IP
283
309
```
284
310
285
311
<!--
286
312
The output shows the message generated by the backend:
287
313
-->
288
- 后端生成的消息输出如下 :
314
+ 输出显示后端生成的消息 :
289
315
290
316
``` json
291
317
{"message" :" Hello" }
@@ -299,7 +325,7 @@ To delete the Services, enter this command:
299
325
要删除服务,输入下面的命令:
300
326
301
327
``` shell
302
- kubectl delete services frontend hello
328
+ kubectl delete services frontend backend
303
329
```
304
330
305
331
<!--
@@ -308,14 +334,16 @@ To delete the Deployments, the ReplicaSets and the Pods that are running the bac
308
334
要删除在前端和后端应用中运行的 Deployment、ReplicaSet 和 Pod,输入下面的命令:
309
335
310
336
``` shell
311
- kubectl delete deployment frontend hello
337
+ kubectl delete deployment frontend backend
312
338
```
313
339
## {{% heading "whatsnext" %}}
314
340
315
341
<!--
316
342
* Learn more about [Services](/docs/concepts/services-networking/service/)
317
343
* Learn more about [ConfigMaps](/docs/tasks/configure-pod-container/configure-pod-configmap/)
344
+ * Learn more about [DNS for Service and Pods](/docs/concepts/services-networking/dns-pod-service/)
318
345
-->
319
- * 进一步了解[ Service] ( /zh/docs/concepts/services-networking/service/ )
320
- * 进一步了解[ ConfigMap] ( /zh/docs/tasks/configure-pod-container/configure-pod-configmap/ )
346
+ * 进一步了解 [ Service] ( /zh/docs/concepts/services-networking/service/ )
347
+ * 进一步了解 [ ConfigMap] ( /zh/docs/tasks/configure-pod-container/configure-pod-configmap/ )
348
+ * 进一步了解 [ Service 和 Pods 的 DNS] ( /docs/concepts/services-networking/dns-pod-service/ )
321
349
0 commit comments