@@ -3,13 +3,10 @@ title: 公开外部 IP 地址以访问集群中应用程序
3
3
content_type : tutorial
4
4
weight : 10
5
5
---
6
-
7
6
<!--
8
- ---
9
7
title: Exposing an External IP Address to Access an Application in a Cluster
10
8
content_type: tutorial
11
9
weight: 10
12
- ---
13
10
-->
14
11
15
12
<!-- overview -->
@@ -20,12 +17,8 @@ external IP address.
20
17
-->
21
18
此页面显示如何创建公开外部 IP 地址的 Kubernetes 服务对象。
22
19
23
-
24
-
25
-
26
20
## {{% heading "prerequisites" %}}
27
21
28
-
29
22
<!--
30
23
* Install [kubectl](/docs/tasks/tools/install-kubectl/).
31
24
@@ -40,30 +33,23 @@ external IP address.
40
33
41
34
* 安装 [ kubectl] ( /zh/docs/tasks/tools/install-kubectl/ ) .
42
35
43
- * 使用 Google Kubernetes Engine 或 Amazon Web Services 等云供应商创建 Kubernetes 群集。
44
- 本教程创建了一个[ 外部负载均衡器] ( /zh/docs/tasks/access-application-cluster/create-external-load-balancer/ ) ,需要云供应商。
36
+ * 使用 Google Kubernetes Engine 或 Amazon Web Services 等云供应商创建 Kubernetes 集群。
37
+ 本教程创建了一个[ 外部负载均衡器] ( /zh/docs/tasks/access-application-cluster/create-external-load-balancer/ ) ,
38
+ 需要云供应商。
45
39
46
40
* 配置 ` kubectl ` 与 Kubernetes API 服务器通信。有关说明,请参阅云供应商文档。
47
41
48
-
49
-
50
-
51
42
## {{% heading "objectives" %}}
52
43
53
-
54
44
<!--
55
45
* Run five instances of a Hello World application.
56
46
* Create a Service object that exposes an external IP address.
57
47
* Use the Service object to access the running application.
58
48
-->
59
-
60
49
* 运行 Hello World 应用程序的五个实例。
61
50
* 创建一个公开外部 IP 地址的 Service 对象。
62
51
* 使用 Service 对象访问正在运行的应用程序。
63
52
64
-
65
-
66
-
67
53
<!-- lessoncontent -->
68
54
69
55
<!--
@@ -77,118 +63,130 @@ external IP address.
77
63
-->
78
64
1 . 在集群中运行 Hello World 应用程序:
79
65
80
- {{< codenew file="service/load-balancer-example.yaml" >}}
81
-
82
- ``` shell
83
- kubectl apply -f https://k8s.io/examples/service/load-balancer-example.yaml
84
- ```
85
-
86
- <!--
87
- The preceding command creates a
88
- [Deployment](/docs/concepts/workloads/controllers/deployment/)
89
- object and an associated
90
- [ReplicaSet](/docs/concepts/workloads/controllers/replicaset/)
91
- object. The ReplicaSet has five
92
- [Pods](/docs/concepts/workloads/pods/pod/),
93
- each of which runs the Hello World application.
94
- -->
95
- 前面的命令创建一个 [ Deployment] ( /zh/docs/concepts/workloads/controllers/deployment/ )
96
- 对象和一个关联的 [ ReplicaSet] ( /zh/docs/concepts/workloads/controllers/replicaset/ ) 对象。
97
- ReplicaSet 有五个 [ Pod] ( /zh/docs/concepts/workloads/pods/pod/ ) ,每个都运行 Hello World 应用程序。
66
+ {{< codenew file="service/load-balancer-example.yaml" >}}
67
+
68
+ ``` shell
69
+ kubectl apply -f https://k8s.io/examples/service/load-balancer-example.yaml
70
+ ```
71
+
72
+ <!--
73
+ The preceding command creates a
74
+ {{< glossary_tooltip text="Deployment" term_id="deployment" >}}
75
+ and an associated
76
+ {{< glossary_tooltip term_id="replica-set" text="ReplicaSet" >}}.
77
+ The ReplicaSet has five
78
+ {{< glossary_tooltip text="Pods" term_id="pod" >}}
79
+ each of which runs the Hello World application.
80
+ -->
81
+ 前面的命令创建一个
82
+ {{< glossary_tooltip text="Deployment" term_id="deployment" >}}
83
+ 对象和一个关联的
84
+ {{< glossary_tooltip term_id="replica-set" text="ReplicaSet" >}} 对象。
85
+ ReplicaSet 有五个 {{< glossary_tooltip text="Pods" term_id="pod" >}},
86
+ 每个都运行 Hello World 应用程序。
98
87
99
88
<!--
100
89
1. Display information about the Deployment:
101
90
-->
102
91
2 . 显示有关 Deployment 的信息:
103
92
104
- kubectl get deployments hello-world
105
- kubectl describe deployments hello-world
93
+ ``` shell
94
+ kubectl get deployments hello-world
95
+ kubectl describe deployments hello-world
96
+ ```
106
97
107
98
<!--
108
99
1. Display information about your ReplicaSet objects:
109
100
-->
110
101
3 . 显示有关 ReplicaSet 对象的信息:
111
102
112
- kubectl get replicasets
113
- kubectl describe replicasets
103
+ ``` shell
104
+ kubectl get replicasets
105
+ kubectl describe replicasets
106
+ ```
114
107
115
108
<!--
116
109
1. Create a Service object that exposes the deployment:
117
110
-->
118
- 4 . 创建公开 deployment 的 Service 对象:
111
+ 4 . 创建公开 Deployment 的 Service 对象:
119
112
120
- kubectl expose deployment hello-world --type=LoadBalancer --name=my-service
113
+ ``` shell
114
+ kubectl expose deployment hello-world --type=LoadBalancer --name=my-service
115
+ ```
121
116
122
117
<!--
123
118
1. Display information about the Service:
124
119
-->
125
120
5 . 显示有关 Service 的信息:
126
121
127
- kubectl get services my-service
122
+ ``` shell
123
+ kubectl get services my-service
124
+ ```
128
125
129
- <!--
126
+ <!--
130
127
The output is similar to this:
131
- -->
128
+ -->
132
129
输出类似于:
133
130
134
- NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
135
- my-service ClusterIP 10.3.245.137 104.198.205.71 8080/TCP 54s
136
-
137
- <!--
138
- {{< note >}}
139
-
140
- The `type=LoadBalancer` service is backed by external cloud providers, which is not covered in this example, please refer to [this page](/docs/concepts/services-networking/service/#loadbalancer) for the details.
141
-
142
- {{< /note >}}
143
- -->
144
- 注意:` type=LoadBalancer ` 服务由外部云服务提供商提供支持,本例中不包含此部分,详细信息请参考[ 此页] ( /docs/concepts/services-networking/service/#loadbalancer )
145
-
146
- <!--
147
- {{< note >}}
148
-
149
- If the external IP address is shown as \<pending\>, wait for a minute and enter the same command again.
150
-
151
- {{< /note >}}
152
- -->
153
- 注意:如果外部 IP 地址显示为 \< pending\> ,请等待一分钟再次输入相同的命令。
131
+ ```
132
+ NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
133
+ my-service ClusterIP 10.3.245.137 104.198.205.71 8080/TCP 54s
134
+ ```
135
+
136
+ <!--
137
+ {{< note >}}
138
+ The `type=LoadBalancer` service is backed by external cloud providers, which is not covered in this example, please refer to [this page](/docs/concepts/services-networking/service/#loadbalancer) for the details.
139
+ {{< /note >}}
140
+ -->
141
+ 提示:` type=LoadBalancer ` 服务由外部云服务提供商提供支持,本例中不包含此部分,
142
+ 详细信息请参考[ 此页] ( /docs/concepts/services-networking/service/#loadbalancer )
143
+
144
+ <!--
145
+ {{< note >}}
146
+ If the external IP address is shown as \<pending\>, wait for a minute and enter the same command again.
147
+ {{< /note >}}
148
+ -->
149
+ 提示:如果外部 IP 地址显示为 \< pending\> ,请等待一分钟再次输入相同的命令。
154
150
155
151
<!--
156
152
1. Display detailed information about the Service:
157
153
-->
158
154
6 . 显示有关 Service 的详细信息:
159
155
160
- kubectl describe services my-service
156
+ ``` shell
157
+ kubectl describe services my-service
158
+ ```
161
159
162
- <!--
160
+ <!--
163
161
The output is similar to this:
164
- -->
162
+ -->
165
163
输出类似于:
166
164
167
- Name: my-service
168
- Namespace: default
169
- Labels: run=load-balancer-example
170
- Annotations: <none>
171
- Selector: run=load-balancer-example
172
- Type: LoadBalancer
173
- IP: 10.3.245.137
174
- LoadBalancer Ingress: 104.198.205.71
175
- Port: <unset> 8080/TCP
176
- NodePort: <unset> 32377/TCP
177
- Endpoints: 10.0.0.6:8080,10.0.1.6:8080,10.0.1.7:8080 + 2 more...
178
- Session Affinity: None
179
- Events: <none>
180
-
181
- <!--
165
+ ```
166
+ Name: my-service
167
+ Namespace: default
168
+ Labels: run=load-balancer-example
169
+ Annotations: <none>
170
+ Selector: run=load-balancer-example
171
+ Type: LoadBalancer
172
+ IP: 10.3.245.137
173
+ LoadBalancer Ingress: 104.198.205.71
174
+ Port: <unset> 8080/TCP
175
+ NodePort: <unset> 32377/TCP
176
+ Endpoints: 10.0.0.6:8080,10.0.1.6:8080,10.0.1.7:8080 + 2 more...
177
+ Session Affinity: None
178
+ Events: <none>
179
+ ```
180
+ <!--
182
181
Make a note of the external IP address (`LoadBalancer Ingress`) exposed by
183
182
your service. In this example, the external IP address is 104.198.205.71.
184
183
Also note the value of `Port` and `NodePort`. In this example, the `Port`
185
184
is 8080 and the `NodePort` is 32377.
186
- -->
185
+ -->
187
186
记下服务公开的外部 IP 地址(` LoadBalancer Ingress ` )。
188
187
在本例中,外部 IP 地址是 104.198.205.71。还要注意 ` Port ` 和 ` NodePort ` 的值。
189
188
在本例中,` Port ` 是 8080,` NodePort ` 是32377。
190
189
191
-
192
190
<!--
193
191
1. In the preceding output, you can see that the service has several endpoints:
194
192
10.0.0.6:8080,10.0.1.6:8080,10.0.1.7:8080 + 2 more. These are internal
@@ -200,78 +198,79 @@ If the external IP address is shown as \<pending\>, wait for a minute and enter
200
198
这些都是正在运行 Hello World 应用程序的 pod 的内部地址。
201
199
要验证这些是 pod 地址,请输入以下命令:
202
200
203
- kubectl get pods --output=wide
201
+ ``` shell
202
+ kubectl get pods --output=wide
203
+ ```
204
204
205
- <!--
205
+ <!--
206
206
The output is similar to this:
207
- -->
207
+ -->
208
208
输出类似于:
209
209
210
- NAME ... IP NODE
211
- hello-world-2895499144-1jaz9 ... 10.0.1.6 gke-cluster-1-default-pool-e0b8d269-1afc
212
- hello-world-2895499144-2e5uh ... 10.0.1.8 gke-cluster-1-default-pool-e0b8d269-1afc
213
- hello-world-2895499144-9m4h1 ... 10.0.0.6 gke-cluster-1-default-pool-e0b8d269-5v7a
214
- hello-world-2895499144-o4z13 ... 10.0.1.7 gke-cluster-1-default-pool-e0b8d269-1afc
215
- hello-world-2895499144-segjf ... 10.0.2.5 gke-cluster-1-default-pool-e0b8d269-cpuc
216
-
210
+ ```
211
+ NAME ... IP NODE
212
+ hello-world-2895499144-1jaz9 ... 10.0.1.6 gke-cluster-1-default-pool-e0b8d269-1afc
213
+ hello-world-2895499144-2e5uh ... 10.0.1.8 gke-cluster-1-default-pool-e0b8d269-1afc
214
+ hello-world-2895499144-9m4h1 ... 10.0.0.6 gke-cluster-1-default-pool-e0b8d269-5v7a
215
+ hello-world-2895499144-o4z13 ... 10.0.1.7 gke-cluster-1-default-pool-e0b8d269-1afc
216
+ hello-world-2895499144-segjf ... 10.0.2.5 gke-cluster-1-default-pool-e0b8d269-cpuc
217
+ ```
217
218
<!--
218
219
1. Use the external IP address (`LoadBalancer Ingress`) to access the Hello
219
220
World application:
220
221
-->
221
222
8 . 使用外部 IP 地址(` LoadBalancer Ingress ` )访问 Hello World 应用程序:
222
223
223
- curl http://<external-ip>:<port>
224
+ ``` shell
225
+ curl http://< external-ip> :< port>
226
+ ```
224
227
225
- <!--
228
+ <!--
226
229
where `<external-ip>` is the external IP address (`LoadBalancer Ingress`)
227
230
of your Service, and `<port>` is the value of `Port` in your Service
228
231
description.
229
232
If you are using minikube, typing `minikube service my-service` will
230
233
automatically open the Hello World application in a browser.
231
- -->
234
+ -->
232
235
其中 ` <external-ip> ` 是您的服务的外部 IP 地址(` LoadBalancer Ingress ` ),
233
236
` <port> ` 是您的服务描述中的 ` port ` 的值。
234
237
如果您正在使用 minikube,输入 ` minikube service my-service ` 将在浏览器中自动打开 Hello World 应用程序。
235
238
236
- <!--
239
+ <!--
237
240
The response to a successful request is a hello message:
238
- -->
241
+ -->
239
242
成功请求的响应是一条问候消息:
240
243
241
- Hello Kubernetes!
242
-
243
-
244
-
244
+ ```
245
+ Hello Kubernetes!
246
+ ```
245
247
246
248
## {{% heading "cleanup" %}}
247
249
248
-
249
250
<!--
250
251
To delete the Service, enter this command:
251
252
-->
252
253
要删除服务,请输入以下命令:
253
254
254
- kubectl delete services my-service
255
+ ``` shell
256
+ kubectl delete services my-service
257
+ ```
255
258
256
259
<!--
257
260
To delete the Deployment, the ReplicaSet, and the Pods that are running
258
261
the Hello World application, enter this command:
259
262
-->
260
263
要删除正在运行 Hello World 应用程序的 Deployment,ReplicaSet 和 Pod,请输入以下命令:
261
264
262
- kubectl delete deployment hello-world
263
-
264
-
265
-
265
+ ``` shell
266
+ kubectl delete deployment hello-world
267
+ ```
266
268
267
269
## {{% heading "whatsnext" %}}
268
270
269
-
270
271
<!--
271
272
Learn more about
272
273
[connecting applications with services](/docs/concepts/services-networking/connect-applications-service/).
273
274
-->
274
-
275
- 了解更多关于[ 将应用程序与服务连接] ( /zh/docs/concepts/services-networking/connect-applications-service/ ) 。
276
-
275
+ 进一步了解[ 将应用程序与服务连接] ( /zh/docs/concepts/services-networking/connect-applications-service/ ) 。
277
276
0 commit comments