📖 Scenario: You are managing a Kubernetes cluster for a small web application. You need to control which external IP addresses can access your application pods and which pods can communicate outside the cluster.
🎯 Goal: Learn how to create Kubernetes NetworkPolicy objects to define ingress and egress rules that allow or block traffic to and from pods.
📋 What You'll Learn
Create a NetworkPolicy YAML manifest with specific ingress and egress rules
Use pod selectors and IP blocks to control traffic
Apply the NetworkPolicy to a namespace
Verify the NetworkPolicy effects by describing it
💡 Why This Matters
🌍 Real World
NetworkPolicies are used in Kubernetes to secure pod communication by controlling which IPs or pods can talk to each other. This is like setting house rules for who can enter or leave your home.
💼 Career
Understanding and managing ingress and egress rules is essential for Kubernetes administrators and DevOps engineers to protect applications and comply with security policies.
Progress0 / 4 steps
1
Create a basic NetworkPolicy skeleton
Create a YAML file named networkpolicy.yaml with a NetworkPolicy resource in the default namespace. Set the metadata.name to allow-web-ingress. Include empty spec with podSelector selecting pods with label app: web.
Kubernetes
Hint
Start by defining the kind, metadata, and podSelector with the correct labels.
2
Add ingress rules to allow traffic from a specific IP block
In the spec section of networkpolicy.yaml, add an ingress rule that allows traffic only from the IP block 192.168.1.0/24. Use from with ipBlock specifying this CIDR.
Kubernetes
Hint
Use ingress with a list containing from and ipBlock with the CIDR.
3
Add egress rules to allow pods to access external internet
In the spec section, add an egress rule that allows all traffic to 0.0.0.0/0. Use to with ipBlock specifying this CIDR. Also, set policyTypes to include both Ingress and Egress.
Kubernetes
Hint
Remember to add egress with to and ipBlock for all IPs. Also add policyTypes with both Ingress and Egress.
4
Apply and verify the NetworkPolicy
Run the command kubectl apply -f networkpolicy.yaml to create the NetworkPolicy. Then run kubectl describe networkpolicy allow-web-ingress -n default to see the applied rules. Copy the output of the describe command exactly as it appears.
Kubernetes
Hint
Run the commands exactly and copy the describe output. It shows the NetworkPolicy details.
Practice
(1/5)
1. What do Ingress rules control in Kubernetes NetworkPolicies?
easy
A. CPU resource limits for pods
B. Incoming traffic to pods
C. Storage access for pods
D. Outgoing traffic from pods
Solution
Step 1: Understand the role of Ingress rules
Ingress rules specify which incoming network traffic is allowed to reach the pods.
Step 2: Differentiate from Egress rules
Egress rules control outgoing traffic, not incoming.
Final Answer:
Incoming traffic to pods -> Option B
Quick Check:
Ingress = Incoming traffic [OK]
Hint: Ingress means incoming traffic control [OK]
Common Mistakes:
Confusing ingress with egress
Thinking ingress controls resource limits
Mixing network traffic with storage access
2. Which of the following is the correct syntax to allow all incoming traffic from pods in namespace frontend using a NetworkPolicy ingress rule?
easy
A. ingress:
- from:
- namespaceSelector:
matchLabels:
name: frontend
B. egress:
- to:
- namespaceSelector:
matchLabels:
name: frontend
C. ingress:
- to:
- namespaceSelector:
matchLabels:
name: frontend
D. egress:
- from:
- namespaceSelector:
matchLabels:
name: frontend
Solution
Step 1: Identify ingress rule syntax
Ingress rules use ingress: with from: to specify allowed sources.
Step 2: Confirm namespaceSelector usage
Using namespaceSelector with matchLabels is correct to select pods in a namespace.
Ingress uses 'from' to allow incoming traffic [OK]
Hint: 'ingress' with 'from' allows incoming traffic sources [OK]
Common Mistakes:
Using 'to' instead of 'from' in ingress
Confusing ingress with egress syntax
Misplacing namespaceSelector under egress
3. Given this NetworkPolicy snippet, what traffic is allowed?
egress:
- to:
- ipBlock:
cidr: 10.0.0.0/24
except:
- 10.0.0.5/32
medium
A. All outgoing traffic to 10.0.0.0/24 except 10.0.0.5
B. All incoming traffic from 10.0.0.0/24 except 10.0.0.5
C. All outgoing traffic to 10.0.0.5 only
D. All incoming traffic from 10.0.0.5 only
Solution
Step 1: Analyze egress rule with ipBlock
The rule allows outgoing traffic to IPs in 10.0.0.0/24 except 10.0.0.5.
Step 2: Confirm direction and exceptions
Egress means outgoing traffic; 'except' excludes 10.0.0.5 from allowed IPs.
Final Answer:
All outgoing traffic to 10.0.0.0/24 except 10.0.0.5 -> Option A
Quick Check:
Egress with ipBlock except excludes IPs [OK]
Hint: Egress 'to' with ipBlock allows excepted IPs blocked [OK]
Common Mistakes:
Confusing ingress with egress direction
Misreading 'except' as allowed IP
Mixing incoming and outgoing traffic
4. You wrote this NetworkPolicy egress rule but pods still cannot reach external IPs:
egress:
- from:
- ipBlock:
cidr: 0.0.0.0/0
What is the error?
medium
A. NetworkPolicy must not have egress rules
B. CIDR 0.0.0.0/0 is invalid
C. Missing 'ports' section
D. Egress rules use 'to', not 'from'
Solution
Step 1: Check egress rule syntax
Egress rules specify destinations with 'to', not 'from'. Using 'from' is invalid here.
Step 2: Understand correct usage
Correct egress syntax uses 'to' to define allowed outgoing IP blocks.
Final Answer:
Egress rules use 'to', not 'from' -> Option D
Quick Check:
Egress uses 'to' for destinations [OK]
Hint: Egress rules always use 'to' keyword [OK]
Common Mistakes:
Using 'from' in egress rules
Assuming CIDR 0.0.0.0/0 is invalid
Thinking ports are always required
5. You want to restrict pods to only receive traffic from pods in the same namespace and send traffic only to a specific external IP range 192.168.1.0/24. Which NetworkPolicy configuration achieves this?