Skip to content

Commit 749647c

Browse files
authored
Merge pull request #22638 from tengqm/zh-network-policy
[zh] Resync network-policy task
2 parents 6ff79d8 + d9215a1 commit 749647c

File tree

1 file changed

+145
-70
lines changed

1 file changed

+145
-70
lines changed

content/zh/docs/tasks/administer-cluster/declare-network-policy.md

Lines changed: 145 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,93 @@
11
---
2-
approvers:
3-
- caseydavenport
4-
- danwinship
52
title: 声明网络策略
63
content_type: task
74
---
5+
<!--
6+
reviewers:
7+
- caseydavenport
8+
- danwinship
9+
title: Declare Network Policy
10+
min-kubernetes-server-version: v1.8
11+
content_type: task
12+
-->
813

914
<!-- overview -->
1015

11-
12-
本文可以帮助您开始使用 Kubernetes 的 [NetworkPolicy API](/docs/concepts/services-networking/network-policies/) 声明网络策略去管理 Pod 之间的通信
13-
14-
16+
<!--
17+
This document helps you get started using the Kubernetes [NetworkPolicy API](/docs/concepts/services-networking/network-policies/) to declare network policies that govern how pods communicate with each other.
18+
-->
19+
本文可以帮助您开始使用 Kubernetes 的 [NetworkPolicy API](/zh/docs/concepts/services-networking/network-policies/) 声明网络策略去管理 Pod 之间的通信
1520

1621
## {{% heading "prerequisites" %}}
1722

23+
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
1824

25+
<!--
26+
Make sure you've configured a network provider with network policy support. There are a number of network providers that support NetworkPolicy, including:
1927
28+
* [Calico](/docs/tasks/administer-cluster/network-policy-provider/calico-network-policy/)
29+
* [Cilium](/docs/tasks/administer-cluster/network-policy-provider/cilium-network-policy/)
30+
* [Kube-router](/docs/tasks/administer-cluster/network-policy-provider/kube-router-network-policy/)
31+
* [Romana](/docs/tasks/administer-cluster/network-policy-provider/romana-network-policy/)
32+
* [Weave Net](/docs/tasks/administer-cluster/network-policy-provider/weave-network-policy/)
33+
-->
2034
您首先需要有一个支持网络策略的 Kubernetes 集群。已经有许多支持 NetworkPolicy 的网络提供商,包括:
2135

22-
* [Calico](/docs/tasks/configure-pod-container/calico-network-policy/)
23-
* [Romana](/docs/tasks/configure-pod-container/romana-network-policy/)
24-
* [Weave 网络](/docs/tasks/configure-pod-container/weave-network-policy/)
25-
26-
27-
**注意**:以上列表是根据产品名称按字母顺序排序,而不是按推荐或偏好排序。下面示例对于使用了上面任何提供商的 Kubernetes 集群都是有效的
28-
36+
* [Calico](/zh/docs/tasks/configure-pod-container/calico-network-policy/)
37+
* [Cilium](/zh/docs/tasks/administer-cluster/network-policy-provider/cilium-network-policy/)
38+
* [Kube-router](/zh/docs/tasks/administer-cluster/network-policy-provider/kube-router-network-policy/)
39+
* [Romana](/zh/docs/tasks/configure-pod-container/romana-network-policy/)
40+
* [Weave 网络](/zh/docs/tasks/configure-pod-container/weave-network-policy/)
2941

42+
<!--
43+
The above list is sorted alphabetically by product name, not by recommendation or preference. This example is valid for a Kubernetes cluster using any of these providers.
44+
-->
45+
{{< note >}}
46+
以上列表是根据产品名称按字母顺序排序,而不是按推荐或偏好排序。
47+
下面示例对于使用了上面任何提供商的 Kubernetes 集群都是有效的
48+
{{< /note >}}
3049

3150
<!-- steps -->
3251

52+
<!--
53+
## Create an `nginx` deployment and expose it via a service
3354
34-
## 创建一个`nginx` deployment 并且通过服务将其暴露
35-
55+
To see how Kubernetes network policy works, start off by creating an `nginx` Deployment.
56+
-->
57+
## 创建一个`nginx` Deployment 并且通过服务将其暴露
3658

3759
为了查看 Kubernetes 网络策略是怎样工作的,可以从创建一个`nginx` deployment 并且通过服务将其暴露开始
3860

3961
```console
40-
$ kubectl create deployment nginx --image=nginx
62+
kubectl create deployment nginx --image=nginx
63+
```
64+
```none
4165
deployment "nginx" created
42-
$ kubectl expose deployment nginx --port=80
43-
service "nginx" exposed
4466
```
4567

68+
<!--
69+
Expose the Deployment through a Service called `nginx`.
70+
-->
71+
将此 Deployment 以名为 `nginx` 的 Service 暴露出来:
72+
73+
```console
74+
kubectl expose deployment nginx --port=80
75+
```
76+
```none
77+
service "nginx" exposed
78+
```
4679

47-
在 default 命名空间下运行了两个 `nginx` pod,而且通过一个名字为 `nginx` 的服务进行了暴露
80+
<!--
81+
The above commands create a Deployment with an nginx Pod and expose the Deployment through a Service named `nginx`. The `nginx` Pod and Deployment are found in the `default` namespace.
82+
-->
83+
上述命令创建了一个带有一个 nginx 的 Deployment,并将之通过名为 `nginx`
84+
Service 暴露出来。名为 `nginx` 的 Pod 和 Deployment 都位于 `default`
85+
名字空间内。
4886

4987
```console
50-
$ kubectl get svc,pod
88+
kubectl get svc,pod
89+
```
90+
```none
5191
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
5292
svc/kubernetes 10.100.0.1 <none> 443/TCP 46m
5393
svc/nginx 10.100.0.16 <none> 80/TCP 33s
@@ -56,93 +96,128 @@ NAME READY STATUS RESTARTS AGE
5696
po/nginx-701339712-e0qfq 1/1 Running 0 35s
5797
```
5898

99+
<!--
100+
## Test the service by accessing it from another Pod
59101
60-
## 测试服务能够被其它的 pod 访问
61-
62-
63-
您应该可以从其它的 pod 访问这个新的 `nginx` 服务。为了验证它,从 default 命名空间下的其它 pod 来访问该服务。请您确保在该命名空间下没有执行孤立动作。
102+
You should be able to access the new `nginx` service from other Pods. To access the `nginx` Service from another Pod in the `default` namespace, start a busybox container:
103+
-->
104+
## 通过从 Pod 访问服务对其进行测试
64105

65-
66-
启动一个 busybox 容器,然后在容器中使用 `wget` 命令去访问 `nginx` 服务
106+
您应该可以从其它的 Pod 访问这个新的 `nginx` 服务。
107+
要从 default 命名空间中的其它s Pod 来访问该服务。可以启动一个 busybox 容器
67108

68109
```console
69-
$ kubectl run busybox --rm -ti --image=busybox /bin/sh
70-
Waiting for pod default/busybox-472357175-y0m47 to be running, status is Pending, pod ready: false
110+
kubectl run busybox --rm -ti --image=busybox /bin/sh
111+
```
71112

72-
Hit enter for command prompt
113+
<!--
114+
In your shell, run the following command:
115+
-->
116+
在你的 Shell 中,运行下面的命令:
73117

74-
/ # wget --spider --timeout=1 nginx
118+
```shell
119+
wget --spider --timeout=1 nginx
120+
```
121+
```none
75122
Connecting to nginx (10.100.0.16:80)
76-
/ #
123+
remote file exists
77124
```
78125

126+
<!--
127+
## Limit access to the `nginx` service
79128
80-
## 限制访问 `nginx` 服务
129+
To limit the access to the `nginx` service so that only Pods with the label `access: true` can query it, create a NetworkPolicy object as follows:
130+
-->
131+
## 限制 `nginx` 服务的访问
81132

133+
如果想限制对 `nginx` 服务的访问,只让那些拥有标签 `access: true` 的 Pod 访问它,
134+
那么可以创建一个如下所示的 NetworkPolicy 对象:
82135

83-
如果说您想限制 `nginx` 服务,只让那些拥有标签 `access: true` 的 pod 访问它,那么您可以创建一个只允许从那些 pod 连接的 `NetworkPolicy`
136+
{{< codenew file="service/networking/nginx-policy.yaml" >}}
84137

85-
```yaml
86-
kind: NetworkPolicy
87-
apiVersion: networking.k8s.io/v1
88-
metadata:
89-
name: access-nginx
90-
spec:
91-
podSelector:
92-
matchLabels:
93-
app: nginx
94-
ingress:
95-
- from:
96-
- podSelector:
97-
matchLabels:
98-
access: "true"
99-
```
138+
<!--
139+
The name of a NetworkPolicy object must be a valid
140+
[DNS subdomain name](/docs/concepts/overview/working-with-objects/names#dns-subdomain-names).
141+
-->
142+
NetworkPolicy 对象的名称必须是一个合法的
143+
[DNS 子域名](/zh/docs/concepts/overview/working-with-objects/names#dns-subdomain-names).
100144

145+
<!--
146+
NetworkPolicy includes a `podSelector` which selects the grouping of Pods to which the policy applies. You can see this policy selects Pods with the label `app=nginx`. The label was automatically added to the Pod in the `nginx` Deployment. An empty `podSelector` selects all pods in the namespace.
147+
-->
148+
{{< note >}}
149+
NetworkPolicy 中包含选择策略所适用的 Pods 集合的 `podSelector`
150+
你可以看到上面的策略选择的是带有标签 `app=nginx` 的 Pods。
151+
此标签是被自动添加到 `nginx` Deployment 中的 Pod 上的。
152+
如果 `podSelector` 为空,则意味着选择的是名字空间中的所有 Pods。
153+
{{< /note >}}
101154

102-
## 为服务指定策略
155+
<!--
156+
## Assign the policy to the service
103157
158+
Use kubectl to create a NetworkPolicy from the above `nginx-policy.yaml` file:
159+
-->
160+
## 为服务指定策略
104161

105-
使用 kubectl 工具根据上面的 nginx-policy.yaml 文件创建一个 NetworkPolicy:
162+
使用 kubectl 根据上面的 `nginx-policy.yaml` 文件创建一个 NetworkPolicy:
106163

107164
```console
108-
$ kubectl create -f nginx-policy.yaml
109-
networkpolicy "access-nginx" created
165+
kubectl apply -f https://k8s.io/examples/service/networking/nginx-policy.yaml
166+
```
167+
```none
168+
networkpolicy.networking.k8s.io/access-nginx created
110169
```
111170

171+
<!--
172+
## Test access to the service when access label is not defined
112173
113-
## 当访问标签没有定义时测试访问服务
114-
174+
When you attempt to access the `nginx` Service from a Pod without the correct labels, the request times out:
175+
-->
176+
## 测试没有定义访问标签时访问服务
115177

116-
如果您尝试从没有设定正确标签的 pod 中去访问 `nginx` 服务,请求将会超时:
178+
如果你尝试从没有设定正确标签的 Pod 中去访问 `nginx` 服务,请求将会超时:
117179

118180
```console
119-
$ kubectl run busybox --rm -ti --image=busybox /bin/sh
120-
Waiting for pod default/busybox-472357175-y0m47 to be running, status is Pending, pod ready: false
181+
kubectl run busybox --rm -ti --image=busybox -- /bin/sh
182+
```
121183

122-
Hit enter for command prompt
184+
<!--
185+
In your shell, run the command:
186+
-->
187+
在 Shell 中运行命令:
123188

124-
/ # wget --spider --timeout=1 nginx
189+
```shell
190+
wget --spider --timeout=1 nginx
191+
```
192+
193+
```none
125194
Connecting to nginx (10.100.0.16:80)
126195
wget: download timed out
127-
/ #
128196
```
129197

198+
<!--
199+
## Define access label and test again
130200
201+
You can create a Pod with the correct labels to see that the request is allowed:
202+
-->
131203
## 定义访问标签后再次测试
132204

133-
134-
创建一个拥有正确标签的 pod,您将看到请求是被允许的:
205+
创建一个拥有正确标签的 Pod,你将看到请求是被允许的:
135206

136207
```console
137-
$ kubectl run busybox --rm -ti --labels="access=true" --image=busybox /bin/sh
138-
Waiting for pod default/busybox-472357175-y0m47 to be running, status is Pending, pod ready: false
208+
kubectl run busybox --rm -ti --labels="access=true" --image=busybox -- /bin/sh
209+
```
210+
<!--
211+
In your shell, run the command:
212+
-->
213+
在 Shell 中运行命令:
139214

140-
Hit enter for command prompt
215+
```shell
216+
wget --spider --timeout=1 nginx
217+
```
141218

142-
/ # wget --spider --timeout=1 nginx
219+
```none
143220
Connecting to nginx (10.100.0.16:80)
144-
/ #
221+
remote file exists
145222
```
146223

147-
148-

0 commit comments

Comments
 (0)