NetworkPolicy

指的是设定Pod之间的网络隔离策略,默认是所有互通,但是我们可以设置,给与一些白名单,规定白名单之外的网络请求都是不可取的

可以设置的层度有

允许的Pods,规定某些Pods是否可以被访问或者可以访问

某些namespaces,那些namespace可以访问或者不可以访问

IP组件,设置CIDR,来控制通信

NetworkPolicy的相关对象如下

图片

那么,我们使用explain来查看一下对象中可选的字段

kubectl explain netpol.spec

KIND:     NetworkPolicy

VERSION:  networking.k8s.io/v1

RESOURCE: spec <Object>

DESCRIPTION:

Specification of the desired behavior for this NetworkPolicy.

NetworkPolicySpec provides the specification of a NetworkPolicy

FIELDS:

egress       <[]Object>

List of egress rules to be applied to the selected pods. Outgoing traffic

is allowed if there are no NetworkPolicies selecting the pod (and cluster

policy otherwise allows the traffic), OR if the traffic matches at least

one egress rule across all of the NetworkPolicy objects whose podSelector

matches the pod. If this field is empty then this NetworkPolicy limits all

outgoing traffic (and serves solely to ensure that the pods it selects are

isolated by default). This field is beta-level in 1.8

ingress      <[]Object>

List of ingress rules to be applied to the selected pods. Traffic is

allowed to a pod if there are no NetworkPolicies selecting the pod (and

cluster policy otherwise allows the traffic), OR if the traffic source is

the pod’s local node, OR if the traffic matches at least one ingress rule

across all of the NetworkPolicy objects whose podSelector matches the pod.

If this field is empty then this NetworkPolicy does not allow any traffic

(and serves solely to ensure that the pods it selects are isolated by

default)

podSelector  <Object> -required-

Selects the pods to which this NetworkPolicy object applies. The array of

ingress rules is applied to any pods selected by this field. Multiple

network policies can select the same set of pods. In this case, the ingress

rules for each are combined additively. This field is NOT optional and

follows standard label selector semantics. An empty podSelector matches all

pods in this namespace.

policyTypes  <[]string>

List of rule types that the NetworkPolicy relates to. Valid options are

[“Ingress”], [“Egress”], or [“Ingress”, “Egress”]. If this field is not

specified, it will default based on the existence of Ingress or Egress

rules; policies that contain an Egress section are assumed to affect

Egress, and all policies (whether or not they contain an Ingress section)

are assumed to affect Ingress. If you want to write an egress-only policy,

you must explicitly specify policyTypes [ “Egress” ]. Likewise, if you want

to write a policy that specifies that no egress is allowed, you must

specify a policyTypes value that include “Egress” (since such a policy

would not include an Egress section and would otherwise default to just [

“Ingress” ]). This field is beta-level in 1.8

其中的首要字段是podSelector,这是一个标准的PodSelector格式,只要被这个选择器选择了的Pod,默认网络都将被封闭,这是不配置Ingress和Engress的情况

其次是关于Ingress和Engress,二者都是数组,代表了入方向和出方向的规则

我们首先来看看Ingress的书写方式

Kubectl explain netpol.spec.ingress

会被告知包含两个字段,分别是from和port

其中from仍然是一个对象数组,我们继续最终下去查看

其中有三种类型,分别是ipBlock,namespaceSelector,podSelector

IpBlock 是指的ip来源,可以设置范围,和except表示范围内不被允许的

NamespaceSelector可以匹配namespace的标签,从而设置允许的namespace

PodSelector,也是和上面一直的选择器,范围为Pod

其次是port,固定了上面设置的白名单可以访问那些port,不写默认不限制

对应的from则是相反,指的是出防线的规则,配置项一样

我们可以给出下面的一个例子

apiVersion: networking.k8s.io/v1

kind: NetworkPolicy

metadata:

name: test-network-policy

namespace: default

spec:

podSelector:  ## 选中指定Pod

matchLabels:

role: default-backend

policyTypes:  ## 定义上面Pod的入站出站规则

– Ingress

– Egress

ingress:    ## 定义入站白名单

– from:

– ipBlock:

cidr: 192.168.0.0/16

except:

– 192.168.10.0/16

– namespaceSelector:

matchLabels:

project: dev

– podSelector:

matchLabels:

role: default-frontend

ports:

– protocol: TCP

port: 80

egress:  ## 定义出站白名单

– to:

– ipBlock:

cidr: 192.168.0.0/16

上面给出了一个具有大多数声明的例子

需要注意,因为Ingress和Engress都声明需要传入一个数组

那么我们的组合方式就很重要了

下面的组合是一个标准的组合

  ingress:

– from:

– namespaceSelector:

matchLabels:

user: alice

– podSelector:

matchLabels:

role: client

上面的组合为

带有user=alice标签的namesapce中的对象和带有role=client的本namesapce的pod都可以访问

下面的组合则表示必须要是user=alice的namespace下,带role=client标签的pod才可以访问

  ingress:

– from:

– namespaceSelector:

matchLabels:

user: alice

podSelector:

matchLabels:

role: client

前者取得是交集关系,后者取的时并集关系

发表评论

邮箱地址不会被公开。 必填项已用*标注