Kubernetes Lab 10 - Network Policies
Solution
Step 1: Create the NetworkPolicy
Save the following to network-policy.yaml:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: my-network-policy
spec:
podSelector:
matchLabels:
app: secure-app
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
allow-access: "true"
Apply the policy:
Understanding the Policy
podSelector.matchLabels.app: secure-app- This policy applies to pods with the labelapp: secure-apppolicyTypes: [Ingress]- This policy controls incoming trafficingress.from.podSelector.matchLabels.allow-access: "true"- Only allow traffic from pods with this label
Step 2: Test that access is blocked
Get the secure pod IP:
Try to access from the client pod (should fail/timeout):
Step 3: Add the required label
Step 4: Test that access is now allowed
You should now see the nginx welcome page HTML.