Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 37 ยท Security and Operators

Network Policies and Project Isolation in OpenShift

Control pod-to-pod traffic with NetworkPolicy in OpenShift: default deny, allow from ingress and monitoring, namespace selectors, egress rules and default project templates.

By default, any pod in a Kubernetes cluster can talk to any other pod. OpenShift's default network plugin OVN-Kubernetes does the same unless you add rules. A NetworkPolicy is the object that limits it: it selects pods and lists which traffic may reach them (ingress) or leave them (egress). Policies are the firewall between your frontend, API and database, and between teams sharing a cluster.

The YAML and commands are read-only examples. The runnable Python stand-ins evaluate policies against sample traffic so you can build intuition for how selectors combine.

How policies work

  • A policy has a podSelector: the pods it protects. An empty selector {} means all pods in the project.
  • Once any policy selects a pod for a direction (ingress or egress), only traffic allowed by some policy is permitted in that direction for that pod.
  • Policies are additive: the allowed set is the union of all policies that select the pod.
  • They are namespaced and only restrict pods, not the node or the router's host network unless allowed explicitly.

Starting point: deny everything, then allow

YAML
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: deny-all
  namespace: shop
spec:
  podSelector: {}
  policyTypes: ["Ingress"]

That one object blocks all incoming traffic to every pod in shop. Now open the paths you need:

YAML
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: api-from-web
  namespace: shop
spec:
  podSelector:
    matchLabels: { app: shop-api }
  policyTypes: ["Ingress"]
  ingress:
    - from:
        - podSelector:
            matchLabels: { app: shop-web }
      ports:
        - protocol: TCP
          port: 8080

Only pods labeled app: shop-web in the same project may reach shop-api on 8080.

Letting the router and monitoring in

If you deny all ingress, your route stops working because the router pods live in another namespace. OpenShift labels its system namespaces, so allow them by label:

YAML
- from:
    - namespaceSelector:
        matchLabels:
          network.openshift.io/policy-group: ingress

For monitoring scrapes use network.openshift.io/policy-group: monitoring. Newer versions also support host-network traffic through policy-group: ingress where the router runs on the host network. OpenShift ships ready-made policies in its default project template documentation: allow-from-openshift-ingress, allow-from-openshift-monitoring and allow-same-namespace.

Terminal
oc get networkpolicy -n shop
oc describe networkpolicy api-from-web -n shop

Selectors: a common trap

Inside one from list, separate items are OR, while selectors inside one item are AND:

YAML
# OR: pods labeled app=web in this project, OR any pod in namespaces labeled team=ops
- from:
    - podSelector: { matchLabels: { app: web } }
    - namespaceSelector: { matchLabels: { team: ops } }

# AND: pods labeled app=web that are ALSO in namespaces labeled team=ops
- from:
    - podSelector: { matchLabels: { app: web } }
      namespaceSelector: { matchLabels: { team: ops } }

One missing dash changes who gets in, so test every policy.

Python Stand-in: NetworkPolicy evaluator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Egress control

Restrict outbound traffic with policyTypes: ["Egress"]. Remember to allow DNS or nothing resolves:

YAML
spec:
  podSelector: {}
  policyTypes: ["Egress"]
  egress:
    - to:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: openshift-dns
      ports:
        - { protocol: UDP, port: 5353 }
        - { protocol: TCP, port: 5353 }

For limiting traffic leaving the cluster by hostname or IP, OpenShift also has the dedicated EgressFirewall and EgressIP features, because NetworkPolicy has no DNS-name rules.

Policies by default in new projects

Cluster admins can edit the project request template so every new project starts with deny-all plus the router and monitoring allowances. This turns secure-by-default into policy rather than a habit.

Terminal
oc adm create-bootstrap-project-template -o yaml > template.yaml
# add NetworkPolicy objects, then:
oc create -f template.yaml -n openshift-config
oc edit projects.config.openshift.io cluster   # set spec.projectRequestTemplate.name

Testing a policy

Terminal
oc rsh deploy/shop-web
curl -s --max-time 3 http://shop-api:8080/healthz    # should work
curl -s --max-time 3 http://shop-db:5432             # should hang or fail

A timeout (not "connection refused") is the signature of a blocked connection.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. After a deny-all ingress policy is applied, what happens to your route?

    Router pods are in another namespace, so you must allow them, for example with the ingress policy-group label.
  2. 2. How are two from entries in separate list items combined?

    Separate items are OR; selectors within one item are AND.
  3. 3. Which is the usual symptom of a blocked connection?

    Dropped packets cause timeouts, while refusal means a reachable host with nothing listening.
  4. 4. What should you add when you restrict egress for all pods?

    Without a DNS exception, name lookups fail and applications seem broken.

Summary

  • Without policies all pods can talk to each other; policies add restriction.
  • Start with deny-all, then allow web to API to database explicitly.
  • Allow the router and monitoring namespaces by their policy-group labels.
  • Mind the OR/AND difference in selectors; test with curl from a pod.
  • Bake default policies into the project template.

Next: Operators and OLM