NetworkPolicy updates for v1 (#3721)
* NetworkPolicy clarifications - For clarity, only use the word "policy" in reference to NetworkPolicies, not in reference to the isolation annotation. - Drop a bunch of text related to the isolation annotation since there's only one interesting value so there's no reason to complicate things. - Fix bad YAML indentation - Misc rewording * Update NetworkPolicy docs for v1
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
---
|
||||
assignees:
|
||||
- caseydavenport
|
||||
- danwinship
|
||||
title: Declaring Network Policy
|
||||
redirect_from:
|
||||
- "/docs/getting-started-guides/network-policy/walkthrough/"
|
||||
@@ -46,7 +47,7 @@ po/nginx-701339712-o00ef 1/1 Running 0 35s
|
||||
```
|
||||
|
||||
We should be able to access our new nginx Service from other Pods. Let's try to access it from another Pod
|
||||
in the default namespace. We haven't put any network policy in place, so this should just work. Start a
|
||||
in the default namespace. We haven't enabled isolation on the namespace, so this should just work. Start a
|
||||
busybox container, and use `wget` to hit the nginx Service:
|
||||
|
||||
```console
|
||||
@@ -64,16 +65,28 @@ Let's say we want to limit access to our nginx Service so that only pods with th
|
||||
enable ingress isolation on the `default` Namespace. This will prevent _any_ pods from accessing the nginx Service.
|
||||
|
||||
```console
|
||||
$ kubectl annotate ns default "net.beta.kubernetes.io/network-policy={\"ingress\": {\"isolation\": \"DefaultDeny\"}}"
|
||||
$ kubectl patch ns default -p '{"spec": {"networkPolicy": {"ingress": {"isolation": "DefaultDeny"}}}}'
|
||||
```
|
||||
|
||||
With ingress isolation in place, we should no longer be able to access the nginx Service like we were able to before.
|
||||
With ingress isolation in place, we should no longer be able to access the nginx Service like we were able to before:
|
||||
|
||||
```console
|
||||
$ kubectl run busybox --rm -ti --image=busybox /bin/sh
|
||||
Waiting for pod default/busybox-472357175-y0m47 to be running, status is Pending, pod ready: false
|
||||
|
||||
Hit enter for command prompt
|
||||
|
||||
/ # wget --spider --timeout=1 nginx
|
||||
Connecting to nginx (10.100.0.16:80)
|
||||
wget: download timed out
|
||||
/ #
|
||||
```
|
||||
|
||||
Let's now create a `NetworkPolicy` which allows connections from pods with the label `access: true`.
|
||||
|
||||
```yaml
|
||||
kind: NetworkPolicy
|
||||
apiVersion: extensions/v1beta1
|
||||
apiVersion: networking/v1
|
||||
metadata:
|
||||
name: access-nginx
|
||||
spec:
|
||||
@@ -81,19 +94,19 @@ spec:
|
||||
matchLabels:
|
||||
run: nginx
|
||||
ingress:
|
||||
- from:
|
||||
- podSelector:
|
||||
matchLabels:
|
||||
access: "true"
|
||||
- from:
|
||||
- podSelector:
|
||||
matchLabels:
|
||||
access: "true"
|
||||
```
|
||||
|
||||
Use kubectl to create the above nginx-policy.yaml file:
|
||||
Use kubectl to create a NetworkPolicy from the above nginx-policy.yaml file:
|
||||
```console
|
||||
$ kubectl create -f nginx-policy.yaml
|
||||
networkpolicy "access-nginx" created
|
||||
```
|
||||
|
||||
If we attempt to access the nginx Service from a pod without the correct labels, the request will timeout:
|
||||
If we attempt to access the nginx Service from a pod without the correct labels, the request will still time out:
|
||||
|
||||
```console
|
||||
$ kubectl run busybox --rm -ti --image=busybox /bin/sh
|
||||
|
||||
Reference in New Issue
Block a user