Enable default deny for Kubernetes pods

Big picture

Enable a default deny policy for Kubernetes pods using Kubernetes or Calico network policy.

Value

A default deny network policy provides an enhanced security posture — so pods without policy (or incorrect policy) are not allowed traffic until appropriate network policy is defined.

Features

This how-to guide uses the following Calico features:

  • NetworkPolicy
  • GlobalNetworkPolicy

Concepts

Default deny/allow behavior

Default allow means all traffic is allowed by default, unless otherwise specified. Default deny means all traffic is denied by default, unless explicitly allowed. Kubernetes pods are default allow, unless network policy is defined to specify otherwise.

For compatibility with Kubernetes, Calico network policy enforcement follows the standard convention for Kubernetes pods:

  • If no network policies apply to a pod, then all traffic to/from that pod is allowed.
  • If one or more network policies apply to a pod with type ingress, then only the ingress traffic specifically allowed by those policies is allowed.
  • If one or more network policies apply to a pod with type egress, then only the egress traffic specifically allowed by those policies is allowed.

For other endpoint types (VMs, host interfaces), the default behavior is to deny traffic. Only traffic specifically allowed by network policy is allowed, even if no network policies apply to the endpoint.

Best practice: implicit default deny policy

We recommend creating an implicit default deny policy for your Kubernetes pods, regardless of whether you use Calico or Kubernetes network policy. This ensures that unwanted traffic is denied by default. Note that implicit default deny policy always occurs last; if any other policy allows the traffic, then the deny does not come into effect. The deny is executed only after all other policies are evaluated.

Before we begin

If you haven’t already, you will need to install calicoctl to apply the example Calico network policies from this page.

How to

Although you can use any of the following policies to create default deny policy for Kubernetes pods, we recommend using the Calico global network policy. A Calico global network policy applies to all workloads (VMs and containers) in all namespaces, as well as hosts (computers that run the hypervisor for VMs, or container runtime for containers). Using a Calico global network policy supports a conservative security stance for protecting resources.

Enable default deny Calico global network policy, non-namespaced

You can use a Calico global network policy to enable a default deny across your whole cluster. The following example applies to all workloads (VMs and containers) in all namespaces, as well as hosts (computers that run the hypervisor for VMs, or container runtime for containers).

Enable default deny for Kubernetes pods - 图1note

Before applying the following please continue reading the rest of this section to find out why this might not be the best policy to apply to your cluster.

  1. apiVersion: projectcalico.org/v3
  2. kind: GlobalNetworkPolicy
  3. metadata:
  4. name: default-deny
  5. spec:
  6. selector: all()
  7. types:
  8. - Ingress
  9. - Egress

The above policy applies to all pods and host endpoints, including Kubernetes control plane and Calico control plane nodes and pods. Such policy has the potential to break your cluster if you do not already have the correct “Allow” policies and Calico failsafe ports in place to ensure control plane traffic does not get blocked.

As an alternative best practice we recommend to use the following example, which applies a default-deny behaviour to all non-system pods. The policy also allows access kube-dns, which simplifies per pod policies since you don’t need to duplicate the DNS rules in every policy.

  1. apiVersion: projectcalico.org/v3
  2. kind: GlobalNetworkPolicy
  3. metadata:
  4. name: deny-app-policy
  5. spec:
  6. namespaceSelector: has(projectcalico.org/name) && projectcalico.org/name not in {"kube-system", "calico-system", "calico-apiserver"}
  7. types:
  8. - Ingress
  9. - Egress
  10. egress:
  11. # allow all namespaces to communicate to DNS pods
  12. - action: Allow
  13. protocol: UDP
  14. destination:
  15. selector: 'k8s-app == "kube-dns"'
  16. ports:
  17. - 53
  18. - action: Allow
  19. protocol: TCP
  20. destination:
  21. selector: 'k8s-app == "kube-dns"'
  22. ports:
  23. - 53

It is important to note the above policy deliberately excludes the kube-system, calico-system and calico-apiserver namespaces by using a negative namespaceSelector to avoid impacting any control plane components. To secure the control plane you can write specific policies for each control plane component, though you should do so with care, ideally at cluster creation time, since getting these wrong can leave your cluster in a broken state. We recommend you always make sure you have the correct Calico failsafe ports in place before you start trying to create policies for the control plane.

Enable default deny Calico network policy, namespaced

In the following example, we enable a default deny NetworkPolicy for all workloads in the namespace, engineering.

  1. apiVersion: projectcalico.org/v3
  2. kind: NetworkPolicy
  3. metadata:
  4. name: default-deny
  5. namespace: engineering
  6. spec:
  7. selector: all()
  8. types:
  9. - Ingress
  10. - Egress

Enable default deny Kubernetes policy, namespaced

In the following example, we enable a default deny Kubernetes network policy for all pods in the namespace, engineering.

  1. apiVersion: networking.k8s.io/v1
  2. kind: NetworkPolicy
  3. metadata:
  4. name: default-deny
  5. namespace: engineering
  6. spec:
  7. podSelector: {}
  8. policyTypes:
  9. - Ingress
  10. - Egress

Additional resources