What is K8s Network Policies
Kubernetes network policy lets administrators and developers enforce which network traffic is allowed using rules.
Network policy lets developers secure access to and from their applications using the same simple language they use to deploy them. Developers can focus on their applications without understanding low-level networking concepts. Enabling developers to easily secure their applications using network policies supports a shift left DevOps environment
The Kubernetes Network Policy API supports the following features:
- Policies are namespace scoped
- Policies are applied to pods using label selectors
- Policy rules can specify the traffic that is allowed to/from pods, namespaces, or CIDRs
- Policy rules can specify protocols (TCP, UDP, SCTP), named ports or port numbers
Remember to enable a CNI that supports network policies when deploying the cluster!
Kubernetes has no built-in capability to enforce the network policy. To enforce network policy, you must use a network plugin such as Calico.
Ingress access best practices:
Ingress is incoming traffic to the pod.
- The default Kubernetes policy is “any-any-any allow” , thi means each pod can reach every pod on every namespace , so every namespace have a deny all policy to correct this insecure default.
- No service should allow incoming traffic from external IPs unless it has a load-balancer or ingress attached to it.
- Services with a load-balancer or ingress should only allow access from the load-balancer IPs:
- – GCP – GKE: 220.127.116.11/22 and 18.104.22.168/16
- – AWS – EKS: 22.214.171.124/16
- Services should only accept traffic on the protocol/port that is served by its pods. For example, a web app should only access incoming traffic on HTTP/HTTPS.
- Kubernetes DNS service should only allow inbound access on UDP 53, from ineternal services/pods.
Egress restrict best practices:
Egress is outgoing traffic from the pod , and it is more difficult to configure than ingress for several reasons:
- Developers often don’t know which external services their applications need to consume (typically cloud services like databases, storage, message queues etc).
- Consuming services outside of the cluster is often based on a DNS name which isn’t supported by Kubernetes network policies (only IP addresses).
- When enforcing egress policies, you must be careful not to block connectivity to essential services like the Kubernetes DNS service.
You should deny outbound traffic from pods that don’t need to connect externally. This can help prevent data exfiltration and downloading of malicious binaries.