@@ -11,33 +11,32 @@ description: >-
11
11
NetworkPolicies allow you to specify rules for traffic flow within your cluster, and
12
12
also between Pods and the outside world.
13
13
Your cluster must use a network plugin that supports NetworkPolicy enforcement.
14
-
15
14
---
16
15
17
16
<!-- overview -->
18
17
19
18
If you want to control traffic flow at the IP address or port level for TCP, UDP, and SCTP protocols,
20
19
then you might consider using Kubernetes NetworkPolicies for particular applications in your cluster.
21
- NetworkPolicies are an application-centric construct which allow you to specify how a {{<
22
- glossary_tooltip text="pod" term_id="pod">}} is allowed to communicate with various network
20
+ NetworkPolicies are an application-centric construct which allow you to specify how a
21
+ {{< glossary_tooltip text="pod" term_id="pod">}} is allowed to communicate with various network
23
22
"entities" (we use the word "entity" here to avoid overloading the more common terms such as
24
23
"endpoints" and "services", which have specific Kubernetes connotations) over the network.
25
24
NetworkPolicies apply to a connection with a pod on one or both ends, and are not relevant to
26
25
other connections.
27
26
28
27
The entities that a Pod can communicate with are identified through a combination of the following
29
- 3 identifiers:
28
+ three identifiers:
30
29
31
30
1 . Other pods that are allowed (exception: a pod cannot block access to itself)
32
- 2 . Namespaces that are allowed
33
- 3 . IP blocks (exception: traffic to and from the node where a Pod is running is always allowed,
31
+ 1 . Namespaces that are allowed
32
+ 1 . IP blocks (exception: traffic to and from the node where a Pod is running is always allowed,
34
33
regardless of the IP address of the Pod or the node)
35
34
36
- When defining a pod- or namespace- based NetworkPolicy, you use a
35
+ When defining a pod- or namespace-based NetworkPolicy, you use a
37
36
{{< glossary_tooltip text="selector" term_id="selector">}} to specify what traffic is allowed to
38
37
and from the Pod(s) that match the selector.
39
38
40
- Meanwhile, when IP based NetworkPolicies are created, we define policies based on IP blocks (CIDR ranges).
39
+ Meanwhile, when IP- based NetworkPolicies are created, we define policies based on IP blocks (CIDR ranges).
41
40
42
41
<!-- body -->
43
42
## Prerequisites
@@ -46,12 +45,12 @@ Network policies are implemented by the [network plugin](/docs/concepts/extend-k
46
45
To use network policies, you must be using a networking solution which supports NetworkPolicy.
47
46
Creating a NetworkPolicy resource without a controller that implements it will have no effect.
48
47
49
- ## The Two Sorts of Pod Isolation
48
+ ## The two sorts of pod isolation
50
49
51
50
There are two sorts of isolation for a pod: isolation for egress, and isolation for ingress.
52
51
They concern what connections may be established. "Isolation" here is not absolute, rather it
53
52
means "some restrictions apply". The alternative, "non-isolated for $direction", means that no
54
- restrictions apply in the stated direction. The two sorts of isolation (or not) are declared
53
+ restrictions apply in the stated direction. The two sorts of isolation (or not) are declared
55
54
independently, and are both relevant for a connection from one pod to another.
56
55
57
56
By default, a pod is non-isolated for egress; all outbound connections are allowed.
@@ -93,7 +92,7 @@ solution supports network policy.
93
92
{{< /note >}}
94
93
95
94
__ Mandatory Fields__ : As with all other Kubernetes config, a NetworkPolicy needs ` apiVersion ` ,
96
- ` kind ` , and ` metadata ` fields. For general information about working with config files, see
95
+ ` kind ` , and ` metadata ` fields. For general information about working with config files, see
97
96
[ Configure a Pod to Use a ConfigMap] ( /docs/tasks/configure-pod-container/configure-pod-configmap/ ) ,
98
97
and [ Object Management] ( /docs/concepts/overview/working-with-objects/object-management ) .
99
98
@@ -227,7 +226,7 @@ that explicitly allows that.
227
226
{{% code_sample file="service/networking/network-policy-allow-all-ingress.yaml" %}}
228
227
229
228
With this policy in place, no additional policy or policies can cause any incoming connection to
230
- those pods to be denied. This policy has no effect on isolation for egress from any pod.
229
+ those pods to be denied. This policy has no effect on isolation for egress from any pod.
231
230
232
231
### Default deny all egress traffic
233
232
@@ -247,7 +246,7 @@ explicitly allows all outgoing connections from pods in that namespace.
247
246
{{% code_sample file="service/networking/network-policy-allow-all-egress.yaml" %}}
248
247
249
248
With this policy in place, no additional policy or policies can cause any outgoing connection from
250
- those pods to be denied. This policy has no effect on isolation for ingress to any pod.
249
+ those pods to be denied. This policy has no effect on isolation for ingress to any pod.
251
250
252
251
### Default deny all ingress and all egress traffic
253
252
@@ -261,8 +260,8 @@ ingress or egress traffic.
261
260
262
261
## Network traffic filtering
263
262
264
- NetworkPolicy is defined for [ layer 4] ( https://en.wikipedia.org/wiki/OSI_model#Layer_4:_Transport_layer )
265
- connections (TCP, UDP, and optionally SCTP). For all the other protocols, the behaviour may vary
263
+ NetworkPolicy is defined for [ layer 4] ( https://en.wikipedia.org/wiki/OSI_model#Layer_4:_Transport_layer )
264
+ connections (TCP, UDP, and optionally SCTP). For all the other protocols, the behaviour may vary
266
265
across network plugins.
267
266
268
267
{{< note >}}
@@ -273,7 +272,7 @@ protocol NetworkPolicies.
273
272
When a ` deny all ` network policy is defined, it is only guaranteed to deny TCP, UDP and SCTP
274
273
connections. For other protocols, such as ARP or ICMP, the behaviour is undefined.
275
274
The same applies to allow rules: when a specific pod is allowed as ingress source or egress destination,
276
- it is undefined what happens with (for example) ICMP packets. Protocols such as ICMP may be allowed by some
275
+ it is undefined what happens with (for example) ICMP packets. Protocols such as ICMP may be allowed by some
277
276
network plugins and denied by others.
278
277
279
278
## Targeting a range of ports
@@ -286,8 +285,8 @@ This is achievable with the usage of the `endPort` field, as the following examp
286
285
287
286
{{% code_sample file="service/networking/networkpolicy-multiport-egress.yaml" %}}
288
287
289
- The above rule allows any Pod with label ` role=db ` on the namespace ` default ` to communicate
290
- with any IP within the range ` 10.0.0.0/24 ` over TCP, provided that the target
288
+ The above rule allows any Pod with label ` role=db ` on the namespace ` default ` to communicate
289
+ with any IP within the range ` 10.0.0.0/24 ` over TCP, provided that the target
291
290
port is between the range 32000 and 32768.
292
291
293
292
The following restrictions apply when using this field:
@@ -299,7 +298,7 @@ The following restrictions apply when using this field:
299
298
{{< note >}}
300
299
Your cluster must be using a {{< glossary_tooltip text="CNI" term_id="cni" >}} plugin that
301
300
supports the ` endPort ` field in NetworkPolicy specifications.
302
- If your [ network plugin] ( /docs/concepts/extend-kubernetes/compute-storage-net/network-plugins/ )
301
+ If your [ network plugin] ( /docs/concepts/extend-kubernetes/compute-storage-net/network-plugins/ )
303
302
does not support the ` endPort ` field and you specify a NetworkPolicy with that,
304
303
the policy will be applied only for the single ` port ` field.
305
304
{{< /note >}}
@@ -310,8 +309,8 @@ In this scenario, your `Egress` NetworkPolicy targets more than one namespace us
310
309
label names. For this to work, you need to label the target namespaces. For example:
311
310
312
311
``` shell
313
- kubectl label namespace frontend namespace=frontend
314
- kubectl label namespace backend namespace=backend
312
+ kubectl label namespace frontend namespace=frontend
313
+ kubectl label namespace backend namespace=backend
315
314
```
316
315
317
316
Add the labels under ` namespaceSelector ` in your NetworkPolicy document. For example:
@@ -360,32 +359,31 @@ NetworkPolicy.
360
359
When a new NetworkPolicy object is created, it may take some time for a network plugin
361
360
to handle the new object. If a pod that is affected by a NetworkPolicy
362
361
is created before the network plugin has completed NetworkPolicy handling,
363
- that pod may be started unprotected, and isolation rules will be applied when
362
+ that pod may be started unprotected, and isolation rules will be applied when
364
363
the NetworkPolicy handling is completed.
365
364
366
365
Once the NetworkPolicy is handled by a network plugin,
367
366
368
- 1. All newly created pods affected by a given NetworkPolicy will be isolated before
369
- they are started.
370
- Implementations of NetworkPolicy must ensure that filtering is effective throughout
371
- the Pod lifecycle, even from the very first instant that any container in that Pod is started.
372
- Because they are applied at Pod level, NetworkPolicies apply equally to init containers,
373
- sidecar containers, and regular containers.
367
+ 1. All newly created pods affected by a given NetworkPolicy will be isolated before they are started.
368
+ Implementations of NetworkPolicy must ensure that filtering is effective throughout
369
+ the Pod lifecycle, even from the very first instant that any container in that Pod is started.
370
+ Because they are applied at Pod level, NetworkPolicies apply equally to init containers,
371
+ sidecar containers, and regular containers.
374
372
375
- 2 . Allow rules will be applied eventually after the isolation rules (or may be applied at the same time).
376
- In the worst case, a newly created pod may have no network connectivity at all when it is first started, if
377
- isolation rules were already applied, but no allow rules were applied yet.
373
+ 1 . Allow rules will be applied eventually after the isolation rules (or may be applied at the same time).
374
+ In the worst case, a newly created pod may have no network connectivity at all when it is first started, if
375
+ isolation rules were already applied, but no allow rules were applied yet.
378
376
379
377
Every created NetworkPolicy will be handled by a network plugin eventually, but there is no
380
378
way to tell from the Kubernetes API when exactly that happens.
381
379
382
380
Therefore, pods must be resilient against being started up with different network
383
- connectivity than expected. If you need to make sure the pod can reach certain destinations
381
+ connectivity than expected. If you need to make sure the pod can reach certain destinations
384
382
before being started, you can use an [init container](/docs/concepts/workloads/pods/init-containers/)
385
383
to wait for those destinations to be reachable before kubelet starts the app containers.
386
384
387
385
Every NetworkPolicy will be applied to all selected pods eventually.
388
- Because the network plugin may implement NetworkPolicy in a distributed manner,
386
+ Because the network plugin may implement NetworkPolicy in a distributed manner,
389
387
it is possible that pods may see a slightly inconsistent view of network policies
390
388
when the pod is first created, or when pods or policies change.
391
389
For example, a newly-created pod that is supposed to be able to reach both Pod A
@@ -395,16 +393,18 @@ but cannot reach Pod B until a few seconds later.
395
393
# # NetworkPolicy and `hostNetwork` pods
396
394
397
395
NetworkPolicy behaviour for `hostNetwork` pods is undefined, but it should be limited to 2 possibilities :
396
+
398
397
- The network plugin can distinguish `hostNetwork` pod traffic from all other traffic
399
398
(including being able to distinguish traffic from different `hostNetwork` pods on
400
399
the same node), and will apply NetworkPolicy to `hostNetwork` pods just like it does
401
400
to pod-network pods.
402
- - The network plugin cannot properly distinguish `hostNetwork` pod traffic,
403
- and so it ignores `hostNetwork` pods when matching `podSelector` and `namespaceSelector`.
404
- Traffic to/from `hostNetwork` pods is treated the same as all other traffic to/from the node IP.
401
+ - The network plugin cannot properly distinguish `hostNetwork` pod traffic,
402
+ and so it ignores `hostNetwork` pods when matching `podSelector` and `namespaceSelector`.
403
+ Traffic to/from `hostNetwork` pods is treated the same as all other traffic to/from the node IP.
405
404
(This is the most common implementation.)
406
405
407
406
This applies when
407
+
408
408
1. a `hostNetwork` pod is selected by `spec.podSelector`.
409
409
410
410
` ` ` yaml
@@ -416,7 +416,7 @@ This applies when
416
416
...
417
417
` ` `
418
418
419
- 2 . a `hostNetwork` pod is selected by a `podSelector` or `namespaceSelector` in an `ingress` or `egress` rule.
419
+ 1 . a `hostNetwork` pod is selected by a `podSelector` or `namespaceSelector` in an `ingress` or `egress` rule.
420
420
421
421
` ` ` yaml
422
422
...
@@ -437,7 +437,7 @@ from a `hostNetwork` Pod using an `ipBlock` rule.
437
437
As of Kubernetes {{< skew currentVersion >}}, the following functionality does not exist in the
438
438
NetworkPolicy API, but you might be able to implement workarounds using Operating System
439
439
components (such as SELinux, OpenVSwitch, IPTables, and so on) or Layer 7 technologies (Ingress
440
- controllers, Service Mesh implementations) or admission controllers. In case you are new to
440
+ controllers, Service Mesh implementations) or admission controllers. In case you are new to
441
441
network security in Kubernetes, its worth noting that the following User Stories cannot (yet) be
442
442
implemented using the NetworkPolicy API.
443
443
0 commit comments