Skip to content

Commit 90b3d45

Browse files
authored
Merge pull request #43404 from windsonsea/appser
[zh] Sync connect-applications-service.md
2 parents 2c4db18 + 4ea5c84 commit 90b3d45

File tree

1 file changed

+90
-12
lines changed

1 file changed

+90
-12
lines changed

content/zh-cn/docs/tutorials/services/connect-applications-service.md

Lines changed: 90 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Create an nginx Pod, and note that it has a container port specification:
5555
我们在之前的示例中已经做过,然而让我们以网络连接的视角再重做一遍。
5656
创建一个 Nginx Pod,注意其中包含一个容器端口的规约:
5757

58-
{{< code file="service/networking/run-my-nginx.yaml" >}}
58+
{{% code_sample file="service/networking/run-my-nginx.yaml" %}}
5959

6060
<!--
6161
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
6666
kubectl apply -f ./run-my-nginx.yaml
6767
kubectl get pods -l run=my-nginx -o wide
6868
```
69+
6970
```
7071
NAME READY STATUS RESTARTS AGE IP NODE
7172
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
9091
the node, nor are there any special NAT rules to route traffic to the pod. This means
9192
you can run multiple nginx pods on the same node all using the same `containerPort`,
9293
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
9495
Node to be forwarded to backing Pods, you can - but the networking model should
9596
mean that you do not need to do so.
9697
@@ -100,18 +101,19 @@ if you're curious.
100101
-->
101102
你应该能够通过 ssh 登录到集群中的任何一个节点上,并使用诸如 `curl` 之类的工具向这两个 IP 地址发出查询请求。
102103
需要注意的是,容器 **不会** 使用该节点上的 80 端口,也不会使用任何特定的 NAT 规则去路由流量到 Pod 上。
103-
这意味着可以在同一个节点上运行多个 Nginx Pod,使用相同的 `containerPort`,并且可以从集群中任何其他的
104-
Pod 或节点上使用 IP 的方式访问到它们
104+
这意味着你可以使用相同的 `containerPort` 在同一个节点上运行多个 Nginx Pod,
105+
并且可以从集群中任何其他的 Pod 或节点上使用为 Pod 分配的 IP 地址访问到它们
105106
如果你想的话,你依然可以将宿主节点的某个端口的流量转发到 Pod 中,但是出于网络模型的原因,你不必这么做。
106107
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)。
108110
109111
<!--
110112
## Creating a Service
111113
112114
So we have pods running nginx in a flat, cluster wide, address space. In theory,
113115
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
115117
the problem a Service solves.
116118
117119
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`:
127129
128130
我们有一组在一个扁平的、集群范围的地址空间中运行 Nginx 服务的 Pod。
129131
理论上,你可以直接连接到这些 Pod,但如果某个节点死掉了会发生什么呢?
130-
Pod 会终止,Deployment 将创建新的 Pod,且使用不同的 IP。这正是 Service 要解决的问题。
132+
Pod 会终止,Deployment 内的 ReplicaSet 将创建新的 Pod,且使用不同的 IP。这正是 Service 要解决的问题。
131133
132134
Kubernetes Service 是集群中提供相同功能的一组 Pod 的抽象表达。
133135
当每个 Service 创建时,会被分配一个唯一的 IP 地址(也称为 clusterIP)。
@@ -140,6 +142,7 @@ Service 中的某些 Pod 上。
140142
```shell
141143
kubectl expose deployment/my-nginx
142144
```
145+
143146
```
144147
service/my-nginx exposed
145148
```
@@ -149,7 +152,7 @@ This is equivalent to `kubectl apply -f` the following yaml:
149152
-->
150153
这等价于使用 `kubectl create -f` 命令及如下的 yaml 文件创建:
151154
152-
{{< code file="service/networking/nginx-svc.yaml" >}}
155+
{{% code_sample file="service/networking/nginx-svc.yaml" %}}
153156
154157
<!--
155158
This specification will create a Service which targets TCP port 80 on any Pod
@@ -171,6 +174,7 @@ API 对象以了解 Service 所能接受的字段列表。
171174
```shell
172175
kubectl get svc my-nginx
173176
```
177+
174178
```
175179
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
176180
my-nginx ClusterIP 10.0.162.149 <none> 80/TCP 21s
@@ -200,6 +204,7 @@ Service Selector 将持续评估,结果被 POST
200204
```shell
201205
kubectl describe svc my-nginx
202206
```
207+
203208
```
204209
Name: my-nginx
205210
Namespace: default
@@ -217,9 +222,11 @@ Endpoints: 10.244.2.5:80,10.244.3.4:80
217222
Session Affinity: None
218223
Events: <none>
219224
```
225+
220226
```shell
221227
kubectl get endpointslices -l kubernetes.io/service-name=my-nginx
222228
```
229+
223230
```
224231
NAME ADDRESSTYPE PORTS ENDPOINTS AGE
225232
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):
275282
```shell
276283
kubectl exec my-nginx-3800858182-jr4a2 -- printenv | grep SERVICE
277284
```
285+
278286
```
279287
KUBERNETES_SERVICE_HOST=10.0.0.1
280288
KUBERNETES_SERVICE_PORT=443
@@ -286,7 +294,7 @@ Note there's no mention of your Service. This is because you created the replica
286294
before the Service. Another disadvantage of doing this is that the scheduler might
287295
put both Pods on the same machine, which will take your entire Service down if
288296
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
290298
replicas. This will give you scheduler-level Service spreading of your Pods
291299
(provided all your nodes have equal capacity), as well as the right environment
292300
variables:
@@ -299,9 +307,9 @@ variables:
299307
300308
```shell
301309
kubectl scale deployment my-nginx --replicas=0; kubectl scale deployment my-nginx --replicas=2;
302-
303310
kubectl get pods -l run=my-nginx -o wide
304311
```
312+
305313
```
306314
NAME READY STATUS RESTARTS AGE IP NODE
307315
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
316324
```shell
317325
kubectl exec my-nginx-3800858182-e9ihh -- printenv | grep SERVICE
318326
```
327+
319328
```
320329
KUBERNETES_SERVICE_PORT=443
321330
MY_NGINX_SERVICE_HOST=10.0.162.149
@@ -336,6 +345,7 @@ Kubernetes 提供了一个自动为其它 Service 分配 DNS 名字的 DNS 插
336345
```shell
337346
kubectl get services kube-dns --namespace=kube-system
338347
```
348+
339349
```
340350
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
341351
kube-dns ClusterIP 10.0.0.10 <none> 53/UDP,53/TCP 8m
@@ -362,6 +372,7 @@ IP 分配名称的 DNS 服务器。 这里我们使用 CoreDNS 集群插件(
362372
```shell
363373
kubectl run curl --image=radial/busyboxplus:curl -i --tty --rm
364374
```
375+
365376
```
366377
Waiting for pod default/curl-131556218-9fnch to be running, status is Pending, pod ready: false
367378
Hit enter for command prompt
@@ -414,12 +425,15 @@ then follow the manual steps later. In short:
414425
make keys KEY=/tmp/nginx.key CERT=/tmp/nginx.crt
415426
kubectl create secret tls nginxsecret --key /tmp/nginx.key --cert /tmp/nginx.crt
416427
```
428+
417429
```
418430
secret/nginxsecret created
419431
```
432+
420433
```shell
421434
kubectl get secrets
422435
```
436+
423437
```
424438
NAME TYPE DATA AGE
425439
nginxsecret kubernetes.io/tls 2 1m
@@ -433,17 +447,76 @@ And also the configmap:
433447
```shell
434448
kubectl create configmap nginxconfigmap --from-file=default.conf
435449
```
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+
436459
```
437460
configmap/nginxconfigmap created
438461
```
462+
439463
```shell
440464
kubectl get configmaps
441465
```
466+
442467
```
443468
NAME DATA AGE
444469
nginxconfigmap 1 114s
445470
```
446471
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+
447520
<!--
448521
Following are the manual steps to follow in case you run into problems running make (on windows for example):
449522
-->
@@ -493,6 +566,7 @@ Now create the secrets using the file:
493566
kubectl apply -f nginxsecrets.yaml
494567
kubectl get secrets
495568
```
569+
496570
```
497571
NAME TYPE DATA AGE
498572
nginxsecret kubernetes.io/tls 2 1m
@@ -504,7 +578,7 @@ in the secret, and the Service, to expose both ports (80 and 443):
504578
-->
505579
现在修改 Nginx 副本以启动一个使用 Secret 中的证书的 HTTPS 服务器以及相应的用于暴露其端口(80 和 443)的 Service:
506580
507-
{{< code file="service/networking/nginx-secure-app.yaml" >}}
581+
{{% code_sample file="service/networking/nginx-secure-app.yaml" %}}
508582
509583
<!--
510584
Noteworthy points about the nginx-secure-app manifest:
@@ -557,16 +631,18 @@ for simplicity, the pod only needs nginx.crt to access the Service):
557631
通过创建 Service,我们连接了在证书中的 CName 与在 Service 查询时被 Pod 使用的实际 DNS 名字。
558632
让我们从一个 Pod 来测试(为了方便,这里使用同一个 Secret,Pod 仅需要使用 nginx.crt 去访问 Service):
559633
560-
{{< code file="service/networking/curlpod.yaml" >}}
634+
{{% code_sample file="service/networking/curlpod.yaml" %}}
561635
562636
```shell
563637
kubectl apply -f ./curlpod.yaml
564638
kubectl get pods -l app=curlpod
565639
```
640+
566641
```
567642
NAME READY STATUS RESTARTS AGE
568643
curl-deployment-1515033274-1410r 1/1 Running 0 1m
569644
```
645+
570646
```shell
571647
kubectl exec curl-deployment-1515033274-1410r -- curl https://my-nginx --cacert /etc/nginx/ssl/tls.crt
572648
...
@@ -643,10 +719,12 @@ Change the `Type` of `my-nginx` Service from `NodePort` to `LoadBalancer`:
643719
kubectl edit svc my-nginx
644720
kubectl get svc my-nginx
645721
```
722+
646723
```
647724
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
648725
my-nginx LoadBalancer 10.0.162.149 xx.xxx.xxx.xxx 8080:30163/TCP 21s
649726
```
727+
650728
```
651729
curl https://<EXTERNAL-IP> -k
652730
...

0 commit comments

Comments
 (0)