Skip to content

Commit 0936272

Browse files
Update NetworkPolicy concept guide with egress and CIDR changes
1 parent 99fbc2b commit 0936272

File tree

1 file changed

+64
-4
lines changed

1 file changed

+64
-4
lines changed

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

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ spec:
3939
podSelector:
4040
matchLabels:
4141
role: db
42+
policyTypes:
43+
- Ingress
44+
- Egress
4245
ingress:
4346
- from:
4447
- namespaceSelector:
@@ -50,6 +53,13 @@ spec:
5053
ports:
5154
- protocol: TCP
5255
port: 6379
56+
egress:
57+
- to:
58+
- ipBlock:
59+
cidr: 10.0.0.0/24
60+
ports:
61+
- protocol: TCP
62+
port: 5978
5363
```
5464
5565
*POSTing this to the API server will have no effect unless your chosen networking solution supports network policy.*
@@ -60,7 +70,11 @@ __spec__: `NetworkPolicy` [spec](https://git.k8s.io/community/contributors/devel
6070

6171
__podSelector__: Each `NetworkPolicy` includes a `podSelector` which selects the grouping of pods to which the policy applies. Since `NetworkPolicy` currently only supports defining `ingress` rules, this `podSelector` essentially defines the "destination pods" for the policy. The example policy selects pods with the label "role=db". An empty `podSelector` selects all pods in the namespace.
6272

63-
__ingress__: Each `NetworkPolicy` includes a list of whitelist `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 either of two sources, the first specified via a `namespaceSelector` and the second specified via a `podSelector`.
73+
__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.
74+
75+
__ingress__: Each `NetworkPolicy` may include a list of whitelist `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 either of two sources, the first specified via a `namespaceSelector` and the second specified via a `podSelector`.
76+
77+
__egress__: Each `NetworkPolicy` may include a list of whitelist `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`.
6478

6579
So, the example NetworkPolicy:
6680

@@ -72,7 +86,12 @@ See the [NetworkPolicy getting started guide](/docs/getting-started-guides/netwo
7286

7387
## Default policies
7488

75-
You can create a "default" isolation policy for a Namespace by creating a NetworkPolicy that selects all pods but does not allow any traffic:
89+
By default, if no policies exist in a namespace, then all ingress and egress traffic is allowed to and from pods in that Namespace. The following examples let you change the default behavior
90+
for a given namespace.
91+
92+
### Default deny all ingress traffic
93+
94+
You can create a "default" isolation policy for a Namespace by creating a NetworkPolicy that selects all pods but does not allow any ingress traffic:
7695

7796
```yaml
7897
apiVersion: networking.k8s.io/v1
@@ -81,11 +100,16 @@ metadata:
81100
name: default-deny
82101
spec:
83102
podSelector:
103+
policyTypes:
104+
- Ingress
84105
```
85106

86-
This ensures that even pods that aren't selected by any other NetworkPolicy will still be isolated.
107+
This ensures that even pods that aren't selected by any other NetworkPolicy will still be isolated. This policy does not change
108+
the default egress isolation behavior.
87109

88-
Alternatively, if you want to allow all traffic for all pods in a Namespace (even if policies are added that cause some pods to be treated as "isolated"), you can create a policy that explicitly allows all traffic:
110+
### Default allow all ingress traffic
111+
112+
If you want to allow all traffic for all pods in a Namespace (even if policies are added that cause some pods to be treated as "isolated"), you can create a policy that explicitly allows all traffic:
89113

90114
```yaml
91115
apiVersion: networking.k8s.io/v1
@@ -98,6 +122,42 @@ spec:
98122
- {}
99123
```
100124

125+
### Default deny all egress traffic.
126+
127+
You can create a "default" egress isolation policy for a Namespace by creating a NetworkPolicy that selects all pods but does not allow any egress traffic:
128+
129+
```yaml
130+
apiVersion: networking.k8s.io/v1
131+
kind: NetworkPolicy
132+
metadata:
133+
name: default-deny
134+
spec:
135+
podSelector:
136+
policyTypes:
137+
- Egress
138+
```
139+
140+
This ensures that even pods that aren't selected by any other NetworkPolicy will not be allowed egress traffic. This policy does not
141+
change the default ingress isolation behavior.
142+
143+
### Default deny all ingress and all egress traffic
144+
145+
You can create a "default" policy for a Namespace which prevents all ingress AND egress traffic by creating the following NetworkPolicy:
146+
147+
```yaml
148+
apiVersion: networking.k8s.io/v1
149+
kind: NetworkPolicy
150+
metadata:
151+
name: default-deny
152+
spec:
153+
podSelector:
154+
policyTypes:
155+
- Ingress
156+
- Egress
157+
```
158+
159+
This ensures that even pods that aren't selected by any other NetworkPolicy will not be allowed ingress or egress traffic.
160+
101161
## What's next?
102162

103163
- See the [Declare Network Policy](/docs/tasks/administer-cluster/declare-network-policy/)

0 commit comments

Comments
 (0)