3.1 KiB
| assignees | title | redirect_from | |||||
|---|---|---|---|---|---|---|---|
|
Network Policies |
|
{% capture overview %} A network policy is a specification of how groups of pods are allowed to communicate with each other and other network endpoints.
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.
{% endcapture %}
{% capture body %}
The NetworkPolicy Resource
See the api-reference for a full definition of the resource.
An example NetworkPolicy might look like this:
apiVersion: networking/v1
kind: NetworkPolicy
metadata:
name: test-network-policy
namespace: default
spec:
podSelector:
matchLabels:
role: db
ingress:
- from:
- namespaceSelector:
matchLabels:
project: myproject
- podSelector:
matchLabels:
role: frontend
ports:
- protocol: tcp
port: 6379
POSTing this to the API server will have no effect unless your chosen networking solution supports network policy.
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, here, and here.
spec: NetworkPolicy spec has all the information needed to define a particular network policy in the given namespace.
podSelector: Each NetworkPolicy includes a podSelector which selects the grouping of pods to which the ingress rules in the policy apply. The example policy selects pods with the label "role=db".
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.
So, the example NetworkPolicy:
- allows connections to tcp port 6379 of "role=db" pods in the "default" namespace from any pod in the "default" namespace with the label "role=frontend"
- allows connections to tcp port 6379 of "role=db" pods in the "default" namespace from any pod in a namespace with the label "project=myproject" {% endcapture %}
{% capture whatsnext %}
- For configuration instructions, see Configuring Namespace Isolation.
- For more NetworkPolicy examples, see the NetworkPolicy getting started guide. {% endcapture %} {% include templates/concept.md %}