2017-04-10 17:26:28 +00:00
---
2018-02-18 19:29:37 +00:00
reviewers:
2017-04-10 17:26:28 +00:00
- caseydavenport
2017-05-11 15:08:55 +00:00
- danwinship
2017-06-08 22:13:29 +00:00
title: Declare Network Policy
2018-05-05 16:00:51 +00:00
content_template: templates/task
2017-04-10 17:26:28 +00:00
---
2018-05-05 16:00:51 +00:00
{{% capture overview %}}
2017-08-16 13:09:07 +00:00
This document helps you get started using the Kubernetes [NetworkPolicy API ](/docs/concepts/services-networking/network-policies/ ) to declare network policies that govern how pods communicate with each other.
2018-05-05 16:00:51 +00:00
{{% /capture %}}
2017-04-10 17:26:28 +00:00
2018-05-05 16:00:51 +00:00
{{% capture prerequisites %}}
2017-04-10 17:26:28 +00:00
2018-10-11 21:21:04 +00:00
{{< include " task-tutorial-prereqs . md " > }} {{< version-check > }}
2017-04-10 17:26:28 +00:00
2018-10-11 21:21:04 +00:00
Make sure you've configured a network provider with network policy support. There are a number of network providers that support NetworkPolicy, including:
2017-05-14 22:10:50 +00:00
2018-10-11 21:21:04 +00:00
* [Calico ](/docs/tasks/administer-cluster/network-policy-provider/calico-network-policy/ )
* [Cilium ](/docs/tasks/administer-cluster/network-policy-provider/cilium-network-policy/ )
* [Kube-router ](/docs/tasks/administer-cluster/network-policy-provider/kube-router-network-policy/ )
* [Romana ](/docs/tasks/administer-cluster/network-policy-provider/romana-network-policy/ )
* [Weave Net ](/docs/tasks/administer-cluster/network-policy-provider/weave-network-policy/ )
2018-06-22 18:20:04 +00:00
2018-11-06 19:33:04 +00:00
{{< note > }}
The above list is sorted alphabetically by product name, not by recommendation or preference. This example is valid for a Kubernetes cluster using any of these providers.
{{< / note > }}
2018-06-22 18:20:04 +00:00
{{% /capture %}}
2017-04-10 17:26:28 +00:00
2018-05-05 16:00:51 +00:00
{{% capture steps %}}
2017-04-10 17:26:28 +00:00
2017-05-14 22:10:50 +00:00
## Create an `nginx` deployment and expose it via a service
2017-04-10 17:26:28 +00:00
2017-07-28 15:23:11 +00:00
To see how Kubernetes network policy works, start off by creating an `nginx` deployment and exposing it via a service.
2017-04-10 17:26:28 +00:00
```console
$ kubectl run nginx --image=nginx --replicas=2
2018-08-08 22:10:30 +00:00
deployment.apps/nginx created
2017-07-28 15:23:11 +00:00
$ kubectl expose deployment nginx --port=80
2018-08-08 22:10:30 +00:00
service/nginx exposed
2017-04-10 17:26:28 +00:00
```
2017-07-28 15:23:11 +00:00
This runs two `nginx` pods in the default namespace, and exposes them through a service called `nginx` .
2017-04-10 17:26:28 +00:00
```console
$ kubectl get svc,pod
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
2018-08-08 22:10:30 +00:00
service/kubernetes 10.100.0.1 < none > 443/TCP 46m
service/nginx 10.100.0.16 < none > 80/TCP 33s
2017-04-10 17:26:28 +00:00
NAME READY STATUS RESTARTS AGE
2018-08-08 22:10:30 +00:00
pod/nginx-701339712-e0qfq 1/1 Running 0 35s
pod/nginx-701339712-o00ef 1/1 Running 0 35s
2017-04-10 17:26:28 +00:00
```
2017-05-14 22:10:50 +00:00
## Test the service by accessing it from another pod
You should be able to access the new `nginx` service from other pods. To test, access the service from another pod in the default namespace. Make sure you haven't enabled isolation on the namespace.
Start a busybox container, and use `wget` on the `nginx` service:
2017-04-10 17:26:28 +00:00
```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)
/ #
```
2017-05-14 22:10:50 +00:00
## Limit access to the `nginx` service
2017-05-11 15:08:55 +00:00
Let's say you want to limit access to the `nginx` service so that only pods with the label `access: true` can query it. To do that, create a `NetworkPolicy` that allows connections only from those pods:
2017-04-10 17:26:28 +00:00
```yaml
kind: NetworkPolicy
2017-06-30 04:49:53 +00:00
apiVersion: networking.k8s.io/v1
2017-04-10 17:26:28 +00:00
metadata:
name: access-nginx
spec:
podSelector:
matchLabels:
run: nginx
ingress:
2017-05-11 20:26:32 +00:00
- from:
- podSelector:
matchLabels:
access: "true"
2017-04-10 17:26:28 +00:00
```
2017-05-14 22:10:50 +00:00
## Assign the policy to the service
2017-05-11 20:26:32 +00:00
Use kubectl to create a NetworkPolicy from the above nginx-policy.yaml file:
2017-08-22 12:37:50 +00:00
2017-04-10 17:26:28 +00:00
```console
$ kubectl create -f nginx-policy.yaml
2018-08-08 22:10:30 +00:00
networkpolicy.networking.k8s.io/access-nginx created
2017-04-10 17:26:28 +00:00
```
2017-05-14 22:10:50 +00:00
## Test access to the service when access label is not defined
2017-05-11 15:08:55 +00:00
If we attempt to access the nginx Service from a pod without the correct labels, the request will now time out:
2017-04-10 17:26:28 +00:00
```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
2017-07-28 15:23:11 +00:00
/ # wget --spider --timeout=1 nginx
2017-04-10 17:26:28 +00:00
Connecting to nginx (10.100.0.16:80)
wget: download timed out
/ #
```
2017-05-14 22:10:50 +00:00
## Define access label and test again
Create a pod with the correct labels, and you'll see that the request is allowed:
2017-04-10 17:26:28 +00:00
```console
$ kubectl run busybox --rm -ti --labels="access=true" --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)
/ #
```
2018-05-05 16:00:51 +00:00
{{% /capture %}}
2017-05-14 22:10:50 +00:00