diff --git a/content/zh/docs/tasks/administer-cluster/declare-network-policy.md b/content/zh/docs/tasks/administer-cluster/declare-network-policy.md index 11ddbff307..f54c581d5d 100644 --- a/content/zh/docs/tasks/administer-cluster/declare-network-policy.md +++ b/content/zh/docs/tasks/administer-cluster/declare-network-policy.md @@ -1,53 +1,93 @@ --- -approvers: -- caseydavenport -- danwinship title: 声明网络策略 content_type: task --- + - -本文可以帮助您开始使用 Kubernetes 的 [NetworkPolicy API](/docs/concepts/services-networking/network-policies/) 声明网络策略去管理 Pod 之间的通信 - - + +本文可以帮助您开始使用 Kubernetes 的 [NetworkPolicy API](/zh/docs/concepts/services-networking/network-policies/) 声明网络策略去管理 Pod 之间的通信 ## {{% heading "prerequisites" %}} +{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}} + 您首先需要有一个支持网络策略的 Kubernetes 集群。已经有许多支持 NetworkPolicy 的网络提供商,包括: -* [Calico](/docs/tasks/configure-pod-container/calico-network-policy/) -* [Romana](/docs/tasks/configure-pod-container/romana-network-policy/) -* [Weave 网络](/docs/tasks/configure-pod-container/weave-network-policy/) - - -**注意**:以上列表是根据产品名称按字母顺序排序,而不是按推荐或偏好排序。下面示例对于使用了上面任何提供商的 Kubernetes 集群都是有效的 - +* [Calico](/zh/docs/tasks/configure-pod-container/calico-network-policy/) +* [Cilium](/zh/docs/tasks/administer-cluster/network-policy-provider/cilium-network-policy/) +* [Kube-router](/zh/docs/tasks/administer-cluster/network-policy-provider/kube-router-network-policy/) +* [Romana](/zh/docs/tasks/configure-pod-container/romana-network-policy/) +* [Weave 网络](/zh/docs/tasks/configure-pod-container/weave-network-policy/) + +{{< note >}} +以上列表是根据产品名称按字母顺序排序,而不是按推荐或偏好排序。 +下面示例对于使用了上面任何提供商的 Kubernetes 集群都是有效的 +{{< /note >}} + +## 创建一个`nginx` Deployment 并且通过服务将其暴露 为了查看 Kubernetes 网络策略是怎样工作的,可以从创建一个`nginx` deployment 并且通过服务将其暴露开始 ```console -$ kubectl create deployment nginx --image=nginx +kubectl create deployment nginx --image=nginx +``` +```none deployment "nginx" created -$ kubectl expose deployment nginx --port=80 +``` + + +将此 Deployment 以名为 `nginx` 的 Service 暴露出来: + +```console +kubectl expose deployment nginx --port=80 +``` +```none service "nginx" exposed ``` - -在 default 命名空间下运行了两个 `nginx` pod,而且通过一个名字为 `nginx` 的服务进行了暴露 + +上述命令创建了一个带有一个 nginx 的 Deployment,并将之通过名为 `nginx` 的 +Service 暴露出来。名为 `nginx` 的 Pod 和 Deployment 都位于 `default` +名字空间内。 ```console -$ kubectl get svc,pod +kubectl get svc,pod +``` +```none NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/kubernetes 10.100.0.1 443/TCP 46m svc/nginx 10.100.0.16 80/TCP 33s @@ -56,93 +96,128 @@ NAME READY STATUS RESTARTS AGE po/nginx-701339712-e0qfq 1/1 Running 0 35s ``` + +## 通过从 Pod 访问服务对其进行测试 - -您应该可以从其它的 pod 访问这个新的 `nginx` 服务。为了验证它,从 default 命名空间下的其它 pod 来访问该服务。请您确保在该命名空间下没有执行孤立动作。 - - -启动一个 busybox 容器,然后在容器中使用 `wget` 命令去访问 `nginx` 服务: +您应该可以从其它的 Pod 访问这个新的 `nginx` 服务。 +要从 default 命名空间中的其它s Pod 来访问该服务。可以启动一个 busybox 容器: ```console -$ kubectl run busybox --rm -ti --image=busybox /bin/sh -Waiting for pod default/busybox-472357175-y0m47 to be running, status is Pending, pod ready: false +kubectl run busybox --rm -ti --image=busybox /bin/sh +``` -Hit enter for command prompt + +在你的 Shell 中,运行下面的命令: -/ # wget --spider --timeout=1 nginx +```shell +wget --spider --timeout=1 nginx +``` +```none Connecting to nginx (10.100.0.16:80) -/ # +remote file exists ``` + +## 限制 `nginx` 服务的访问 +如果想限制对 `nginx` 服务的访问,只让那些拥有标签 `access: true` 的 Pod 访问它, +那么可以创建一个如下所示的 NetworkPolicy 对象: -如果说您想限制 `nginx` 服务,只让那些拥有标签 `access: true` 的 pod 访问它,那么您可以创建一个只允许从那些 pod 连接的 `NetworkPolicy`: +{{< codenew file="service/networking/nginx-policy.yaml" >}} -```yaml -kind: NetworkPolicy -apiVersion: networking.k8s.io/v1 -metadata: - name: access-nginx -spec: - podSelector: - matchLabels: - app: nginx - ingress: - - from: - - podSelector: - matchLabels: - access: "true" -``` + +NetworkPolicy 对象的名称必须是一个合法的 +[DNS 子域名](/zh/docs/concepts/overview/working-with-objects/names#dns-subdomain-names). + +{{< note >}} +NetworkPolicy 中包含选择策略所适用的 Pods 集合的 `podSelector`。 +你可以看到上面的策略选择的是带有标签 `app=nginx` 的 Pods。 +此标签是被自动添加到 `nginx` Deployment 中的 Pod 上的。 +如果 `podSelector` 为空,则意味着选择的是名字空间中的所有 Pods。 +{{< /note >}} + ## 为服务指定策略 - -使用 kubectl 工具根据上面的 nginx-policy.yaml 文件创建一个 NetworkPolicy: +使用 kubectl 根据上面的 `nginx-policy.yaml` 文件创建一个 NetworkPolicy: ```console -$ kubectl create -f nginx-policy.yaml -networkpolicy "access-nginx" created +kubectl apply -f https://k8s.io/examples/service/networking/nginx-policy.yaml +``` +```none +networkpolicy.networking.k8s.io/access-nginx created ``` + +## 测试没有定义访问标签时访问服务 - -如果您尝试从没有设定正确标签的 pod 中去访问 `nginx` 服务,请求将会超时: +如果你尝试从没有设定正确标签的 Pod 中去访问 `nginx` 服务,请求将会超时: ```console -$ kubectl run busybox --rm -ti --image=busybox /bin/sh -Waiting for pod default/busybox-472357175-y0m47 to be running, status is Pending, pod ready: false +kubectl run busybox --rm -ti --image=busybox -- /bin/sh +``` -Hit enter for command prompt + +在 Shell 中运行命令: -/ # wget --spider --timeout=1 nginx +```shell +wget --spider --timeout=1 nginx +``` + +```none Connecting to nginx (10.100.0.16:80) wget: download timed out -/ # ``` + ## 定义访问标签后再次测试 - -创建一个拥有正确标签的 pod,您将看到请求是被允许的: +创建一个拥有正确标签的 Pod,你将看到请求是被允许的: ```console -$ kubectl run busybox --rm -ti --labels="access=true" --image=busybox /bin/sh -Waiting for pod default/busybox-472357175-y0m47 to be running, status is Pending, pod ready: false +kubectl run busybox --rm -ti --labels="access=true" --image=busybox -- /bin/sh +``` + +在 Shell 中运行命令: -Hit enter for command prompt - -/ # wget --spider --timeout=1 nginx -Connecting to nginx (10.100.0.16:80) -/ # +```shell +wget --spider --timeout=1 nginx ``` - +```none +Connecting to nginx (10.100.0.16:80) +remote file exists +```