About Lesson
Overview:
Network policies define how Pods communicate with each other and external systems. They are essential for segmenting traffic and restricting unwanted access.
Example Network Policy:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-specific-namespace
namespace: default
spec:
podSelector:
matchLabels:
role: frontend
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
name: trusted-namespace
Steps:
-
Apply the policy:
kubectl apply -f network-policy.yaml
-
Test communication between Pods:
-
Deploy Pods in different namespaces and validate traffic flow.
-
Activity:
Create a network policy to allow traffic only from a specific namespace to Pods labeled role: frontend
.