1
1
---
2
-
3
2
title : 访问集群上运行的服务
4
3
content_type : task
5
4
---
6
5
7
6
<!-- overview -->
8
-
7
+ <!--
8
+ This page shows how to connect to services running on the Kubernetes cluster.
9
+ -->
9
10
本文展示了如何连接 Kubernetes 集群上运行的服务。
10
11
11
-
12
12
## {{% heading "prerequisites" %}}
13
13
14
-
15
14
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
16
15
17
-
18
16
<!-- steps -->
19
17
20
- ## 访问集群上运行的服务
21
-
22
-
23
- 在 Kubernetes 里, [ nodes] ( /docs/admin/node ) 、[ pods] ( /docs/user-guide/pods ) 和 [ services] ( /docs/user-guide/services ) 都有它们自己的 IP。许多情况下,集群上的 node IP、pod IP 和某些 service IP 路由不可达,所以不能从一个集群之外的节点访问它们,例如从你自己的台式机。
24
-
18
+ <!--
19
+ ## Accessing services running on the cluster
25
20
26
- ### 连接方式
27
-
28
-
29
- 你有多种从集群外连接 nodes、pods 和 services 的选项:
21
+ In Kubernetes, [nodes](/docs/concepts/architecture/nodes/),
22
+ [pods](/docs/concepts/workloads/pods/) and [services](/docs/concepts/services-networking/service/) all have
23
+ their own IPs. In many cases, the node IPs, pod IPs, and some service IPs on a cluster will not be
24
+ routable, so they will not be reachable from a machine outside the cluster,
25
+ such as your desktop machine.
26
+ -->
27
+ ## 访问集群上运行的服务
30
28
31
29
32
- - 通过公共 IP 访问 services。
33
- - 使用具有 ` NodePort ` 或 ` LoadBalancer ` 类型的 service,可以从外部访问它们。请查阅 [ services] ( /docs/user-guide/services ) 和 [ kubectl expose] ( /docs/user-guide/kubectl/v1.6/#expose ) 文档。
34
- - 取决于你的集群环境,你可以仅把 service 暴露在你的企业网络环境中,也可以将其暴露在因特网上。需要考虑暴露的 service 是否安全,它是否有自己的用户认证?
35
- - 将 pods 放置于 services 背后。如果要访问一个副本集合中特定的 pod,例如用于调试目的时,请给 pod 指定一个独特的标签并创建一个新 service 选择这个标签。
36
- - 大部分情况下,都不需要应用开发者通过节点 IP 直接访问 nodes。
37
- - 通过 Proxy Verb 访问 services、nodes 或者 pods。
38
- - 在访问 Apiserver 远程服务之前是否经过认证和授权?如果你的服务暴露到因特网中不够安全,或者需要获取 node IP 之上的端口,又或者处于调试目的时,请使用这个特性。
39
- - Proxies 可能给某些应用带来麻烦。
40
- - 仅适用于 HTTP/HTTPS。
41
- - 在[ 这里] ( #manually-constructing-apiserver-proxy-urls ) 描述
30
+ 在 Kubernetes 里,[ Node] ( /zh/docs/concepts/architecture/nodes/ ) 、
31
+ [ Pod] ( /zh/docs/concepts/workloads/pods/ ) 和
32
+ [ Service] ( /zh/docs/concepts/services-networking/services/ ) 都有自己的 IP。
33
+ 许多情况下,集群上的节点 IP、Pod IP 和某些服务 IP 是路由不可达的,
34
+ 所以不能从集群之外访问它们,例如从你自己的台式机。
35
+
36
+ <!--
37
+ ### Ways to connect
38
+
39
+ You have several options for connecting to nodes, pods and services from outside the cluster:
40
+ -->
41
+ ### 连接方式 {#ways-to-connect}
42
+
43
+ 你有多种可选方式从集群外连接节点、Pod 和服务:
44
+
45
+ <!--
46
+ - Access services through public IPs.
47
+ - Use a service with type `NodePort` or `LoadBalancer` to make the service reachable outside
48
+ the cluster. See the [services](/docs/concepts/services-networking/service/) and
49
+ [kubectl expose](/docs/reference/generated/kubectl/kubectl-commands/#expose) documentation.
50
+ - Depending on your cluster environment, this may just expose the service to your corporate network,
51
+ or it may expose it to the internet. Think about whether the service being exposed is secure.
52
+ Does it do its own authentication?
53
+ - Place pods behind services. To access one specific pod from a set of replicas, such as for debugging,
54
+ place a unique label on the pod and create a new service which selects this label.
55
+ - In most cases, it should not be necessary for application developer to directly access
56
+ nodes via their nodeIPs.
57
+ -->
58
+ - 通过公网 IP 访问服务
59
+ - 使用类型为 ` NodePort ` 或 ` LoadBalancer ` 的服务,可以从外部访问它们。
60
+ 请查阅[ 服务] ( /zh/docs/concepts/services-networking/services/ ) 和
61
+ [ kubectl expose] ( /docs/reference/generated/kubectl/kubectl-commands/#expose ) 文档。
62
+ - 取决于你的集群环境,你可以仅把服务暴露在你的企业网络环境中,也可以将其暴露在
63
+ 因特网上。需要考虑暴露的服务是否安全,它是否有自己的用户认证?
64
+ - 将 Pod 放置于服务背后。如果要访问一个副本集合中特定的 Pod,例如用于调试目的,
65
+ 请给 Pod 指定一个独特的标签并创建一个新服务选择该标签。
66
+ - 大部分情况下,都不需要应用开发者通过节点 IP 直接访问节点。
67
+ <!--
68
+ - Access services, nodes, or pods using the Proxy Verb.
69
+ - Does apiserver authentication and authorization prior to accessing the remote service.
70
+ Use this if the services are not secure enough to expose to the internet, or to gain
71
+ access to ports on the node IP, or for debugging.
72
+ - Proxies may cause problems for some web applications.
73
+ - Only works for HTTP/HTTPS.
74
+ - Described [here](#manually-constructing-apiserver-proxy-urls).
75
+ -->
76
+ - 通过 Proxy 动词访问服务、节点或者 Pod
77
+ - 在访问远程服务之前,利用 API 服务器执行身份认证和鉴权。
78
+ 如果你的服务不够安全,无法暴露到因特网中,或者需要访问节点 IP 上的端口,
79
+ 又或者出于调试目的,可使用这种方式。
80
+ - 代理可能给某些应用带来麻烦
81
+ - 此方式仅适用于 HTTP/HTTPS
82
+ - 进一步的描述在[ 这里] ( #manually-constructing-apiserver-proxy-urls )
42
83
- 从集群中的 node 或者 pod 访问。
43
- - 运行一个 pod,然后使用 [ kubectl exec] ( /docs/user-guide/kubectl/v1.6/#exec ) 连接到它的一个shell。从那个 shell 连接其他的 nodes、pods 和 services。
44
- - 某些集群可能允许你 ssh 到集群中的节点。你可能可以从那儿访问集群服务。这是一个非标准的方式,可能在一些集群上能工作,但在另一些上却不能。浏览器和其他工具可能安装或可能不会安装。集群 DNS 可能不会正常工作。
45
-
46
-
47
- ### 发现内置服务
48
-
49
-
50
- 典型情况下,kube-system 会启动集群中的几个服务。使用 ` kubectl cluster-info ` 命令获取它们的列表:
84
+ <!--
85
+ - Access from a node or pod in the cluster.
86
+ - Run a pod, and then connect to a shell in it using [kubectl exec](/docs/reference/generated/kubectl/kubectl-commands/#exec).
87
+ Connect to other nodes, pods, and services from that shell.
88
+ - Some clusters may allow you to ssh to a node in the cluster. From there you may be able to
89
+ access cluster services. This is a non-standard method, and will work on some clusters but
90
+ not others. Browsers and other tools may or may not be installed. Cluster DNS may not work.
91
+ -->
92
+ - 从集群中的一个节点或 Pod 访问
93
+ - 运行一个 Pod,然后使用
94
+ [ kubectl exec] ( /docs/reference/generated/kubectl/kubectl-commands/#exec )
95
+ 连接到它的 Shell。从那个 Shell 连接其他的节点、Pod 和 服务
96
+ - 某些集群可能允许你 SSH 到集群中的节点。你可能可以从那儿访问集群服务。
97
+ 这是一个非标准的方式,可能在一些集群上能工作,但在另一些上却不能。
98
+ 浏览器和其他工具可能已经安装也可能没有安装。集群 DNS 可能不会正常工作。
99
+
100
+ <!--
101
+ ### Discovering builtin services
102
+
103
+ Typically, there are several services which are started on a cluster by kube-system. Get a list of these
104
+ with the `kubectl cluster-info` command:
105
+ -->
106
+ ### 发现内置服务 {#discovering-builtin-services}
107
+
108
+ 典型情况下,kube-system 名字空间中会启动集群的几个服务。
109
+ 使用 ` kubectl cluster-info ` 命令获取这些服务的列表:
51
110
52
111
``` shell
53
- $ kubectl cluster-info
54
-
55
- Kubernetes master is running at https://104.197.5.247
56
- elasticsearch-logging is running at https://104.197.5.247/api/v1/namespaces/kube-system/services/elasticsearch-logging/proxy
57
- kibana-logging is running at https://104.197.5.247/api/v1/namespaces/kube-system/services/kibana-logging/proxy
58
- kube-dns is running at https://104.197.5.247/api/v1/namespaces/kube-system/services/kube-dns/proxy
59
- grafana is running at https://104.197.5.247/api/v1/namespaces/kube-system/services/monitoring-grafana/proxy
60
- heapster is running at https://104.197.5.247/api/v1/namespaces/kube-system/services/monitoring-heapster/proxy
112
+ kubectl cluster-info
61
113
```
62
114
63
- 这显示了用于访问每个服务的 proxy-verb URL。例如,这个集群启用了(使用 Elasticsearch)集群层面的日志,如果提供合适的凭据可以通过 ` https://104.197.5.247/api/v1/namespaces/kube-system/services/elasticsearch-logging/proxy/ ` 访问,或通过一个 kubectl 代理地址访问,如: ` http://localhost:8080/api/v1/namespaces/kube-system/services/elasticsearch-logging/proxy/ ` 。(请查看 [ 上文 ] ( #accessing-the-cluster-api ) 关于如何传递凭据或者使用 kubectl 代理的说明。)
64
-
65
-
66
- #### 手动构建 apiserver 代理 URLs
115
+ <!--
116
+ The output is similar to this:
117
+ -->
118
+ 输出类似于:
67
119
120
+ ```
121
+ Kubernetes master is running at https://104.197.5.247
122
+ elasticsearch-logging is running at https://104.197.5.247/api/v1/namespaces/kube-system/services/elasticsearch-logging/proxy
123
+ kibana-logging is running at https://104.197.5.247/api/v1/namespaces/kube-system/services/kibana-logging/proxy
124
+ kube-dns is running at https://104.197.5.247/api/v1/namespaces/kube-system/services/kube-dns/proxy
125
+ grafana is running at https://104.197.5.247/api/v1/namespaces/kube-system/services/monitoring-grafana/proxy
126
+ heapster is running at https://104.197.5.247/api/v1/namespaces/kube-system/services/monitoring-heapster/proxy
127
+ ```
68
128
69
- 如同上面所提到的,你可以使用 ` kubectl cluster-info ` 命令取得 service 的代理 URL。为了创建包含 service endpoints、suffixes 和 parameters 的代理 URLs,你可以简单的在 service 的代理 URL中 添加:
129
+ <!--
130
+ This shows the proxy-verb URL for accessing each service.
131
+ For example, this cluster has cluster-level logging enabled (using Elasticsearch), which can be reached
132
+ at `https://104.197.5.247/api/v1/namespaces/kube-system/services/elasticsearch-logging/proxy/` if suitable credentials are passed, or through a kubectl proxy at, for example:
133
+ `http://localhost:8080/api/v1/namespaces/kube-system/services/elasticsearch-logging/proxy/`.
134
+ -->
135
+ 这一输出显示了用 proxy 动词访问每个服务时可用的 URL。例如,此集群
136
+ (使用 Elasticsearch)启用了集群层面的日志。如果提供合适的凭据,可以通过
137
+ ` https://104.197.5.247/api/v1/namespaces/kube-system/services/elasticsearch-logging/proxy/ ` 访问,或通过一个 ` kubectl proxy ` 来访问:` http://localhost:8080/api/v1/namespaces/kube-system/services/elasticsearch-logging/proxy/ ` 。
138
+
139
+ <!--
140
+ See [Access Clusters Using the Kubernetes API](/docs/tasks/administer-cluster/access-cluster-api/#accessing-the-cluster-api) for how to pass credentials or use kubectl proxy.
141
+ -->
142
+ {{< note >}}
143
+ 请参阅[ 使用 Kubernets API 访问集群] ( /docs/tasks/administer-cluster/access-cluster-api/#accessing-the-cluster-api ) 了解如何传递凭据或如何使用 ` kubectl proxy ` 。
144
+ {{< /note >}}
145
+
146
+ <!--
147
+ #### Manually constructing apiserver proxy URLs
148
+
149
+ As mentioned above, you use the `kubectl cluster-info` command to retrieve the service's proxy URL. To create proxy URLs that include service endpoints, suffixes, and parameters, you simply append to the service's proxy URL:
150
+ `http://`*`kubernetes_master_address`*`/api/v1/namespaces/`*`namespace_name`*`/services/`*`[https:]service_name[:port_name]`*`/proxy`
151
+
152
+ If you haven't specified a name for your port, you don't have to specify *port_name* in the URL.
153
+ -->
154
+ #### 手动构建 API 服务器代理 URLs
155
+
156
+ 如前所述,你可以使用 ` kubectl cluster-info ` 命令取得服务的代理 URL。
157
+ 为了创建包含服务末端、后缀和参数的代理 URLs,你可以简单地在服务的代理 URL 中添加:
70
158
` http:// ` * ` kubernetes_master_address ` * ` /api/v1/namespaces/ ` * ` namespace_name ` * ` /services/ ` * ` service_name[:port_name] ` * ` /proxy `
71
159
72
-
73
160
如果还没有为你的端口指定名称,你可以不用在 URL 中指定 * port_name* 。
74
161
162
+ <!--
163
+ ##### Examples
75
164
165
+ * To access the Elasticsearch service endpoint `_search?q=user:kimchy`, you would use:
166
+ -->
76
167
##### 示例
77
168
169
+ * 如要访问 Elasticsearch 服务末端 ` _search?q=user:kimchy ` ,你可以使用:
170
+
171
+ ```
172
+ http://104.197.5.247/api/v1/namespaces/kube-system/services/elasticsearch-logging/proxy/_search?q=user:kimchy
173
+ ```
78
174
79
- * 你可以通过 ` http://104.197.5.247/api/v1/namespaces/kube-system/services/elasticsearch-logging/proxy/_search?q=user:kimchy ` 访问 Elasticsearch service endpoint ` _search?q=user:kimchy ` 。
80
- * 你可以通过 ` https://104.197.5.247/api/v1/namespaces/kube-system/services/elasticsearch-logging/proxy/_cluster/health?pretty=true ` 访问 Elasticsearch 集群健康信息 endpoint ` _cluster/health?pretty=true ` 。
175
+ <!--
176
+ * To access the Elasticsearch cluster health information `_cluster/health?pretty=true`, you would use:
177
+ -->
178
+ * 如要访问 Elasticsearch 集群健康信息` _cluster/health?pretty=true ` ,你会使用:
81
179
82
- ``` json
180
+ ```
181
+ https://104.197.5.247/api/v1/namespaces/kube-system/services/elasticsearch-logging/proxy/_cluster/health?pretty=true`
182
+ ```
183
+
184
+ <!--
185
+ The health information is similar to this:
186
+ -->
187
+ 健康信息与下面的例子类似:
188
+
189
+ ``` json
83
190
{
84
191
"cluster_name" : " kubernetes_logging" ,
85
192
"status" : " yellow" ,
@@ -92,18 +199,34 @@ $ kubectl cluster-info
92
199
"initializing_shards" : 0 ,
93
200
"unassigned_shards" : 5
94
201
}
95
- ```
96
-
97
-
98
- #### 通过 web 浏览器访问集群中运行的服务
99
-
100
-
101
- 你或许能够将 apiserver 代理的 url 放入浏览器的地址栏,然而:
102
-
103
-
104
- - Web 服务器不总是能够传递令牌,所以你可能需要使用基本(密码)认证。 Apiserver 可以配置为接受基本认证,但你的集群可能并没有这样配置。
105
- - 某些 web 应用可能不能工作,特别是那些使用客户端侧 javascript 的应用,它们构造 url 的方式可能不能理解代理路径前缀。
106
-
107
-
108
-
202
+ ```
203
+
204
+ <!--
205
+ * To access the *https* Elasticsearch service health information `_cluster/health?pretty=true`, you would use:
206
+ -->
207
+ * 要访问 * https* Elasticsearch 服务健康信息 ` _cluster/health?pretty=true ` ,你会使用:
208
+
209
+ ```
210
+ https://104.197.5.247/api/v1/namespaces/kube-system/services/https:elasticsearch-logging/proxy/_cluster/health?pretty=true
211
+ ```
212
+
213
+ <!--
214
+ #### Using web browsers to access services running on the cluster
215
+
216
+ You may be able to put an apiserver proxy URL into the address bar of a browser. However:
217
+ -->
218
+ #### 通过 Web 浏览器访问集群中运行的服务
219
+
220
+ 你或许能够将 API 服务器代理的 URL 放入浏览器的地址栏,然而:
221
+
222
+ <!--
223
+ - Web browsers cannot usually pass tokens, so you may need to use basic (password) auth. Apiserver can be configured to accept basic auth,
224
+ but your cluster may not be configured to accept basic auth.
225
+ - Some web apps may not work, particularly those with client side javascript that construct URLs in a
226
+ way that is unaware of the proxy path prefix.
227
+ -->
228
+ - Web 服务器通常不能传递令牌,所以你可能需要使用基本(密码)认证。
229
+ API 服务器可以配置为接受基本认证,但你的集群可能并没有这样配置。
230
+ - 某些 Web 应用可能无法工作,特别是那些使用客户端 Javascript 构造 URL 的
231
+ 应用,所构造的 URL 可能并不支持代理路径前缀。
109
232
0 commit comments