Network Policies
If you want to control traffic flow at the IP address or port level (OSI layer 3 or 4), then you might consider using Kubernetes NetworkPolicies for particular applications in your cluster. NetworkPolicies are an application-centric construct which allow you to specify how a pod is allowed to communicate with various network "entities" (we use the word "entity" here to avoid overloading the more common terms such as "endpoints" and "services", which have specific Kubernetes connotations) over the network. NetworkPolicies apply to a connection with a pod on one or both ends, and are not relevant to other connections.
The entities that a Pod can communicate with are identified through a combination of the following 3 identifiers:
- Other pods that are allowed (exception: a pod cannot block access to itself)
- Namespaces that are allowed
- IP blocks (exception: traffic to and from the node where a Pod is running is always allowed, regardless of the IP address of the Pod or the node)
When defining a pod- or namespace- based NetworkPolicy, you use a selector to specify what traffic is allowed to and from the Pod(s) that match the selector.
Meanwhile, when IP based NetworkPolicies are created, we define policies based on IP blocks (CIDR ranges).
Prerequisites
Network policies are implemented by the network plugin . To use network policies, you must be using a networking solution which supports NetworkPolicy. Creating a NetworkPolicy resource without a controller that implements it will have no effect.
The Two Sorts of Pod Isolation
There are two sorts of isolation for a pod: isolation for egress, and isolation for ingress. They concern what connections may be established. "Isolation" here is not absolute, rather it means "some restrictions apply". The alternative, "non-isolated for $direction", means that no restrictions apply in the stated direction. The two sorts of isolation (or not) are declared independently, and are both relevant for a connection from one pod to another.
By default, a pod is non-isolated for egress; all outbound connections are allowed.
A pod is isolated for egress if there is any NetworkPolicy that both selects the pod and has
"Egress" in its
policyTypes
; we say that such a policy applies to the pod for egress.
When a pod is isolated for egress, the only allowed connections from the pod are those allowed by
the
egress
list of some NetworkPolicy that applies to the pod for egress.
The effects of those
egress
lists combine additively.
By default, a pod is non-isolated for ingress; all inbound connections are allowed.
A pod is isolated for ingress if there is any NetworkPolicy that both selects the pod and
has "Ingress" in its
policyTypes
; we say that such a policy applies to the pod for ingress.
When a pod is isolated for ingress, the only allowed connections into the pod are those from
the pod's node and those allowed by the
ingress
list of some NetworkPolicy that applies to
the pod for ingress. The effects of those
ingress
lists combine additively.
Network policies do not conflict; they are additive. If any policy or policies apply to a given pod for a given direction, the connections allowed in that direction from that pod is the union of what the applicable policies allow. Thus, order of evaluation does not affect the policy result.
For a connection from a source pod to a destination pod to be allowed, both the egress policy on the source pod and the ingress policy on the destination pod need to allow the connection. If either side does not allow the connection, it will not happen.
The NetworkPolicy resource
See the NetworkPolicy reference for a full definition of the resource.
An example NetworkPolicy might look like this:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: test-network-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
Mandatory Fields
: As with all other Kubernetes config, a NetworkPolicy needs
apiVersion
,
kind
, and
metadata
fields. For general information about working with config files, see
Configure a Pod to Use a ConfigMap
,
and
Object Management
.
spec : NetworkPolicy spec has all the information needed to define a particular network policy in the given namespace.
podSelector
: Each NetworkPolicy includes a
podSelector
which selects the grouping of pods to
which the policy applies. The example policy selects pods with the label "role=db". An empty
podSelector
selects all pods in the namespace.
policyTypes
: Each NetworkPolicy includes a
policyTypes
list which may include either
Ingress
,
Egress
, or both. The
policyTypes
field indicates whether or not the given policy
applies to ingress traffic to selected pod, egress traffic from selected pods, or both. If no
policyTypes
are specified on a NetworkPolicy then by default
Ingress
will always be set and
Egress
will be set if the NetworkPolicy has any egress rules.
ingress
: Each NetworkPolicy may include a list of allowed
ingress
rules. Each rule allows
traffic which matches both the
from
and
ports
sections. The example policy contains a single
rule, which matches traffic on a single port, from one of three sources, the first specified via
an
ipBlock
, the second via a
namespaceSelector
and the third via a
podSelector
.
egress
: Each NetworkPolicy may include a list of allowed
egress
rules. Each rule allows
traffic which matches both the
to
and
ports
sections. The example policy contains a single
rule, which matches traffic on a single port to any destination in
10.0.0.0/24
.
So, the example NetworkPolicy:
-
isolates
role=db
pods in thedefault
namespace for both ingress and egress traffic (if they weren't already isolated) -
(Ingress rules) allows connections to all pods in the
default
namespace with the labelrole=db
on TCP port 6379 from:-
any pod in the
default
namespace with the labelrole=frontend
-
any pod in a namespace with the label
project=myproject
-
IP addresses in the ranges
172.17.0.0
–172.17.0.255
and172.17.2.0
–172.17.255.255
(ie, all of172.17.0.0/16
except172.17.1.0/24
)
-
any pod in the
-
(Egress rules) allows connections from any pod in the
default
namespace with the labelrole=db
to CIDR10.0.0.0/24
on TCP port 5978
See the Declare Network Policy walkthrough for further examples.
Behavior of
to
and
from
selectors
There are four kinds of selectors that can be specified in an
ingress
from
section or
egress
to
section:
podSelector : This selects particular Pods in the same namespace as the NetworkPolicy which should be allowed as ingress sources or egress destinations.
namespaceSelector : This selects particular namespaces for which all Pods should be allowed as ingress sources or egress destinations.
namespaceSelector
and
podSelector
: A single
to
/
from
entry that specifies both
namespaceSelector
and
podSelector
selects particular Pods within particular namespaces. Be
careful to use correct YAML syntax. For example:
...
ingress:
- from:
- namespaceSelector:
matchLabels:
user: alice
podSelector:
matchLabels:
role: client
This policy contains a single
from
element allowing connections from Pods with the label
role=client
in namespaces with the label
user=alice
. But the following policy is different:
...
ingress:
- from:
- namespaceSelector:
matchLabels:
user: alice
- podSelector:
matchLabels:
role: client
It contains two elements in the
from
array, and allows connections from Pods in the local
Namespace with the label
role=client
,
or
from any Pod in any namespace with the label
user=alice
.
When in doubt, use
kubectl describe
to see how Kubernetes has interpreted the policy.
Default policies
Default deny all ingress traffic
This ensures that even pods that aren't selected by any other NetworkPolicy will still be isolated for ingress. This policy does not affect isolation for egress from any pod.
Allow all ingress traffic
If you want to allow all incoming connections to all pods in a namespace, you can create a policy that explicitly allows that.