Browse topics

Network policies

Version
1.37
Since
1.8
Review
Draft

Summary#

A NetworkPolicy selects Pods in its namespace and limits which connections they can accept or initiate. Ingress and egress isolation are separate. Matching policies add their permitted traffic together. For a Pod-to-Pod connection, both the source's egress and the destination's ingress must allow it.

Syntax#

sh
kubectl get networkpolicy -n NAMESPACE
kubectl describe networkpolicy NAME -n NAMESPACE

Set podSelector to select the Pods being protected. With policyTypes: [Ingress] and no ingress rules, the policy grants no incoming connections to those Pods.

Example#

Run this on a test cluster whose network plugin enforces NetworkPolicy. Two clients initially reach a local web server. The policy then restricts TCP port 8080 to the client labeled access=trusted.

sh
(
  set -eu
  ns="ref-netpol-$$"
  kubectl create namespace "$ns" >/dev/null
  trap 'kubectl delete namespace "$ns" >/dev/null' EXIT
  kubectl wait -n "$ns" --for=create serviceaccount/default --timeout=60s >/dev/null

  kubectl run -n "$ns" web --image=busybox:1.37 --labels=app=web --port=8080 --expose \
    --overrides='{"spec":{"terminationGracePeriodSeconds":1}}' --command -- \
    sh -ec "mkdir /www; echo 'web reached' > /www/index.html; exec httpd -f -p 8080 -h /www" >/dev/null
  for client in trusted visitor; do
    kubectl run -n "$ns" "$client" --image=busybox:1.37 --labels="access=$client" \
      --overrides='{"spec":{"terminationGracePeriodSeconds":1}}' --command -- sleep 3600 >/dev/null
  done
  kubectl wait -n "$ns" --for=condition=Ready pod --all --timeout=120s >/dev/null

  fetch() {
    kubectl exec -n "$ns" "$1" -- wget -T 2 -qO- http://web:8080/
  }
  for client in trusted visitor; do
    tries=0
    until fetch "$client" >/dev/null 2>&1; do
      tries=$((tries + 1)); [ "$tries" -lt 15 ]; sleep 1
    done
  done

  kubectl apply -n "$ns" -f - <<'YAML'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: web-ingress
spec:
  podSelector:
    matchLabels:
      app: web
  policyTypes: [Ingress]
  ingress:
    - from:
        - podSelector:
            matchLabels:
              access: trusted
      ports:
        - protocol: TCP
          port: 8080
YAML
  tries=0
  while fetch visitor >/dev/null 2>&1; do
    tries=$((tries + 1)); [ "$tries" -lt 15 ]; sleep 1
  done
  fetch trusted
  echo "visitor blocked"
)

Expected output#

Text
networkpolicy.networking.k8s.io/web-ingress created
web reached
visitor blocked

Common mistakes#

Creating the object succeeds even if the network plugin ignores it. Test traffic from both an allowed and a blocked Pod. To require both a namespace label and a Pod label for a peer, put namespaceSelector and podSelector in the same list item. Separate items allow either match.

Version applicability#

The networking.k8s.io/v1 API is available since Kubernetes 1.8. The extensions/v1beta1 API stopped being served in 1.16. The example targets 1.37.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0