Skip to content

Commit ce6620d

Browse files
authored
Merge pull request #26298 from tengqm/zh-sync-connecting-fb
[zh] Resync connecting-frontend-backend
2 parents 42da03e + 15be3b1 commit ce6620d

File tree

8 files changed

+152
-114
lines changed

8 files changed

+152
-114
lines changed

content/zh/docs/tasks/access-application-cluster/connecting-frontend-backend.md

Lines changed: 90 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,40 @@ title: 使用 Service 把前端连接到后端
33
content_type: tutorial
44
weight: 70
55
---
6+
<!--
7+
title: Connect a Frontend to a Backend Using Services
8+
content_type: tutorial
9+
weight: 70
10+
-->
611

712
<!-- overview -->
813

914
<!--
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.
1418
-->
1519

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+
暴露后端所提供的服务
1923

2024
## {{% heading "objectives" %}}
2125

2226
<!--
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.
2734
-->
28-
* 使用部署对象(Deployment object)创建并运行一个微服务
29-
* 从后端将流量路由到前端
30-
* 使用服务对象把前端应用连接到后端应用
35+
* 使用部署对象(Deployment object)创建并运行一个 `hello` 后端微服务
36+
* 使用一个 Service 对象将请求流量发送到后端微服务的多个副本
37+
* 同样使用一个 Deployment 对象创建并运行一个 `nginx` 前端微服务
38+
* 配置前端微服务将请求流量发送到后端微服务
39+
* 使用 `type=LoadBalancer` 的 Service 对象将全段微服务暴露到集群外部
3140

3241
## {{% heading "prerequisites" %}}
3342

@@ -39,8 +48,7 @@ This task uses
3948
require a supported environment. If your environment does not support this, you can use a Service of type
4049
[NodePort](/docs/concepts/services-networking/service/#nodeport) instead.
4150
-->
42-
43-
本任务使用 [外部负载均衡服务](/zh/docs/tasks/access-application-cluster/create-external-load-balancer/)
51+
本任务使用[外部负载均衡服务](/zh/docs/tasks/access-application-cluster/create-external-load-balancer/)
4452
所以需要对应的可支持此功能的环境。如果你的环境不能支持,你可以使用
4553
[NodePort](/zh/docs/concepts/services-networking/service/#nodeport)
4654
类型的服务代替。
@@ -57,23 +65,23 @@ file for the backend Deployment:
5765

5866
后端是一个简单的 hello 欢迎微服务应用。这是后端应用的 Deployment 配置文件:
5967

60-
{{< codenew file="service/access/hello.yaml" >}}
68+
{{< codenew file="service/access/backend-deployment.yaml" >}}
6169

6270
<!--
6371
Create the backend Deployment:
6472
-->
6573
创建后端 Deployment:
6674

6775
```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
6977
```
7078

7179
<!--
7280
View information about the backend Deployment:
7381
-->
7482
查看后端的 Deployment 信息:
7583

76-
```
84+
```shell
7785
kubectl describe deployment hello
7886
```
7987

@@ -83,15 +91,15 @@ The output is similar to this:
8391
输出类似于:
8492

8593
```
86-
Name: hello
94+
Name: backend
8795
Namespace: default
8896
CreationTimestamp: Mon, 24 Oct 2016 14:21:02 -0700
8997
Labels: app=hello
9098
tier=backend
9199
track=stable
92100
Annotations: deployment.kubernetes.io/revision=1
93101
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
95103
StrategyType: RollingUpdate
96104
MinReadySeconds: 0
97105
RollingUpdateStrategy: 1 max unavailable, 1 max surge
@@ -112,92 +120,109 @@ Conditions:
112120
Available True MinimumReplicasAvailable
113121
Progressing True NewReplicaSetAvailable
114122
OldReplicaSets: <none>
115-
NewReplicaSet: hello-3621623197 (7/7 replicas created)
123+
NewReplicaSet: hello-3621623197 (3/3 replicas created)
116124
Events:
117125
...
118126
```
119127

120128
<!--
121-
## Creating the backend Service object
129+
## Creating the `hello` Service object
122130
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
124132
Service. A Service creates a persistent IP address and DNS name entry
125133
so that the backend microservice can always be reached. A Service uses
126134
{{< glossary_tooltip text="selectors" term_id="selector" >}} to find
127135
the Pods that it routes traffic to.
128136
129137
First, explore the Service configuration file:
130138
-->
131-
### 创建后端服务对象
139+
### 创建 `hello` Service 对象
132140

133-
前端连接到后端的关键是 Service(服务)。Service 创建一个固定 IP 和 DNS 解析名入口,
134-
使得后端微服务可达。Service 使用
141+
将请求从前端发送到到后端的关键是后端 Service。Service 创建一个固定 IP 和 DNS 解析名入口,
142+
使得后端微服务总是可达。Service 使用
135143
{{< glossary_tooltip text="选择算符" term_id="selector" >}}
136144
来寻找目标 Pod。
137145

138146
首先,浏览 Service 的配置文件:
139147

140-
{{< codenew file="service/access/hello-service.yaml" >}}
148+
{{< codenew file="service/access/backend-service.yaml" >}}
141149

142150
<!--
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`.
145153
-->
146-
配置文件中,你可以看到 Service 将流量路由到包含 `app: hello``tier: backend` 标签的 Pod。
154+
配置文件中,你可以看到名为 `hello` 的 Service 将流量路由到包含 `app: hello`
155+
`tier: backend` 标签的 Pod。
147156

148157
<!--
149-
Create the `hello` Service:
158+
Create the backend Service:
150159
-->
151-
创建 `hello` Service:
160+
创建后端 Service:
152161

153162
```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
155164
```
156165

157166
<!--
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.
160170
-->
161-
此时,你已经有了一个在运行的后端 Deployment,你也有了一个 Service 用于路由网络流量。
171+
此时,你已经有了一个运行着 `hello` 应用的三个副本的 `backend` Deployment,你也有了
172+
一个 Service 用于路由网络流量。不过,这个服务在集群外部无法访问也无法解析。
162173

163174
<!--
164175
## Creating the frontend
165176
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.
170184
171185
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:
173187
-->
174188
### 创建前端应用
175189

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` 字段的取值。
178196

179-
前端 Deployment 中的 Pods 运行一个 nginx 镜像,这个已经配置好镜像去寻找后端的 hello Service。
180-
只是 nginx 的配置文件:
197+
前端 Deployment 中的 Pods 运行一个 nginx 镜像,这个已经配置好的镜像会将请求转发
198+
给后端的 hello Service。下面是 nginx 的配置文件:
181199

182-
{{< codenew file="service/access/frontend.conf" >}}
200+
{{< codenew file="service/access/frontend-nginx.conf" >}}
183201

184202
<!--
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.
188208
-->
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" >}}
191216

192-
{{< codenew file="service/access/frontend.yaml" >}}
193217

194218
<!--
195219
Create the frontend Deployment and Service:
196220
-->
197221
创建前端 Deployment 和 Service:
198222

199223
```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
201226
```
202227

203228
<!--
@@ -271,21 +296,22 @@ cluster.
271296
<!--
272297
## Send traffic through the frontend
273298
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
275300
by using the curl command on the external IP of your frontend Service.
276301
-->
277302
### 通过前端发送流量
278303

279-
前端和后端已经完成连接了。你可以使用 curl 命令通过你的前端 Service 的外部 IP 访问服务端点。
304+
前端和后端已经完成连接了。你可以使用 curl 命令通过你的前端 Service 的外部
305+
IP 访问服务端点。
280306

281307
```shell
282-
curl http://<EXTERNAL-IP>
308+
curl http://${EXTERNAL_IP} # 将 EXTERNAL_P 替换为你之前看到的外部 IP
283309
```
284310

285311
<!--
286312
The output shows the message generated by the backend:
287313
-->
288-
后端生成的消息输出如下
314+
输出显示后端生成的消息
289315

290316
```json
291317
{"message":"Hello"}
@@ -299,7 +325,7 @@ To delete the Services, enter this command:
299325
要删除服务,输入下面的命令:
300326

301327
```shell
302-
kubectl delete services frontend hello
328+
kubectl delete services frontend backend
303329
```
304330

305331
<!--
@@ -308,14 +334,16 @@ To delete the Deployments, the ReplicaSets and the Pods that are running the bac
308334
要删除在前端和后端应用中运行的 Deployment、ReplicaSet 和 Pod,输入下面的命令:
309335

310336
```shell
311-
kubectl delete deployment frontend hello
337+
kubectl delete deployment frontend backend
312338
```
313339
## {{% heading "whatsnext" %}}
314340

315341
<!--
316342
* Learn more about [Services](/docs/concepts/services-networking/service/)
317343
* 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/)
318345
-->
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/)
321349

content/zh/examples/service/access/hello.yaml renamed to content/zh/examples/service/access/backend-deployment.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
---
12
apiVersion: apps/v1
23
kind: Deployment
34
metadata:
4-
name: hello
5+
name: backend
56
spec:
67
selector:
78
matchLabels:
89
app: hello
910
tier: backend
1011
track: stable
11-
replicas: 7
12+
replicas: 3
1213
template:
1314
metadata:
1415
labels:
@@ -22,3 +23,4 @@ spec:
2223
ports:
2324
- name: http
2425
containerPort: 80
26+
...

content/zh/examples/service/access/hello-service.yaml renamed to content/zh/examples/service/access/backend-service.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
apiVersion: v1
23
kind: Service
34
metadata:
@@ -10,3 +11,4 @@ spec:
1011
- protocol: TCP
1112
port: 80
1213
targetPort: http
14+
...
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
apiVersion: apps/v1
3+
kind: Deployment
4+
metadata:
5+
name: frontend
6+
spec:
7+
selector:
8+
matchLabels:
9+
app: hello
10+
tier: frontend
11+
track: stable
12+
replicas: 1
13+
template:
14+
metadata:
15+
labels:
16+
app: hello
17+
tier: frontend
18+
track: stable
19+
spec:
20+
containers:
21+
- name: nginx
22+
image: "gcr.io/google-samples/hello-frontend:1.0"
23+
lifecycle:
24+
preStop:
25+
exec:
26+
command: ["/usr/sbin/nginx","-s","quit"]
27+
...
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# The identifier Backend is internal to nginx, and used to name this specific upstream
2+
upstream Backend {
3+
# hello is the internal DNS name used by the backend Service inside Kubernetes
4+
server hello;
5+
}
6+
7+
server {
8+
listen 80;
9+
10+
location / {
11+
# The following statement will proxy traffic to the upstream named Backend
12+
proxy_pass http://Backend;
13+
}
14+
}

0 commit comments

Comments
 (0)