Kubernetes NetworkPolicy to block internal calls

Wed 11 September 2019

Network policies are pretty powerful in kubernetes. It helps us define how a group of pods can communicate with other network endpoints - which can be internal or external to the cluster.

And if you want to run customer workloads, it's always a good idea to make sure none of the pods can connect to other network endpoints running inside your cluster. NetworkPolicies are the perfect candidate to implement this.

One thing to keep in mind, as soon as you block all outgoing connections internal to the cluster, DNS will be unreachable as well. To make it work, you have to add a DNS config for the pod which will configure it's resolv.conf. Example pod spec:

    - name: test
      image: nginx
  dnsPolicy: "None"

Once this is set and assuming your pod don't have to connect to any other component inside the cluster, we can have a NetworkPolicy similar to this:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
  namespace: example-namespace
  name: np-block-internal
  podSelector: {}
  - Egress
  - to:
    - ipBlock:

Few things to notice here:

  • podSelector: {} specifies all pods in this namespace should be affected
  • egress means outgoing connections
  • to ipBlock cidr sets the rule. means all IP addresses over the internet are allowed. Except
  • is the pod address range. This is different for each cloud provider, configured during cluster creation. In GCP it's set with the --cluster-ipv4-cidr flag if you are creating the cluster via CLI.