cka真题之---网络策略

Create a new NetworkPolicy named allow-port-from-namespace to allow Pods in the existing namespace internal to connect to port 8080 of other Pods in the same namespace.
Ensure that the new NetworkPolicy:

does not allow access to Pods not listening on port 8080.

does not allow access from Pods not in namespace internal

参考官方文档:

https://kubernetes.io/docs/concepts/services-networking/network-policies/

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: test-network-policy
  namespace: default
spec:
  podSelector:
    matchLabels:
      role: db
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - ipBlock:
        cidr: 172.17.0.0/16
        except:
        - 172.17.1.0/24
    - namespaceSelector:
        matchLabels:
          project: myproject
    - podSelector:
        matchLabels:
          role: frontend
    ports:
    - protocol: TCP
      port: 6379
  egress:
  - to:
    - ipBlock:
        cidr: 10.0.0.0/24
    ports:
    - protocol: TCP
      port: 5978

policyTypes指定控制入站流量还是出战流量

然后分别配置ingress 和 egress

Mandatory Fields: As with all other Kubernetes config, a NetworkPolicy needs apiVersionkind, and metadata fields. For general information about working with config files, see Configure Containers Using a ConfigMap, and Object Management.

spec: NetworkPolicy spec has all the information needed to define a particular network policy in the given namespace.

podSelector: Each NetworkPolicy includes a podSelector which selects the grouping of pods to which the policy applies. The example policy selects pods with the label "role=db". An empty podSelector selects all pods in the namespace.

policyTypes: Each NetworkPolicy includes a policyTypes list which may include either IngressEgress, or both. The policyTypes field indicates whether or not the given policy applies to ingress traffic to selected pod, egress traffic from selected pods, or both. If no policyTypes are specified on a NetworkPolicy then by default Ingress will always be set and Egress will be set if the NetworkPolicy has any egress rules.

ingress: Each NetworkPolicy may include a list of allowed ingress rules. Each rule allows traffic which matches both the from and ports sections. The example policy contains a single rule, which matches traffic on a single port, from one of three sources, the first specified via an ipBlock, the second via a namespaceSelector and the third via a podSelector.

egress: Each NetworkPolicy may include a list of allowed egress rules. Each rule allows traffic which matches both the to and ports sections. The example policy contains a single rule, which matches traffic on a single port to any destination in 10.0.0.0/24.

创建networkPolicy,针对namespace internal下的pod,只允许同样namespace下的pod访问,并且可访问pod的9000端口。

不允许不是来自这个namespace的pod访问。

不允许不是监听9000端口的pod访问。

创建yml文件 类型是NetworkPolicy 名字是allow-port-from-namespace

root@lijian:~/k8s# cat networkpolicy.yml 
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
   name: allow-port-from-namespace
   namespace: internal
spec:
    podSelector: {}
    policyTypes:
    - Ingress
    ingress:
    - from:
      - podSelector: {}
      ports: 
      - protocol: TCP
        port: 8080

root@lijian:~/k8s# kubectl apply -f networkpolicy.yml 
Error from server (NotFound): error when creating "networkpolicy.yml": namespaces "internal" not found
root@lijian:~/k8s# kubectl create ns internal
namespace/internal created
root@lijian:~/k8s# kubectl apply -f networkpolicy.yml 
networkpolicy.networking.k8s.io/allow-port-from-namespace created


 

你可能感兴趣的:(k8s,kubernetes)