Network Policies

Contents

    Network Policies
    Network Policy recipes (GitHub)
    Guide to Kubernetes Ingress Network Policies (StackRox)
    Guide to Kubernetes Egress Network Policies (StackRox)

    Network Policies are sets of rules which define how Pods are allowed to talk to other Pods and resources inside and outside the cluster.
    → kind of firewalls

    ingress / egress

    Important notes

    A deny all rule is a best practice !

    network policies are namespaced resources
    You will need to create a deny all policy for each namespace!

    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
      name: default-deny-all
    spec:
      podSelector: {}
      policyTypes:
      - Ingress
    

    ingress configuration first
    While it is usually relatively straightforward to figure out from which network endpoints we expect communications to a pod, it is, in practice, usually much harder to figure out to which network endpoints connections from a pod go.

    apiVersion: networking.k8s.io/v1
    kind: NetworkPolicy
    metadata:
      name: ingress-egress-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
    

    Proudly Powered by Zim 0.75.2.

    Template by Etienne Gandrille, based on ZeroFiveEight and using JQuery Toc Plugin.