Network_Security_Policies
Network Security Policies
By default, all Pod
s can reach each other; all ingress and egress traffic is allowed. This has been a high-level networking requirement in Kubernetes. However, network isolation can be configured and traffic to Pod
s can be blocked. In newer versions of Kubernetes, egress traffic can also be blocked. This is done by configuring a NetworkPolicy
. As all traffic is allowed, you may want to implement a policy that drops all traffic, then, other policies which allow desired ingress and egress traffic.
The spec of the policy can narrow down the effect to a particular namespace, which can be handy. Further settings include a podSelector
, or label
, to narrow down which Pod
s are affected. Further ingress and egress settings declare traffic to and from IP addresses and ports.
Not all network providers support the NetworkPolicies
kind. A non-exhaustive list of providers with support includes Calico, Romana, Cilium, Kube-router, and WeaveNet.
In previous versions of Kubernetes, there was a requirement to annotate a namespace as part of network isolation, specifically the net.beta.kubernetes.io/network-policy=
value. Some network plugins may still require this setting.
Network Security Example
The use of policies has become stable, noted with the v1 apiVersion
. The example below narrows down the policy to affect the default namespace.
Only Pod
s with the label
of role: db
will be affected by this policy, and the policy has both Ingress
and Egress
settings.
The ingress setting includes a 172.17
network, with a smaller range of 172.17.1.0
IPs being excluded from this traffic.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: ingress-egress-policy
namespace: default
spec:
podSelector:
matchLabels:
role: db
policyTypes:
- Ingress
- Egress
ingress:
- from:
- ipBlock:
cidr: 172.17.0.0/16
except:
- 172.17.1.0/24
- namespaceSelector:
matchLabels:
project: myproject
- podSelector:
matchLabels:
role: frontend
ports:
- protocol: TCP
port: 6379
egress:
- to:
- ipBlock:
cidr: 10.0.0.0/24
ports:
- protocol: TCP
port: 5978
These rules change the namespace for the following settings to be labeled project: myproject
. The affected Pod
s also would need to match the label role: frontend
. Finally, TCP traffic on port 6379
would be allowed from these Pod
s.
The egress rules have the to
settings, in this case the 10.0.0.0/24
range TCP traffic to port 5978
.
The use of empty ingress or egress rules denies all type of traffic for the included Pods, though this is not suggested. Use another dedicated NetworkPolicy
instead.
Note that there can also be complex matchExpressions
statements in the spec, but this may change as NetworkPolicy
matures.
podSelector:
matchExpressions:
- {key: inns, operator: In, values: ["yes"]}
More network policy recipes can be found on GitHub.
Default Policy Example
The empty braces will match all Pod
s not selected by other NetworkPolicy
and will not allow ingress traffic. Egress traffic would be unaffected by this policy.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny
spec:
podSelector: {}
policyTypes:
- Ingress
With the potential for complex ingress and egress rules, it may be helpful to create multiple objects which include simple isolation rules and use easy to understand names and labels.
Some network plugins, such as WeaveNet, may require annotation of the Namespace
. The following shows the setting of a DefaultDeny
for the myns
namespace:
kind: Namespace
apiVersion: v1
metadata:
name: myns
annotations:
net.beta.kubernetes.io/network-policy: |
{
"ingress": {
"isolation": "DefaultDeny"
}
}