Kubernetes Lab 10 - Network Policies
Problem
Network policies allow you to control traffic flow between pods. In this lab, you will create a network policy that restricts access to a secure pod, allowing only pods with a specific label to connect.
Prerequisites
Network policies require a CNI plugin that supports them (such as Calico, Cilium, or Weave Net). This lab uses Calico.
Setup for Minikube
If using minikube, start it with CNI support and install Calico:
minikube start --network-plugin=cni
kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/calico.yaml
kubectl -n kube-system get pods | grep calico-node
Wait for all calico-node pods to be Running before proceeding.
Setup for OpenShift
OpenShift includes network policy support by default. No additional setup is required.
Setup
Step 1: Create the secured pod
Save the following to secure-pod.yaml and apply it:
apiVersion: v1
kind: Pod
metadata:
name: network-policy-secure-pod
labels:
app: secure-app
spec:
containers:
- name: nginx
image: bitnami/nginx:1.25
ports:
- containerPort: 8080
Step 2: Create the client pod (without the required label)
Save the following to client-pod.yaml and apply it:
apiVersion: v1
kind: Pod
metadata:
name: network-policy-client-pod
spec:
containers:
- name: busybox
image: curlimages/curl:8.5.0
command: ["/bin/sh", "-c", "while true; do sleep 3600; done"]
Step 3: Get the secure pod IP address
Save this IP for testing connectivity.
Step 4: Test connectivity before network policy
Before applying any network policy, verify the client can reach the secure pod:
You should see the nginx welcome page HTML.
Tasks
- Create a NetworkPolicy that:
- Applies to pods with label
app: secure-app - Only allows ingress traffic from pods with label
allow-access: "true" -
Denies all other ingress traffic
-
Test that the policy works by verifying:
- The client pod (without the label) cannot access the secure pod
- After adding the label, the client pod can access the secure pod
Hints
- NetworkPolicy uses
podSelectorto select which pods the policy applies to - Use
ingress.from.podSelectorto specify which pods can send traffic - The
policyTypesfield should includeIngress
Verification
Test 1: Verify access is denied without the label
After applying the network policy, the client pod should NOT be able to reach the secure pod:
This should timeout or fail.
Test 2: Add the required label to the client pod
Test 3: Verify access is now allowed
You should now see the nginx welcome page.