当前位置:  首页>> 技术小册>> Kubernetes中文教程(五)

本文可以帮助你开始使用 Kubernetes 的
[NetworkPolicy API]
声明网络策略去管理 Pod 之间的通信

你首先需要有一个支持网络策略的 Kubernetes 集群。已经有许多支持 NetworkPolicy 的网络提供商,包括:

  • [Antrea]
  • [Calico]
  • [Cilium]
  • [Kube-router]
  • [Romana]
  • [Weave 网络]

创建一个nginx Deployment 并且通过服务将其暴露

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

  1. kubectl create deployment nginx --image=nginx
  1. deployment.apps/nginx created

将此 Deployment 以名为 nginx 的 Service 暴露出来:

  1. kubectl expose deployment nginx --port=80
  1. service/nginx exposed

上述命令创建了一个带有一个 nginx 的 Deployment,并将之通过名为 nginx
Service 暴露出来。名为 nginx 的 Pod 和 Deployment 都位于 default
名字空间内。

  1. kubectl get svc,pod
  1. NAME CLUSTER-IP EXTERNAL-IP PORT AGE
  2. service/kubernetes 10.100.0.1 <none> 443/TCP 46m
  3. service/nginx 10.100.0.16 <none> 80/TCP 33s
  4. NAME READY STATUS RESTARTS AGE
  5. pod/nginx-701339712-e0qfq 1/1 Running 0 35s

通过从 Pod 访问服务对其进行测试

你应该可以从其它的 Pod 访问这个新的 nginx 服务。
要从 default 命名空间中的其它 Pod 来访问该服务。可以启动一个 busybox 容器:

  1. kubectl run busybox --rm -ti --image=busybox:1.28 -- /bin/sh

在你的 Shell 中,运行下面的命令:

  1. wget --spider --timeout=1 nginx
  1. Connecting to nginx
  2. remote file exists

限制 nginx 服务的访问

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

NetworkPolicy 对象的名称必须是一个合法的
[DNS 子域名].

NetworkPolicy 中包含选择策略所适用的 Pods 集合的 podSelector
你可以看到上面的策略选择的是带有标签 app=nginx 的 Pods。
此标签是被自动添加到 nginx Deployment 中的 Pod 上的。
如果 podSelector 为空,则意味着选择的是名字空间中的所有 Pods。

为服务指定策略

使用 kubectl 根据上面的 nginx-policy.yaml 文件创建一个 NetworkPolicy:

  1. kubectl apply -f https://k8s.io/examples/service/networking/nginx-policy.yaml
  1. networkpolicy.networking.k8s.io/access-nginx created

测试没有定义访问标签时访问服务

如果你尝试从没有设定正确标签的 Pod 中去访问 nginx 服务,请求将会超时:

  1. kubectl run busybox --rm -ti --image=busybox:1.28 -- /bin/sh

在 Shell 中运行命令:

  1. wget --spider --timeout=1 nginx
  1. Connecting to nginx
  2. wget: download timed out

定义访问标签后再次测试

创建一个拥有正确标签的 Pod,你将看到请求是被允许的:

  1. kubectl run busybox --rm -ti --labels="access=true" --image=busybox:1.28 -- /bin/sh

在 Shell 中运行命令:

  1. wget --spider --timeout=1 nginx
  1. Connecting to nginx
  2. remote file exists

该分类下的相关小册推荐: