Kubernetes Network Policy for no in-cluster egress.

Sometimes it’s useful to run workloads with restricted network access to other internal services.

One way to achieve this in Kubernetes is via network policies.

One way to do it: permit egress to 0.0.0.0, then deny all standardized private CIDR ranges.

Example:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: egress-proxy
spec:
  podSelector:
    matchLabels:
        change.me: please ### CHANGE ME
  policyTypes:
    - Egress
  egress:
    # block all internal traffic on port 443
    - ports:
        - protocol: TCP
          port: 443
      to:
        - ipBlock:
            cidr: 0.0.0.0/0
            except:
              # IPv4
              # RFC1122, localhost
              - 127.0.0.0/8
              # RFC1918 private networks
              - 10.0.0.0/8
              - 172.16.0.0/12
              - 192.168.0.0/16
              # RFC6598 shared address space
              - 100.64.0.0/10
              # RFC3927 link local addresses
              - 169.254.0.0/16
              # RFC5736, RC5737 test networks
              - 192.0.0.0/24
              - 192.0.2.0/24
              - 198.51.100.0/24
              - 203.0.113.0/24
              # RFC3068 6to4 relay
              - 192.88.99.0/24
              # RFC2544 network interconnect benchmarking
              - 198.18.0.0/15
              # multicast
              - 224.0.0.0/4
              # reserved
              - 240.0.0.0/4

This policy restricts basically all traffic. It may be useful to permit some subset of internal traffic, like DNS, or sidecars like linkerd.

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: no-egress-netpol
spec:
  podSelector:
    matchLabels:
      change.me: please ### CHANGE THIS
  policyTypes:
    - Egress
  egress:
    # allow DNS resolution
    - to:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: kube-system
          podSelector:
            matchLabels:
              k8s-app: kube-dns
      ports:
        - port: 53
          protocol: UDP
        - port: 53
          protocol: TCP
    
    # DNS service IP blocked by other rules erroneously
    - to:
      - ipBlock:
          cidr: 10.80.0.10/32
      ports:
      - port: 53
        protocol: UDP
      - port: 53
        protocol: TCP

    # allow linkerd sidecar to communicate with the control plane
    - to:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: linkerd
          podSelector:
            matchLabels:
              linkerd.io/control-plane-ns: linkerd
      ports:
        - protocol: TCP
          port: 443
        - protocol: TCP
          port: 8080
        - protocol: TCP
          port: 8090

    # allow linkerd sidecar to communicate with the otel collector
    - to:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: opentelemetry
          podSelector:
            matchLabels:
              app.kubernetes.io/name: opentelemetry-collector
      ports:
        - protocol: TCP
          port: 55678

    # block all other internal traffic on 443
    - ports:
        - protocol: TCP
          port: 443
      to:
        - ipBlock:
            cidr: 0.0.0.0/0
            except:
              # IPv4
              # RFC1122, localhost
              - 127.0.0.0/8
              # RFC1918 private networks
              - 10.0.0.0/8
              - 172.16.0.0/12
              - 192.168.0.0/16
              # RFC6598 shared address space
              - 100.64.0.0/10
              # RFC3927 link local addresses
              - 169.254.0.0/16
              # RFC5736, RC5737 test networks
              - 192.0.0.0/24
              - 192.0.2.0/24
              - 198.51.100.0/24
              - 203.0.113.0/24
              # RFC3068 6to4 relay
              - 192.88.99.0/24
              # RFC2544 network interconnect benchmarking
              - 198.18.0.0/15
              # multicast
              - 224.0.0.0/4
              # reserved
              - 240.0.0.0/4

These examples block a specific port. If needed, you can block a range by specifying endPort (implementation).

September 21, 2025


Previous post
Large Language Model Internals We’re statistically modeling language, with the goal of accurately predicting text. Ths simplest model is a bigram model: modeling pairs of