Skip to content

Commit 694ff27

Browse files
authored
Merge pull request #45222 from windsonsea/netplc
Minor improvements to network-policies concept
2 parents ed3c881 + d952522 commit 694ff27

File tree

1 file changed

+38
-38
lines changed

1 file changed

+38
-38
lines changed

content/en/docs/concepts/services-networking/network-policies.md

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,32 @@ description: >-
1111
NetworkPolicies allow you to specify rules for traffic flow within your cluster, and
1212
also between Pods and the outside world.
1313
Your cluster must use a network plugin that supports NetworkPolicy enforcement.
14-
1514
---
1615

1716
<!-- overview -->
1817

1918
If you want to control traffic flow at the IP address or port level for TCP, UDP, and SCTP protocols,
2019
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
2322
"entities" (we use the word "entity" here to avoid overloading the more common terms such as
2423
"endpoints" and "services", which have specific Kubernetes connotations) over the network.
2524
NetworkPolicies apply to a connection with a pod on one or both ends, and are not relevant to
2625
other connections.
2726

2827
The entities that a Pod can communicate with are identified through a combination of the following
29-
3 identifiers:
28+
three identifiers:
3029

3130
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,
3433
regardless of the IP address of the Pod or the node)
3534

36-
When defining a pod- or namespace- based NetworkPolicy, you use a
35+
When defining a pod- or namespace-based NetworkPolicy, you use a
3736
{{< glossary_tooltip text="selector" term_id="selector">}} to specify what traffic is allowed to
3837
and from the Pod(s) that match the selector.
3938

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).
4140

4241
<!-- body -->
4342
## Prerequisites
@@ -46,12 +45,12 @@ Network policies are implemented by the [network plugin](/docs/concepts/extend-k
4645
To use network policies, you must be using a networking solution which supports NetworkPolicy.
4746
Creating a NetworkPolicy resource without a controller that implements it will have no effect.
4847

49-
## The Two Sorts of Pod Isolation
48+
## The two sorts of pod isolation
5049

5150
There are two sorts of isolation for a pod: isolation for egress, and isolation for ingress.
5251
They concern what connections may be established. "Isolation" here is not absolute, rather it
5352
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
5554
independently, and are both relevant for a connection from one pod to another.
5655

5756
By default, a pod is non-isolated for egress; all outbound connections are allowed.
@@ -93,7 +92,7 @@ solution supports network policy.
9392
{{< /note >}}
9493

9594
__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
9796
[Configure a Pod to Use a ConfigMap](/docs/tasks/configure-pod-container/configure-pod-configmap/),
9897
and [Object Management](/docs/concepts/overview/working-with-objects/object-management).
9998

@@ -227,7 +226,7 @@ that explicitly allows that.
227226
{{% code_sample file="service/networking/network-policy-allow-all-ingress.yaml" %}}
228227

229228
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.
231230

232231
### Default deny all egress traffic
233232

@@ -247,7 +246,7 @@ explicitly allows all outgoing connections from pods in that namespace.
247246
{{% code_sample file="service/networking/network-policy-allow-all-egress.yaml" %}}
248247

249248
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.
251250

252251
### Default deny all ingress and all egress traffic
253252

@@ -261,8 +260,8 @@ ingress or egress traffic.
261260

262261
## Network traffic filtering
263262

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
266265
across network plugins.
267266

268267
{{< note >}}
@@ -273,7 +272,7 @@ protocol NetworkPolicies.
273272
When a `deny all` network policy is defined, it is only guaranteed to deny TCP, UDP and SCTP
274273
connections. For other protocols, such as ARP or ICMP, the behaviour is undefined.
275274
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
277276
network plugins and denied by others.
278277

279278
## Targeting a range of ports
@@ -286,8 +285,8 @@ This is achievable with the usage of the `endPort` field, as the following examp
286285

287286
{{% code_sample file="service/networking/networkpolicy-multiport-egress.yaml" %}}
288287

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
291290
port is between the range 32000 and 32768.
292291

293292
The following restrictions apply when using this field:
@@ -299,7 +298,7 @@ The following restrictions apply when using this field:
299298
{{< note >}}
300299
Your cluster must be using a {{< glossary_tooltip text="CNI" term_id="cni" >}} plugin that
301300
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/)
303302
does not support the `endPort` field and you specify a NetworkPolicy with that,
304303
the policy will be applied only for the single `port` field.
305304
{{< /note >}}
@@ -310,8 +309,8 @@ In this scenario, your `Egress` NetworkPolicy targets more than one namespace us
310309
label names. For this to work, you need to label the target namespaces. For example:
311310

312311
```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
315314
```
316315

317316
Add the labels under `namespaceSelector` in your NetworkPolicy document. For example:
@@ -360,32 +359,31 @@ NetworkPolicy.
360359
When a new NetworkPolicy object is created, it may take some time for a network plugin
361360
to handle the new object. If a pod that is affected by a NetworkPolicy
362361
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
364363
the NetworkPolicy handling is completed.
365364

366365
Once the NetworkPolicy is handled by a network plugin,
367366

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.
374372

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.
378376

379377
Every created NetworkPolicy will be handled by a network plugin eventually, but there is no
380378
way to tell from the Kubernetes API when exactly that happens.
381379

382380
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
384382
before being started, you can use an [init container](/docs/concepts/workloads/pods/init-containers/)
385383
to wait for those destinations to be reachable before kubelet starts the app containers.
386384

387385
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,
389387
it is possible that pods may see a slightly inconsistent view of network policies
390388
when the pod is first created, or when pods or policies change.
391389
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.
395393
## NetworkPolicy and `hostNetwork` pods
396394

397395
NetworkPolicy behaviour for `hostNetwork` pods is undefined, but it should be limited to 2 possibilities:
396+
398397
- The network plugin can distinguish `hostNetwork` pod traffic from all other traffic
399398
(including being able to distinguish traffic from different `hostNetwork` pods on
400399
the same node), and will apply NetworkPolicy to `hostNetwork` pods just like it does
401400
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.
405404
(This is the most common implementation.)
406405

407406
This applies when
407+
408408
1. a `hostNetwork` pod is selected by `spec.podSelector`.
409409

410410
```yaml
@@ -416,7 +416,7 @@ This applies when
416416
...
417417
```
418418

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.
420420

421421
```yaml
422422
...
@@ -437,7 +437,7 @@ from a `hostNetwork` Pod using an `ipBlock` rule.
437437
As of Kubernetes {{< skew currentVersion >}}, the following functionality does not exist in the
438438
NetworkPolicy API, but you might be able to implement workarounds using Operating System
439439
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
441441
network security in Kubernetes, its worth noting that the following User Stories cannot (yet) be
442442
implemented using the NetworkPolicy API.
443443

0 commit comments

Comments
 (0)