环境构建

基础环境构建

[root@master01 cksstudy]# vi studyns01.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: studyns01
  labels:
    role: studyns01
[root@master01 cksstudy]# vi studyns02.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: studyns02
  labels:
    role: studyns02
[root@master01 cksstudy]# vi studyns03.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: studyns03
  labels:
    role: studyns03
[root@master01 cksstudy]# vi studyns04.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: studyns04
  labels:
    role: studyns04
[root@master01 cksstudy]# vi studypod01.yaml
apiVersion: v1
kind: Pod
metadata:
  name: studypod01
  namespace: studyns01
  labels:
    role: studypod01
spec:
  containers:
  - name: studypod01
    image: training/webapp
    command:
      - python
      - app.py
    imagePullPolicy: IfNotPresent
  restartPolicy: Always
[root@master01 cksstudy]# vi studypod02.yaml
apiVersion: v1
kind: Pod
metadata:
  name: studypod02
  namespace: studyns01
  labels:
    role: studypod02
spec:
  containers:
  - name: studypod02
    image: training/webapp
    command:
      - python
      - app.py
    imagePullPolicy: IfNotPresent
  restartPolicy: Always
[root@master01 cksstudy]# vi studypod03.yaml
apiVersion: v1
kind: Pod
metadata:
  name: studypod03
  namespace: studyns01
  labels:
    role: studypod03
spec:
  containers:
  - name: studypod03
    image: training/webapp
    command:
      - python
      - app.py
    imagePullPolicy: IfNotPresent
  restartPolicy: Always
[root@master01 cksstudy]# vi studypod04.yaml
apiVersion: v1
kind: Pod
metadata:
  name: studypod04
  namespace: studyns02
  labels:
    role: studypod04
spec:
  containers:
  - name: studypod04
    image: training/webapp
    command:
      - python
      - app.py
    imagePullPolicy: IfNotPresent
  restartPolicy: Always
[root@master01 cksstudy]# vi studypod05.yaml
apiVersion: v1
kind: Pod
metadata:
  name: studypod05
  namespace: studyns03
  labels:
    role: studypod05
spec:
  containers:
  - name: studypod05
    image: training/webapp
    command:
      - python
      - app.py
    imagePullPolicy: IfNotPresent
  restartPolicy: Always
[root@master01 cksstudy]# vi studypod06.yaml
apiVersion: v1
kind: Pod
metadata:
  name: studypod06
  namespace: studyns04
  labels:
    role: studypod06
spec:
  containers:
  - name: studypod06
    image: training/webapp
    command:
      - python
      - app.py
    imagePullPolicy: IfNotPresent
  restartPolicy: Always

[root@master01 cksstudy]# kubectl apply -f .

[root@master01 cksstudy]# kubectl -n studyns01 get pods -o wide
NAME         READY   STATUS    RESTARTS   AGE   IP             NODE       NOMINATED NODE   READINESS GATES
studypod01   1/1     Running   0          14s   10.10.30.108   worker02   <none>           <none>
studypod02   1/1     Running   0          14s   10.10.5.46     worker01   <none>           <none>
studypod03   1/1     Running   0          14s   10.10.5.47     worker01   <none>           <none>
[root@master01 cksstudy]# kubectl -n studyns02 get pods -o wide
NAME         READY   STATUS    RESTARTS   AGE   IP             NODE       NOMINATED NODE   READINESS GATES
studypod04   1/1     Running   0          16s   10.10.30.109   worker02   <none>           <none>
[root@master01 cksstudy]# kubectl -n studyns03 get pods -o wide
NAME         READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
studypod05   1/1     Running   0          19s   10.10.5.48   worker01   <none>           <none>
[root@master01 cksstudy]# kubectl -n studyns04 get pods -o wide
NAME         READY   STATUS    RESTARTS   AGE   IP             NODE       NOMINATED NODE   READINESS GATES
studypod06   1/1     Running   0          21s   10.10.30.110   worker02   <none>           <none>

网络测试

[root@master01 cksstudy]# kubectl -n studyns01 exec -ti studypod01 -- /bin/sh
# ping -c 1 10.10.5.46
PING 10.10.5.46 (10.10.5.46) 56(84) bytes of data.
64 bytes from 10.10.5.46: icmp_seq=1 ttl=62 time=0.374 ms

--- 10.10.5.46 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.374/0.374/0.374/0.000 ms
# ping -c 1 10.10.5.47
PING 10.10.5.47 (10.10.5.47) 56(84) bytes of data.
64 bytes from 10.10.5.47: icmp_seq=1 ttl=62 time=0.522 ms

--- 10.10.5.47 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.522/0.522/0.522/0.000 ms
# ping -c 1 10.10.30.109
PING 10.10.30.109 (10.10.30.109) 56(84) bytes of data.
64 bytes from 10.10.30.109: icmp_seq=1 ttl=63 time=0.109 ms

--- 10.10.30.109 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.109/0.109/0.109/0.000 ms
# ping -c 1 10.10.5.48
PING 10.10.5.48 (10.10.5.48) 56(84) bytes of data.
64 bytes from 10.10.5.48: icmp_seq=1 ttl=62 time=0.408 ms

--- 10.10.5.48 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.408/0.408/0.408/0.000 ms
# ping -c 1 10.10.30.110
PING 10.10.30.110 (10.10.30.110) 56(84) bytes of data.
64 bytes from 10.10.30.110: icmp_seq=1 ttl=63 time=0.073 ms

--- 10.10.30.110 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.073/0.073/0.073/0.000 ms

安全策略

策略配置

[root@master01 cksstudy]# vi studynp01.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: studynp01
  namespace: studyns01
spec:
  podSelector:
    matchLabels:
      role: studypod01
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - ipBlock:
        cidr: 10.10.0.0/16
        except:
        - 10.10.30.0/24
    - namespaceSelector:
        matchLabels:
          role: studyns03
    - podSelector:
        matchLabels:
          role: studypod02
    ports:
    - protocol: TCP
      port: 5000
  egress:
  - to:
    - ipBlock:
        cidr: 10.10.30.0/24
    ports:
    - protocol: TCP
      port: 5000

[root@master01 cksstudy]# kubectl apply -f studynp01.yaml

释义说明:

  • 必须字段:类似于Kubernetes其他安全策略,NetworkPolicy 资源需要 apiVersion、 kind 和 metadata 字段。
  • pec:NetworkPolicy spec中包含了在一个命名空间中定义特定网络策略所需的所有信息。
  • podSelector:每个 NetworkPolicy 都包括一个 podSelector,它对该策略所适用的一组 Pod 进行选择。空的 podSelector 表示匹配命名空间下的所有 Pod。
  • policyTypes: 每个 NetworkPolicy 都包含一个 policyTypes 列表,其中包含 Ingress 或 Egress 或两者兼具。policyTypes 字段表示该策略是应用于 进入所选 Pod 的入站流量还是来自所选 Pod 的出站流量,或两者都进行了配置。 如果 NetworkPolicy 未指定 policyTypes 则默认情况下始终设置 Ingress; 如果 NetworkPolicy 有需要配置任何出口规则的话则必须配置 Egress。
  • ingress: 每个 NetworkPolicy 可包含一个 ingress 规则的白名单列表。 每个规则都允许同时匹配 from 和 ports 部分的流量。
  • egress: 每个 NetworkPolicy 可包含一个 egress 规则的白名单列表。 每个规则都允许匹配 to 和 port 部分的流量。

如上网络策略示例表示:

  • 隔离 "studyns01" 命名空间下 "role:=studypod01" 的 Pod ,即隔离studypod01;
  • Ingress 规则允许以下 Pod 连接到 "studyns01" 命名空间下的带有 "role:=studypod01" 标签的所有 Pod 的 5000 TCP 端口:
    • 允许"studyns01" 命名空间下带有 "role:=studypod01" 标签的所有 Pod入;
    • 允许带有 "role=studyns03" 标签的命名空间中的所有 Pod入;
    • 允许IP 地址范围为除了 10.10.30.0/24 之外的所有 10.10.0.0/16入。
  • Egress 规则允许从"studyns01" 命名空间中带有 "role:=studypod01" 标签的任何 Pod 到 CIDR 10.0.0.0/16 下 5000 TCP 端口的连接。

策略测试

ingress方向测试
  • 拥有10.10.30.0/24 ip的Pod(如studypod04)访问studypod01(10.10.5.46):
[root@master01 ~]# kubectl -n studyns02 exec -ti studypod04 -- /bin/bash
root@studypod04:/opt/webapp# nc -v 10.10.30.108 5000            #根据策略应该不通
  • 拥有10.10.0.0/16 ip的Pod(如studypod03)访问studypod01(10.10.5.46):
[root@master01 ~]# kubectl -n studyns01 exec -ti studypod03 -- /bin/bash
root@studypod03:/opt/webapp# nc -v 10.10.30.108 5000            #根据策略应该能通
Connection to 10.10.30.108 5000 port [tcp/*] succeeded!
  • 所有studyns03命名空间下的Pod(如studypod05)访问studypod01(10.10.5.46):
[root@master01 ~]# kubectl -n studyns03 exec -ti studypod05 -- /bin/bash
root@studypod05:/opt/webapp# nc -v 10.10.30.108 5000            #根据策略应该能通
Connection to 10.10.30.108 5000 port [tcp/*] succeeded!
  • 所有带role=studypod02标签的Pod(如studypod02)访问studypod01(10.10.5.46):
[root@master01 ~]# kubectl -n studyns01 exec -ti studypod02 -- /bin/bash
root@studypod02:/opt/webapp# nc -v 10.10.30.108 5000            #根据策略应该能通
Connection to 10.10.30.108 5000 port [tcp/*] succeeded!
egress方向测试
  • studypod01访问拥有10.10.30.0/24 ip的Pod(如studypod06):
[root@master01 ~]# kubectl -n studyns01 exec -ti studypod01 -- /bin/bash
root@studypod01:/opt/webapp# nc -v 10.10.30.109 5000            #根据策略应该能通
Connection to 10.10.30.109 5000 port [tcp/*] succeeded!
^C
root@studypod01:/opt/webapp# nc -v 10.10.30.110 5000            #根据策略应该能通
Connection to 10.10.30.110 5000 port [tcp/*] succeeded!
^C
root@studypod01:/opt/webapp# nc -v 10.10.5.46 5000              #根据策略应该不通
^C

to和from行为

在 ingress 的 from 部分或 egress 的 to 部分中指定四种选择器:

  • podSelector: 此选择器将在与 NetworkPolicy 相同的命名空间中选择特定的 Pod,ingress 的 from 则表示允许该选择器选定的Pod作为入站流量来源,egress 的 to则表示允许去往该选择器选定的Pod的出站流量(即该podSelector选定的Pod为目的地)。
  • namespaceSelector:此选择器将选择特定的命名空间,应将所有 Pod 用作其入站流量来源或出站流量目的地。
  • namespaceSelector 和 podSelector: 一个指定 namespaceSelector 和 podSelector 的 to/from 条目选择特定命名空间中的特定 Pod。
    示例001:只允许来自带有user=xhyns标签的命名空间下,且带有role=xhyuser的特定Pod的入请求连接。
  ...
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          user: alice
      podSelector:
        matchLabels:
          role: client
  ...

示例002:允许来自带有user=xhyns标签的命名空间下的任何Pod,或来自该策略所在命名空间的带有role=xhyuser的Pod的入请求连接。

  ...
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          user: xhyns
    - podSelector:
        matchLabels:
          role: xhyuser
  ...
  • ipBlock: 此选择器将选择特定的 IP CIDR 范围以用作入站流量来源或出站流量目的地。通常为集群外部IP,Pod IP通常为随机产生,使用Pod IP段无实际意义。

默认策略

默认情况下,如果命名空间中不存在任何策略,则所有进出该命名空间中 Pod 的流量都被允许。 可通过如下方式修改命名空间中的默认行为。

  • 默认拒绝所有入站流量
    禁止进入studyns01命名空间下的所有Pod的入流量,即默认拒绝所有进入studyns01命名空间任何Pod的流量。
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-ingress
  namespace: studyns01
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  • 默认允许所有入站流量
    允许进入studyns01命名空间下的所有Pod的入流量,即默认允许所有进入studyns01命名空间任何Pod的流量。
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-all-ingress
  namespace: studyns01
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  ingress:
  - {}
  • 默认拒绝所有出站流量
    禁止studyns01命名空间下的所有Pod的出流量,即默认禁止studyns01命名空间任何Pod的出流量。
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-egress
  namespace: studyns01
spec:
  podSelector: {}
  policyTypes:
  - Egress
  • 默认允许所有出站流量
    允许studyns01命名空间下的所有Pod的出流量,即默认允许studyns01命名空间任何Pod的出流量。
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-all-egress
  namespace: studyns01
spec:
  podSelector: {}
  egress:
  - {}
  policyTypes:
  - Egress

  • 默认拒绝所有入口和所有出站流量
    禁止studyns01命名空间下的所有出入流量。
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-all
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  - Egress
12-23 22:48