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

225 lines
5.0 KiB
Markdown
Raw Normal View History

2017-09-08 10:03:12 +00:00
---
title: 使用 Service 把前端连接到后端
content_template: templates/tutorial
2017-09-08 10:03:12 +00:00
---
2017-09-08 10:03:12 +00:00
{{% capture overview %}}
2017-09-08 10:03:12 +00:00
2017-09-21 04:14:53 +00:00
本任务会描述如何创建前端微服务和后端微服务。后端微服务是一个 hello 欢迎程序。
前端和后端的连接是通过 Kubernetes 服务对象Service object完成的。
2017-09-08 10:03:12 +00:00
{{% /capture %}}
2017-09-08 10:03:12 +00:00
{{% capture objectives %}}
2017-09-08 10:03:12 +00:00
2017-09-21 04:14:53 +00:00
* 使用部署对象Deployment object创建并运行一个微服务
2017-09-08 10:03:12 +00:00
* 从后端将流量路由到前端
* 使用服务对象把前端应用连接到后端应用
2017-09-08 10:03:12 +00:00
{{% /capture %}}
2017-09-08 10:03:12 +00:00
{{% capture prerequisites %}}
2017-09-08 10:03:12 +00:00
* {{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
2017-09-08 10:03:12 +00:00
2017-09-21 04:14:53 +00:00
* 本任务使用 [外部负载均衡服务](/docs/tasks/access-application-cluster/create-external-load-balancer/)
所以需要对应的可支持此功能的环境。如果你的环境不能支持,你可以使用
Update localization guidelines (#10485) * Update localization guidelines for language labels Continuing work Continuing work Continuing work More work in progress Add local OWNERS folders Add an OWNERS file to Chinese Remove shortcode for repos Add Japanese Alphabetize languages, change weights accordingly More updates Add Korean in Korean Add English to languageName Feedback from gochist Move Chinese content from cn/ to zh/ Move OWNERS from cn/ to zh/ Resolve merge conflicts by updating from master Add files back in to prep for resolution After rebase on upstream/master, remove files Review and update localization guidelines Feedback from gochist, tnir, cstoku Add a trailing newline to content/ja/OWNERS Add a trailing newline to content/zh/OWNERS Drop requirement for GH repo project Clarify language about forks/branches Edits and typos Remove a shortcode specific to a multi-repo language setup Update aliases and owners Add explicit OWNERS for content/en Migrate content from Chinese repo, update regex in config.toml Remove untranslated strings Add trailing newline to content/en/OWNERS Add trailing newlines to OWNERS files add Jaguar project description (#10433) * add Jaguar project description [Jaguar](https://gitlab.com/sdnlab/jaguar) is an open source solution for Kubernetes's network based on OpenDaylight. Jaguar provides overlay network using vxlan and Jaguar CNIPlugin provides one IP address per pod. * Minor newline tweak blog post for azure vmss (#10538) Add microk8s to pick-right-solution.md (#10542) * Add microk8s to pick-right-solution.md Microk8s is a single-command installation of upstream Kubernetes on any Linux and should be included in the list of local-machine solutions. * capitalized Istio Add microk8s to foundational.md (#10543) * Add microk8s to foundational.md Adding microk8s as credible and stable alternative to get started with Kubernetes on a local machine. This is especially attractive for those not wanting to incur the overhead of running a VM for a local cluster. * Update foundational.md Thank you for your suggestions! LMK if this works now? * Rewrote first paragraph And included a bullet list of features of microk8s * Copyedit fix typo (#10545) Fix the kubectl subcommands links. (#10550) Signed-off-by: William Zhang <warmchang@outlook.com> Fix command issue (#10515) Signed-off-by: mooncake <xcoder@tenxcloud.com> remove imported community files per issue 10184 (#10501) networking.md: Markdown fix (#10498) Fix front matter, federation command-line tools (#10500) Clean up glossary entry (#10399) update slack link (#10536) typo in StatefulSet docs (#10558) fix discription about horizontal pod autoscale (#10557) Remove redundant symbols (#10556) Fix issue #10520 (#10554) Signed-off-by: William Zhang <warmchang@outlook.com> Update api-concepts.md (#10534) Revert "Fix command issue (#10515)" This reverts commit c02a7fb9f9d19872d9227814b3e9ffaaa28d85f0. Update memory-constraint-namespace.md (#10530) update memory request to 100MiB corresponding the yaml content Blog: Introducing Volume Snapshot Alpha for Kubernetes (#10562) * blog post for azure vmss * snapshot blog post Resolve merge conflicts in OWNERS* Minor typo fix (#10567) Not sure what's supposed to be here, proposing removing it. * Feedback from gochist Tweaks to feedback * Feedback from ClaudiaJKang
2018-10-12 21:25:01 +00:00
[NodePort](/docs/user-guide/services/#type-nodeport) 类型的服务代替。
2017-09-08 10:03:12 +00:00
{{% /capture %}}
2017-09-08 10:03:12 +00:00
{{% capture lessoncontent %}}
2017-09-08 10:03:12 +00:00
2017-09-21 04:14:53 +00:00
### 使用部署对象Deployment创建后端
2017-09-08 10:03:12 +00:00
2017-09-08 10:03:12 +00:00
后端是一个简单的 hello 欢迎微服务应用。这是后端应用的 Deployment 配置文件:
{{< code file="hello.yaml" >}}
2017-09-08 10:03:12 +00:00
2017-09-08 10:03:12 +00:00
创建后端 Deployment
```
kubectl create -f https://k8s.io/docs/tasks/access-application-cluster/hello.yaml
```
2017-09-08 10:03:12 +00:00
查看后端的 Deployment 信息:
```
kubectl describe deployment hello
```
2017-09-08 10:03:12 +00:00
输出类似于:
```
Name: hello
Namespace: default
CreationTimestamp: Mon, 24 Oct 2016 14:21:02 -0700
Labels: app=hello
tier=backend
track=stable
Selector: app=hello,tier=backend,track=stable
Replicas: 7 updated | 7 total | 7 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
OldReplicaSets: <none>
NewReplicaSet: hello-3621623197 (7/7 replicas created)
Events:
...
```
2017-09-21 04:14:53 +00:00
### 创建后端服务对象Service object
2017-09-08 10:03:12 +00:00
2017-09-08 10:03:12 +00:00
前端连接到后端的关键是 Service。Service 创建一个固定 IP 和 DNS 解析名入口,
2017-09-21 04:14:53 +00:00
使得后端微服务可达。Service 使用 selector 标签来寻找目标 Pod。
2017-09-08 10:03:12 +00:00
2017-09-08 10:03:12 +00:00
首先,浏览 Service 的配置文件:
{{< code file="hello-service.yaml" >}}
2017-09-08 10:03:12 +00:00
2017-09-21 04:14:53 +00:00
配置文件中,你可以看到 Service 将流量路由到包含 `app: hello``tier: backend` 标签的 Pod。
2017-09-08 10:03:12 +00:00
2017-09-08 10:03:12 +00:00
创建 `hello` Service
```
kubectl create -f https://k8s.io/docs/tasks/access-application-cluster/hello-service.yaml
```
2017-09-08 10:03:12 +00:00
此时,你已经有了一个在运行的后端 Deployment你也有了一个 Service 用于路由网络流量。
2017-09-08 10:03:12 +00:00
### 创建前端应用
2017-09-08 10:03:12 +00:00
既然你已经有了后端应用,你可以创建一个前端应用连接到后端。前端应用通过 DNS 名连接到后端的工作 Pods。
DNS 名是 "hello",也就是 Service 配置文件中 `name` 字段的值。
2017-09-08 10:03:12 +00:00
前端 Deployment 中的 Pods 运行一个 nginx 镜像,这个已经配置好镜像去寻找后端的 hello Service。
只是 nginx 的配置文件:
{{< code file="frontend/frontend.conf" >}}
2017-09-08 10:03:12 +00:00
与后端类似,前端用包含一个 Deployment 和一个 Service。Service 的配置文件包含了 `type: LoadBalancer`
2017-09-08 10:03:12 +00:00
也就是说Service 会使用你的云服务商的默认负载均衡设备。
{{< code file="frontend.yaml" >}}
2017-09-08 10:03:12 +00:00
2017-09-08 10:03:12 +00:00
创建前端 Deployment 和 Service
```
kubectl create -f https://k8s.io/docs/tasks/access-application-cluster/frontend.yaml
```
2017-09-21 04:14:53 +00:00
通过输出确认两个资源都已经被创建:
2017-09-08 10:03:12 +00:00
```
deployment "frontend" created
service "frontend" created
```
2017-09-21 04:14:53 +00:00
**注意**:这个 nginx 配置文件是被打包在 [容器镜像](/docs/tasks/access-application-cluster/frontend/Dockerfile) 里的。
更好的方法是使用 [ConfigMap](/docs/tasks/configure-pod-container/configure-pod-configmap/),这样的话你可以更轻易地更改配置。
2017-09-08 10:03:12 +00:00
### 与前端 Service 交互
2017-09-08 10:03:12 +00:00
一旦你创建了 LoadBalancer 类型的 Service你可以使用这条命令查看外部 IP
```
kubectl get service frontend
```
2017-09-21 04:14:53 +00:00
外部 IP 字段的生成可能需要一些时间。如果是这种情况,外部 IP 会显示为 `<pending>`
2017-09-08 10:03:12 +00:00
```
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend 10.51.252.116 <pending> 80/TCP 10s
```
2017-09-08 10:03:12 +00:00
使用相同的命令直到它显示外部 IP 地址:
```
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
frontend 10.51.252.116 XXX.XXX.XXX.XXX 80/TCP 1m
```
2017-09-08 10:03:12 +00:00
### 通过前端发送流量
2017-09-08 10:03:12 +00:00
前端和后端已经完成连接了。你可以使用 curl 命令通过你的前端 Service 的外部 IP 访问服务端点。
```
curl http://<EXTERNAL-IP>
```
2017-09-08 10:03:12 +00:00
后端生成的消息输出如下:
```
{"message":"Hello"}
```
{{% /capture %}}
2017-09-08 10:03:12 +00:00
{{% capture whatsnext %}}
2017-09-08 10:03:12 +00:00
2017-09-21 04:14:53 +00:00
* 了解更多 [Services](/docs/concepts/services-networking/service/)
* 了解更多 [ConfigMaps](/docs/tasks/configure-pod-container/configure-pod-configmap/)
2017-09-08 10:03:12 +00:00
{{% /capture %}}
2017-09-08 10:03:12 +00:00