website/content/zh/docs/tasks/access-application-cluster/service-access-application-...

226 lines
7.4 KiB
Markdown
Raw Normal View History

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
<!--
---
title: Use a Service to Access an Application in a Cluster
content_template: templates/tutorial
weight: 60
---
-->
---
title: 使用服务来访问集群中的应用
content_template: templates/tutorial
weight: 60
---
{{% capture overview %}}
<!--
This page shows how to create a Kubernetes Service object that external
clients can use to access an application running in a cluster. The Service
provides load balancing for an application that has two running instances.
-->
本文展示如何创建一个 Kubernetes 服务对象,能让外部客户端访问在集群中运行的应用。该服务为一个应用的两个运行实例提供负载均衡。
{{% /capture %}}
{{% capture prerequisites %}}
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
{{% /capture %}}
{{% capture objectives %}}
<!--
* Run two instances of a Hello World application.
* Create a Service object that exposes a node port.
* Use the Service object to access the running application.
-->
* 运行 Hello World 应用的两个实例。
* 创建一个服务对象来暴露一个 node port。
* 使用服务对象来访问正在运行的应用。
{{% /capture %}}
{{% capture lessoncontent %}}
<!--
## Creating a service for an application running in two pods
1. Run a Hello World application in your cluster:
-->
## 为运行在两个 pod 中的应用创建一个服务:
1. 在您的集群中运行一个 Hello World 应用:
```shell
kubectl run hello-world --replicas=2 --labels="run=load-balancer-example" --image=gcr.io/google-samples/node-hello:1.0 --port=8080
```
<!--
The preceding command creates a
[Deployment](/docs/concepts/workloads/controllers/deployment/)
object and an associated
[ReplicaSet](/docs/concepts/workloads/controllers/replicaset/)
object. The ReplicaSet has two
[Pods](/docs/concepts/workloads/pods/pod/),
each of which runs the Hello World application.
-->
上面的命令创建一个 [Deployment](/docs/concepts/workloads/controllers/deployment/) 对象和一个关联的 [ReplicaSet](/docs/concepts/workloads/controllers/replicaset/) 对象。这个 ReplicaSet 有两个 [Pod](/docs/concepts/workloads/pods/pod/),每个 Pod 都运行着 Hello World 应用。
<!--
1. Display information about the Deployment:
-->
1. 展示 Deployment 的信息:
```shell
kubectl get deployments hello-world
kubectl describe deployments hello-world
```
<!--
1. Display information about your ReplicaSet objects:
-->
1. 展示您的 ReplicaSet 对象信息:
```shell
kubectl get replicasets
kubectl describe replicasets
```
<!--
1. Create a Service object that exposes the deployment:
-->
1. 创建一个服务对象来暴露 deployment
```shell
kubectl expose deployment hello-world --type=NodePort --name=example-service
```
<!--
1. Display information about the Service:
-->
1. 展示服务信息:
```shell
kubectl describe services example-service
```
<!--
The output is similar to this:
-->
输出类似于:
```shell
Name: example-service
Namespace: default
Labels: run=load-balancer-example
Annotations: <none>
Selector: run=load-balancer-example
Type: NodePort
IP: 10.32.0.16
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 31496/TCP
Endpoints: 10.200.1.4:8080,10.200.2.5:8080
Session Affinity: None
Events: <none>
```
<!--
Make a note of the NodePort value for the service. For example,
in the preceding output, the NodePort value is 31496.
-->
注意服务中的 NodePort 值。例如在上面的输出中NodePort 是 31496。
<!--
1. List the pods that are running the Hello World application:
-->
1. 列出运行 Hello World 应用的 pod
```shell
kubectl get pods --selector="run=load-balancer-example" --output=wide
```
<!--
The output is similar to this:
-->
输出类似于:
```shell
NAME READY STATUS ... IP NODE
hello-world-2895499144-bsbk5 1/1 Running ... 10.200.1.4 worker1
hello-world-2895499144-m1pwt 1/1 Running ... 10.200.2.5 worker2
```
<!--
1. Get the public IP address of one of your nodes that is running
a Hello World pod. How you get this address depends on how you set
up your cluster. For example, if you are using Minikube, you can
see the node address by running `kubectl cluster-info`. If you are
using Google Compute Engine instances, you can use the
`gcloud compute instances list` command to see the public addresses of your
nodes.
1. On your chosen node, create a firewall rule that allows TCP traffic
on your node port. For example, if your Service has a NodePort value of
31568, create a firewall rule that allows TCP traffic on port 31568. Different
cloud providers offer different ways of configuring firewall rules.
1. Use the node address and node port to access the Hello World application:
-->
1. 获取运行 Hello World 的 pod 的其中一个节点的公共 IP 地址。如何获得此地址取决于您设置集群的方式。
例如,如果您使用的是 Minikube则可以通过运行 `kubectl cluster-info` 来查看节点地址。
如果您使用的是 Google Compute Engine 实例,则可以使用 `gcloud compute instances list` 命令查看节点的公共地址。
1. 在您选择的节点上,创建一个防火墙规则以开放 node port 上的 TCP 流量。
例如,如果您的服务的 NodePort 值为 31568请创建一个防火墙规则以允许 31568 端口上的 TCP 流量。
不同的云提供商提供了不同方法来配置防火墙规则。
1. 使用节点地址和 node port 来访问 Hello World 应用:
```shell
curl http://<public-node-ip>:<node-port>
```
<!--
where `<public-node-ip>` is the public IP address of your node,
and `<node-port>` is the NodePort value for your service. The
response to a successful request is a hello message:
-->
这里的 `<public-node-ip>` 是您节点的公共 IP 地址,`<node-port>` 是您服务的 NodePort 值。
对于请求成功的响应是一个 hello 消息:
```shell
Hello Kubernetes!
```
<!--
## Using a service configuration file
As an alternative to using `kubectl expose`, you can use a
[service configuration file](/docs/concepts/services-networking/service/)
to create a Service.
-->
## 使用服务配置文件
作为 `kubectl expose` 的替代方法,您可以使用 [服务配置文件](/docs/concepts/services-networking/service/) 来创建服务。
{{% /capture %}}
{{% capture cleanup %}}
<!--
To delete the Service, enter this command:
-->
想要删除服务,输入以下命令:
kubectl delete services example-service
<!--
To delete the Deployment, the ReplicaSet, and the Pods that are running
the Hello World application, enter this command:
-->
想要删除运行 Hello World 应用的 Deployment、ReplicaSet 和 Pod输入以下命令
kubectl delete deployment hello-world
{{% /capture %}}
{{% capture whatsnext %}}
<!--
Learn more about
[connecting applications with services](/docs/concepts/services-networking/connect-applications-service/).
-->
学习更多关于如何 [通过服务连接应用](/docs/concepts/services-networking/connect-applications-service/)。
{{% /capture %}}