@@ -55,7 +55,7 @@ Create an nginx Pod, and note that it has a container port specification:
55
55
我们在之前的示例中已经做过,然而让我们以网络连接的视角再重做一遍。
56
56
创建一个 Nginx Pod,注意其中包含一个容器端口的规约:
57
57
58
- {{< code file="service/networking/run-my-nginx.yaml" > }}
58
+ {{% code_sample file="service/networking/run-my-nginx.yaml" % }}
59
59
60
60
<!--
61
61
This makes it accessible from any node in your cluster. Check the nodes the Pod is running on:
@@ -66,6 +66,7 @@ This makes it accessible from any node in your cluster. Check the nodes the Pod
66
66
kubectl apply -f ./run-my-nginx.yaml
67
67
kubectl get pods -l run=my-nginx -o wide
68
68
```
69
+
69
70
```
70
71
NAME READY STATUS RESTARTS AGE IP NODE
71
72
my-nginx-3800858182-jr4a2 1/1 Running 0 13s 10.244.3.4 kubernetes-minion-905m
@@ -90,7 +91,7 @@ to make queries against both IPs. Note that the containers are *not* using port
90
91
the node, nor are there any special NAT rules to route traffic to the pod. This means
91
92
you can run multiple nginx pods on the same node all using the same ` containerPort` ,
92
93
and access them from any other pod or node in your cluster using the assigned IP
93
- address for the Service . If you want to arrange for a specific port on the host
94
+ address for the pod . If you want to arrange for a specific port on the host
94
95
Node to be forwarded to backing Pods, you can - but the networking model should
95
96
mean that you do not need to do so.
96
97
@@ -100,18 +101,19 @@ if you're curious.
100
101
-->
101
102
你应该能够通过 ssh 登录到集群中的任何一个节点上,并使用诸如 `curl` 之类的工具向这两个 IP 地址发出查询请求。
102
103
需要注意的是,容器 **不会** 使用该节点上的 80 端口,也不会使用任何特定的 NAT 规则去路由流量到 Pod 上。
103
- 这意味着可以在同一个节点上运行多个 Nginx Pod,使用相同的 `containerPort`,并且可以从集群中任何其他的
104
- Pod 或节点上使用 IP 的方式访问到它们 。
104
+ 这意味着你可以使用相同的 `containerPort` 在同一个节点上运行多个 Nginx Pod,
105
+ 并且可以从集群中任何其他的 Pod 或节点上使用为 Pod 分配的 IP 地址访问到它们 。
105
106
如果你想的话,你依然可以将宿主节点的某个端口的流量转发到 Pod 中,但是出于网络模型的原因,你不必这么做。
106
107
107
- 如果对此好奇,请参考 [Kubernetes 网络模型](/zh-cn/docs/concepts/cluster-administration/networking/#the-kubernetes-network-model)。
108
+ 如果对此好奇,请参考
109
+ [Kubernetes 网络模型](/zh-cn/docs/concepts/cluster-administration/networking/#the-kubernetes-network-model)。
108
110
109
111
<!--
110
112
## Creating a Service
111
113
112
114
So we have pods running nginx in a flat, cluster wide, address space. In theory,
113
115
you could talk to these pods directly, but what happens when a node dies? The pods
114
- die with it, and the Deployment will create new ones, with different IPs. This is
116
+ die with it, and the ReplicaSet inside the Deployment will create new ones, with different IPs. This is
115
117
the problem a Service solves.
116
118
117
119
A Kubernetes Service is an abstraction which defines a logical set of Pods running
@@ -127,7 +129,7 @@ You can create a Service for your 2 nginx replicas with `kubectl expose`:
127
129
128
130
我们有一组在一个扁平的、集群范围的地址空间中运行 Nginx 服务的 Pod。
129
131
理论上,你可以直接连接到这些 Pod,但如果某个节点死掉了会发生什么呢?
130
- Pod 会终止,Deployment 将创建新的 Pod,且使用不同的 IP。这正是 Service 要解决的问题。
132
+ Pod 会终止,Deployment 内的 ReplicaSet 将创建新的 Pod,且使用不同的 IP。这正是 Service 要解决的问题。
131
133
132
134
Kubernetes Service 是集群中提供相同功能的一组 Pod 的抽象表达。
133
135
当每个 Service 创建时,会被分配一个唯一的 IP 地址(也称为 clusterIP)。
@@ -140,6 +142,7 @@ Service 中的某些 Pod 上。
140
142
```shell
141
143
kubectl expose deployment/my-nginx
142
144
```
145
+
143
146
```
144
147
service/my-nginx exposed
145
148
```
@@ -149,7 +152,7 @@ This is equivalent to `kubectl apply -f` the following yaml:
149
152
-->
150
153
这等价于使用 `kubectl create -f` 命令及如下的 yaml 文件创建:
151
154
152
- {{< code file="service/networking/nginx-svc.yaml" > }}
155
+ {{% code_sample file="service/networking/nginx-svc.yaml" % }}
153
156
154
157
<!--
155
158
This specification will create a Service which targets TCP port 80 on any Pod
@@ -171,6 +174,7 @@ API 对象以了解 Service 所能接受的字段列表。
171
174
```shell
172
175
kubectl get svc my-nginx
173
176
```
177
+
174
178
```
175
179
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
176
180
my-nginx ClusterIP 10.0.162.149 <none> 80/TCP 21s
@@ -200,6 +204,7 @@ Service Selector 将持续评估,结果被 POST
200
204
```shell
201
205
kubectl describe svc my-nginx
202
206
```
207
+
203
208
```
204
209
Name: my-nginx
205
210
Namespace: default
@@ -217,9 +222,11 @@ Endpoints: 10.244.2.5:80,10.244.3.4:80
217
222
Session Affinity: None
218
223
Events: <none>
219
224
```
225
+
220
226
```shell
221
227
kubectl get endpointslices -l kubernetes.io/service-name=my-nginx
222
228
```
229
+
223
230
```
224
231
NAME ADDRESSTYPE PORTS ENDPOINTS AGE
225
232
my-nginx-7vzhx IPv4 80 10.244.2.5,10.244.3.4 21s
@@ -275,6 +282,7 @@ the environment of your running nginx Pods (your Pod name will be different):
275
282
` ` ` shell
276
283
kubectl exec my-nginx-3800858182-jr4a2 -- printenv | grep SERVICE
277
284
` ` `
285
+
278
286
` ` `
279
287
KUBERNETES_SERVICE_HOST=10.0.0.1
280
288
KUBERNETES_SERVICE_PORT=443
@@ -286,7 +294,7 @@ Note there's no mention of your Service. This is because you created the replica
286
294
before the Service. Another disadvantage of doing this is that the scheduler might
287
295
put both Pods on the same machine, which will take your entire Service down if
288
296
it dies. We can do this the right way by killing the 2 Pods and waiting for the
289
- Deployment to recreate them. This time around the Service exists *before* the
297
+ Deployment to recreate them. This time the Service exists *before* the
290
298
replicas. This will give you scheduler-level Service spreading of your Pods
291
299
(provided all your nodes have equal capacity), as well as the right environment
292
300
variables:
@@ -299,9 +307,9 @@ variables:
299
307
300
308
```shell
301
309
kubectl scale deployment my-nginx --replicas=0; kubectl scale deployment my-nginx --replicas=2;
302
-
303
310
kubectl get pods -l run=my-nginx -o wide
304
311
```
312
+
305
313
```
306
314
NAME READY STATUS RESTARTS AGE IP NODE
307
315
my-nginx-3800858182-e9ihh 1/1 Running 0 5s 10.244.2.7 kubernetes-minion-ljyd
@@ -316,6 +324,7 @@ You may notice that the pods have different names, since they are killed and rec
316
324
```shell
317
325
kubectl exec my-nginx-3800858182-e9ihh -- printenv | grep SERVICE
318
326
```
327
+
319
328
```
320
329
KUBERNETES_SERVICE_PORT=443
321
330
MY_NGINX_SERVICE_HOST=10.0.162.149
@@ -336,6 +345,7 @@ Kubernetes 提供了一个自动为其它 Service 分配 DNS 名字的 DNS 插
336
345
` ` ` shell
337
346
kubectl get services kube-dns --namespace=kube-system
338
347
` ` `
348
+
339
349
` ` `
340
350
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
341
351
kube-dns ClusterIP 10.0.0.10 < none> 53/UDP,53/TCP 8m
@@ -362,6 +372,7 @@ IP 分配名称的 DNS 服务器。 这里我们使用 CoreDNS 集群插件(
362
372
` ` ` shell
363
373
kubectl run curl --image=radial/busyboxplus:curl -i --tty --rm
364
374
` ` `
375
+
365
376
` ` `
366
377
Waiting for pod default/curl-131556218-9fnch to be running, status is Pending, pod ready: false
367
378
Hit enter for command prompt
@@ -414,12 +425,15 @@ then follow the manual steps later. In short:
414
425
make keys KEY=/tmp/nginx.key CERT=/tmp/nginx.crt
415
426
kubectl create secret tls nginxsecret --key /tmp/nginx.key --cert /tmp/nginx.crt
416
427
```
428
+
417
429
```
418
430
secret/nginxsecret created
419
431
```
432
+
420
433
```shell
421
434
kubectl get secrets
422
435
```
436
+
423
437
```
424
438
NAME TYPE DATA AGE
425
439
nginxsecret kubernetes.io/tls 2 1m
@@ -433,17 +447,76 @@ And also the configmap:
433
447
```shell
434
448
kubectl create configmap nginxconfigmap --from-file=default.conf
435
449
```
450
+
451
+ <!--
452
+ You can find an example for `default.conf` in
453
+ [the Kubernetes examples project repo](https://github.com/kubernetes/examples/tree/bc9ca4ca32bb28762ef216386934bef20f1f9930/staging/https-nginx/).
454
+ -->
455
+ 你可以在
456
+ [Kubernetes examples 项目代码仓库](https://github.com/kubernetes/examples/tree/bc9ca4ca32bb28762ef216386934bef20f1f9930/staging/https-nginx/)中找到
457
+ `default.conf` 示例。
458
+
436
459
```
437
460
configmap/nginxconfigmap created
438
461
```
462
+
439
463
```shell
440
464
kubectl get configmaps
441
465
```
466
+
442
467
```
443
468
NAME DATA AGE
444
469
nginxconfigmap 1 114s
445
470
```
446
471
472
+ <!--
473
+ You can view the details of the `nginxconfigmap` ConfigMap using the following command:
474
+ -->
475
+ 你可以使用以下命令来查看 `nginxconfigmap` ConfigMap 的细节:
476
+
477
+ ```shell
478
+ kubectl describe configmap nginxconfigmap
479
+ ```
480
+
481
+ <!--
482
+ The output is similar to:
483
+ -->
484
+ 输出类似于:
485
+
486
+ ```console
487
+ Name: nginxconfigmap
488
+ Namespace: default
489
+ Labels: <none>
490
+ Annotations: <none>
491
+
492
+ Data
493
+ ====
494
+ default.conf:
495
+ ----
496
+ server {
497
+ listen 80 default_server;
498
+ listen [::]:80 default_server ipv6only=on;
499
+
500
+ listen 443 ssl;
501
+
502
+ root /usr/share/nginx/html;
503
+ index index.html;
504
+
505
+ server_name localhost;
506
+ ssl_certificate /etc/nginx/ssl/tls.crt;
507
+ ssl_certificate_key /etc/nginx/ssl/tls.key;
508
+
509
+ location / {
510
+ try_files $uri $uri/ =404;
511
+ }
512
+ }
513
+
514
+ BinaryData
515
+ ====
516
+
517
+ Events: <none>
518
+ ```
519
+
447
520
<!--
448
521
Following are the manual steps to follow in case you run into problems running make (on windows for example):
449
522
-->
@@ -493,6 +566,7 @@ Now create the secrets using the file:
493
566
kubectl apply -f nginxsecrets.yaml
494
567
kubectl get secrets
495
568
```
569
+
496
570
```
497
571
NAME TYPE DATA AGE
498
572
nginxsecret kubernetes.io/tls 2 1m
@@ -504,7 +578,7 @@ in the secret, and the Service, to expose both ports (80 and 443):
504
578
-->
505
579
现在修改 Nginx 副本以启动一个使用 Secret 中的证书的 HTTPS 服务器以及相应的用于暴露其端口(80 和 443)的 Service:
506
580
507
- {{< code file="service/networking/nginx-secure-app.yaml" > }}
581
+ {{% code_sample file="service/networking/nginx-secure-app.yaml" % }}
508
582
509
583
<!--
510
584
Noteworthy points about the nginx-secure-app manifest:
@@ -557,16 +631,18 @@ for simplicity, the pod only needs nginx.crt to access the Service):
557
631
通过创建 Service,我们连接了在证书中的 CName 与在 Service 查询时被 Pod 使用的实际 DNS 名字。
558
632
让我们从一个 Pod 来测试(为了方便,这里使用同一个 Secret,Pod 仅需要使用 nginx.crt 去访问 Service):
559
633
560
- {{< code file="service/networking/curlpod.yaml" > }}
634
+ {{% code_sample file="service/networking/curlpod.yaml" % }}
561
635
562
636
```shell
563
637
kubectl apply -f ./curlpod.yaml
564
638
kubectl get pods -l app=curlpod
565
639
```
640
+
566
641
```
567
642
NAME READY STATUS RESTARTS AGE
568
643
curl-deployment-1515033274-1410r 1/1 Running 0 1m
569
644
```
645
+
570
646
```shell
571
647
kubectl exec curl-deployment-1515033274-1410r -- curl https://my-nginx --cacert /etc/nginx/ssl/tls.crt
572
648
...
@@ -643,10 +719,12 @@ Change the `Type` of `my-nginx` Service from `NodePort` to `LoadBalancer`:
643
719
kubectl edit svc my-nginx
644
720
kubectl get svc my-nginx
645
721
` ` `
722
+
646
723
` ` `
647
724
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
648
725
my-nginx LoadBalancer 10.0.162.149 xx.xxx.xxx.xxx 8080:30163/TCP 21s
649
726
` ` `
727
+
650
728
` ` `
651
729
curl https://< EXTERNAL-IP> -k
652
730
...
0 commit comments