Urgent.News

What's breaking now, across thousands of outlets.

Tech

Kubernetes NetworkPolicy: isolate and test traffic

Picture a Friday evening: a container in your staging namespace starts querying the prod database, for the plain reason that nothing stops it. In Kubernetes that is the default behaviour. A Kubernetes NetworkPolicy is how you put walls where the cluster has none, and this post follows the routine I would suggest: lock a namespace down, reopen only the doors you need, then prove each wall holds.…

In a Kubernetes cluster, a pod that is not selected by any NetworkPolicy is allowed to communicate both in and out of the cluster by default. This default behavior can pose a security risk, as demonstrated by a container in a staging namespace being able to query the production database. To mitigate this risk, a Kubernetes NetworkPolicy can be used to restrict traffic flow.

A NetworkPolicy is created by defining a podSelector to select the pods it applies to, and specifying the policyTypes (ingress or egress) and rules for allowed connections. The podSelector is set to {} to apply the policy to all pods in the namespace. The rules are defined by pods, namespaces, IP ranges, and optional ports, with optional ingress and egress rules.

When creating a NetworkPolicy, it is important to follow best practices to avoid unintended consequences. The policy should only grant the necessary permissions and avoid the use of deny rules, as these are not supported. Instead, the focus should be on granting only the required permissions. Additionally, the policy should be tested thoroughly to ensure that it is functioning as intended and that no unintended connections are allowed.

One common mistake is using a full egress deny, which can also cut off name resolution for applications. To avoid this issue, a second policy can be added to allow egress to the cluster DNS. It is important to note that the NetworkPolicy does not provide logging of network security events and relies on the CNI (Container Network Interface) for observability.

When implementing NetworkPolicies, it is recommended to follow a rollout order, starting with a default-deny-all policy for each namespace, followed by specific DNS rules. Each policy should be named after its purpose and versioned in Git. Finally, each rule should have both a passing and failing test to ensure that it is functioning correctly.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Secret Scanner False Positives From Whitespace Stripping

A secret scanner has to catch keys that a line break has cut in two. The fast way to do that is to delete every whitespace character and run the key patterns on what is left.

  • Scanner struggles with API key matches due to whitespace stripping
  • Fix verifies stripped match against original text
  • Scanner re-runs pattern on original span with whitespace runs

More from Saturday 14 November →