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
2020-02-12 19:06:51 +00:00
min-kubernetes-server-version: v1.8
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
2020-02-12 19:06:51 +00:00
To see how Kubernetes network policy works, start off by creating an `nginx` Deployment.
2017-04-10 17:26:28 +00:00
```console
2019-10-09 20:25:49 +00:00
kubectl create deployment nginx --image=nginx
2018-12-23 01:20:33 +00:00
```
```none
2018-08-08 22:10:30 +00:00
deployment.apps/nginx created
2018-12-23 01:20:33 +00:00
```
2020-02-12 19:06:51 +00:00
Expose the Deployment through a Service called `nginx` .
2018-12-23 01:20:33 +00:00
```console
kubectl expose deployment nginx --port=80
```
```none
2018-08-08 22:10:30 +00:00
service/nginx exposed
2017-04-10 17:26:28 +00:00
```
2020-02-12 19:06:51 +00:00
The above commands create a Deployment with an nginx Pod and expose the Deployment through a Service named `nginx` . The `nginx` Pod and Deployment are found in the `default` namespace.
2017-04-10 17:26:28 +00:00
```console
2018-12-23 01:20:33 +00:00
kubectl get svc,pod
```
```none
2017-04-10 17:26:28 +00:00
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
2017-04-10 17:26:28 +00:00
```
2020-02-12 19:06:51 +00:00
## Test the service by accessing it from another Pod
2017-05-14 22:10:50 +00:00
2020-02-12 19:06:51 +00:00
You should be able to access the new `nginx` service from other Pods. To access the `nginx` Service from another Pod in the `default` namespace, start a busybox container:
2017-04-10 17:26:28 +00:00
```console
2019-10-09 20:25:49 +00:00
kubectl run --generator=run-pod/v1 busybox --rm -ti --image=busybox -- /bin/sh
2018-12-23 01:20:33 +00:00
```
2020-02-12 19:06:51 +00:00
In your shell, run the following command:
2017-04-10 17:26:28 +00:00
2020-02-12 19:06:51 +00:00
```shell
wget --spider --timeout=1 nginx
```
2017-04-10 17:26:28 +00:00
2020-02-12 19:06:51 +00:00
```none
2017-04-10 17:26:28 +00:00
Connecting to nginx (10.100.0.16:80)
2020-02-12 19:06:51 +00:00
remote file exists
2017-04-10 17:26:28 +00:00
```
2017-05-14 22:10:50 +00:00
## Limit access to the `nginx` service
2020-02-12 19:06:51 +00:00
To limit the access to the `nginx` service so that only Pods with the label `access: true` can query it, create a NetworkPolicy object as follows:
2017-04-10 17:26:28 +00:00
2020-02-12 19:06:51 +00:00
{{< codenew file = "service/networking/nginx-policy.yaml" > }}
2017-04-10 17:26:28 +00:00
2020-03-05 19:28:38 +00:00
The name of a NetworkPolicy object must be a valid
[DNS subdomain name ](/docs/concepts/overview/working-with-objects/names#dns-subdomain-names ).
2019-10-09 20:25:49 +00:00
2020-03-05 19:28:38 +00:00
{{< note > }}
2020-02-12 19:06:51 +00:00
NetworkPolicy includes a `podSelector` which selects the grouping of Pods to which the policy applies. You can see this policy selects Pods with the label `app=nginx` . The label was automatically added to the Pod in the `nginx` Deployment. An empty `podSelector` selects all pods in the namespace.
2019-10-09 20:25:49 +00:00
{{< / note > }}
2017-05-14 22:10:50 +00:00
## Assign the policy to the service
2020-02-12 19:06:51 +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
2020-02-12 19:06:51 +00:00
kubectl apply -f https://k8s.io/examples/service/networking/nginx-policy.yaml
2018-12-23 01:20:33 +00:00
```
```none
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
2020-02-12 19:06:51 +00:00
When you attempt to access the `nginx` Service from a Pod without the correct labels, the request times out:
2017-04-10 17:26:28 +00:00
```console
2019-10-09 20:25:49 +00:00
kubectl run --generator=run-pod/v1 busybox --rm -ti --image=busybox -- /bin/sh
2018-12-23 01:20:33 +00:00
```
2020-02-12 19:06:51 +00:00
In your shell, run the command:
2017-04-10 17:26:28 +00:00
2020-02-12 19:06:51 +00:00
```shell
wget --spider --timeout=1 nginx
```
2017-04-10 17:26:28 +00:00
2020-02-12 19:06:51 +00:00
```none
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
2020-02-12 19:06:51 +00:00
You can create a Pod with the correct labels to see that the request is allowed:
2017-04-10 17:26:28 +00:00
```console
2019-10-09 20:25:49 +00:00
kubectl run --generator=run-pod/v1 busybox --rm -ti --labels="access=true" --image=busybox -- /bin/sh
2018-12-23 01:20:33 +00:00
```
2020-02-12 19:06:51 +00:00
In your shell, run the command:
2017-04-10 17:26:28 +00:00
2020-02-12 19:06:51 +00:00
```shell
wget --spider --timeout=1 nginx
```
2017-04-10 17:26:28 +00:00
2020-02-12 19:06:51 +00:00
```none
2017-04-10 17:26:28 +00:00
Connecting to nginx (10.100.0.16:80)
2020-02-12 19:06:51 +00:00
remote file exists
2017-04-10 17:26:28 +00:00
```
2017-05-14 22:10:50 +00:00
2020-02-12 19:06:51 +00:00
{{% /capture %}}