website/docs/user-guide/networkpolicies.md

99 lines
3.7 KiB
Markdown
Raw Normal View History

---
2016-07-29 17:36:25 +00:00
assignees:
- thockin
- caseydavenport
2016-12-15 20:16:54 +00:00
title: Network Policies
---
* TOC
{:toc}
2016-06-27 22:58:08 +00:00
A network policy is a specification of how selections of pods are allowed to communicate with each other and other network endpoints.
2016-06-27 22:58:08 +00:00
`NetworkPolicy` resources use labels to select pods and define whitelist rules which allow traffic to the selected pods in addition to what is allowed by the isolation policy for a given namespace.
## Prerequisites
You must enable the `extensions/v1beta1/networkpolicies` runtime config in your apiserver to enable this resource.
2016-06-27 22:58:08 +00:00
You must also be using a networking solution which supports `NetworkPolicy` - simply creating the
resource without a controller to implement it will have no effect.
## Configuring Namespace Isolation Policy
2016-06-27 22:58:08 +00:00
Isolation can be configured on a per-namespace basis. Once isolation is configured on a namespace it will be applied to all pods in that namespace. Currently, only isolation policy on inbound traffic (ingress) can be defined.
The following ingress isolation types being supported:
- `DefaultDeny`: Pods in the namespace will be inaccessible from any source except the pod's local node.
Ingress isolation can be enabled using an annotation on the Namespace.
```yaml
kind: Namespace
apiVersion: v1
metadata:
annotations:
net.beta.kubernetes.io/network-policy: |
{
"ingress": {
"isolation": "DefaultDeny"
}
}
```
To configure the annotation via `kubectl`:
```shell
{% raw %}
kubectl annotate ns <namespace> "net.beta.kubernetes.io/network-policy={\"ingress\": {\"isolation\": \"DefaultDeny\"}}"
{% endraw %}
```
2016-06-27 22:58:08 +00:00
2016-09-13 01:32:35 +00:00
See the [NetworkPolicy getting started guide](/docs/getting-started-guides/network-policy/walkthrough) for an example.
2016-06-27 22:58:08 +00:00
## The `NetworkPolicy` Resource
2016-06-28 20:23:30 +00:00
See the [api-reference](/docs/api-reference/extensions/v1beta1/definitions/#_v1beta1_networkpolicy) for a full definition of the resource.
A minimal `NetworkPolicy` might look like this:
```yaml
2016-06-27 22:58:08 +00:00
apiVersion: extensions/v1beta1
kind: NetworkPolicy
metadata:
name: test-network-policy
2016-09-22 17:11:18 +00:00
namespace: default
2016-06-27 22:58:08 +00:00
spec:
podSelector:
matchLabels:
role: db
ingress:
- from:
- namespaceSelector:
2016-09-22 17:18:18 +00:00
matchLabels:
project: myproject
- podSelector:
matchLabels:
role: frontend
2016-06-27 22:58:08 +00:00
ports:
- protocol: tcp
port: 6379
```
*POSTing this to the API server will have no effect unless your chosen networking solution supports network policy.*
2016-06-27 22:58:08 +00:00
__Mandatory Fields__: As with all other Kubernetes config, a `NetworkPolicy` needs `apiVersion`, `kind`, and `metadata` fields. For general information about working with config files, see [here](/docs/user-guide/simple-yaml), [here](/docs/user-guide/configuring-containers), and [here](/docs/user-guide/working-with-resources).
2016-06-27 22:58:08 +00:00
__spec__: `NetworkPolicy` [spec](https://github.com/kubernetes/kubernetes/tree/{{page.githubbranch}}/docs/devel/api-conventions.md#spec-and-status) has all the information needed to define a network isolation policy in the deployed controller.
__podSelector__: Each `NetworkPolicy` includes a `podSelector` which selects the grouping of pods to which the `ingress` rules in the policy apply.
__ingress__: Each `NetworkPolicy` includes a list of whitelist `ingress` rules. Each rule allows traffic which matches both the `from` and `ports` sections.
2016-09-22 16:52:39 +00:00
2016-10-19 12:55:03 +00:00
This example NetworkPolicy has the following characteristics:
2016-09-22 16:52:39 +00:00
2016-09-22 16:55:09 +00:00
1. applies to all pods in the default namespace with the label "role=db"
2016-09-22 16:52:39 +00:00
2. allows tcp/6379 ingress traffic to the "role=db" pods from any pod in the current namespace with the label "role=frontend" (due to the podSelector list element)
3. allows tcp/6379 ingress traffic to the "role=db" pods from any pod in the namespace "myproject" (due to the namespaceSelector list element)