Overview
Network Policies in Kubernetes are like firewalls for your pods - they control which pods can communicate with each other. kguardian observes actual traffic patterns and generates policies that allow only the connections your application actually uses.Best Practice: Let your application run under normal load for at least 5-15 minutes before generating policies. This ensures you capture all typical communication patterns.
Basic Usage
Generate for a Single Pod
The simplest use case - generate a policy for one specific pod:1
kguardian discovers the pod
The CLI queries the broker for all traffic involving
my-app in the production namespace2
Analyzes traffic patterns
- Identifies all unique source IPs (for ingress rules)
- Identifies all destination IPs (for egress rules)
- Resolves IPs to pods/services
- Groups by port and protocol
3
Generates YAML
Creates
production-my-app-networkpolicy.yaml in the ./policies directoryBatch Generation
All Pods in a Namespace
Generate policies for every pod in a namespace:All Pods Across All Namespaces
For cluster-wide policy generation:This can generate hundreds of files in large clusters. Use with caution and review before applying.
Cilium Network Policies
Cilium is an advanced CNI that provides enhanced network policies with L7 (HTTP, gRPC, Kafka) visibility and identity-based rules.Generate Cilium Policies
Cilium vs Kubernetes Policies
| Feature | Kubernetes | Cilium |
|---|---|---|
| Namespace selectors | ✅ | ✅ |
| Pod selectors | ✅ | ✅ |
| Port/protocol rules | ✅ | ✅ |
| CIDR blocks | ✅ | ✅ |
| L7 rules (HTTP, gRPC) | ❌ | ✅ |
| DNS-based rules | ❌ | ✅ |
| Identity-based | ❌ | ✅ |
| Cluster-wide policies | ❌ | ✅ |
If you’re running Cilium CNI, use
--type cilium for better security and features. Otherwise, stick with --type kubernetes (the default).Dry Run vs Apply
Dry Run (Default)
By default,gen networkpolicy runs in dry-run mode - it saves policies to files without applying them to the cluster:
Direct Apply
To generate and immediately apply policies:Use with caution! Applying network policies can break communication if the observed traffic was incomplete. Always test in non-production first.
Recommended Workflow
- Generate policies in dry-run mode
- Review the generated YAML files
- Test in a staging environment
- Apply manually after validation:
Advanced Options
Custom Output Directory
Save policies to a specific location:Print to Stdout Only
Skip file creation and just print the policy:Understanding Generated Policies
Policy Structure
Every generated policy includes:- podSelector: Identifies which pods this policy applies to
- policyTypes: Declares whether it has Ingress, Egress, or both
- ingress: List of allowed incoming connections
- egress: List of allowed outgoing connections
How Peers are Identified
kguardian resolves IPs to Kubernetes resources:Pod-to-Pod Traffic
Pod-to-Pod Traffic
When traffic is between pods, kguardian generates:This allows traffic from any pod with
app=my-service in the production namespace.Service Traffic
Service Traffic
For traffic to a Kubernetes Service, kguardian uses the service’s selector:
External Traffic
External Traffic
For traffic to external IPs (e.g., public APIs), kguardian generates CIDR blocks:
DNS Traffic
DNS Traffic
kguardian automatically detects DNS queries:
Common Scenarios
Scenario 1: Microservices Application
You have a 3-tier app:frontend → backend → database.
frontendpolicy allows ingress from ingress controller, egress tobackendbackendpolicy allows ingress fromfrontend, egress todatabasedatabasepolicy allows ingress frombackendonly
Scenario 2: Multi-Namespace Communication
Your app instaging talks to a shared database in data:
Scenario 3: Default Deny + Allowlist
Start with default-deny, then allowlist only observed traffic:Now your namespace has zero-trust networking - only explicitly observed communication is allowed.
Troubleshooting
No Traffic Data
Symptom: CLI says “No traffic data found for pod” Solutions:- Ensure the pod has been running for at least 5 minutes
- Generate some traffic (hit HTTP endpoints, trigger background jobs, etc.)
- Check broker has data:
Generated Policy Too Permissive
Symptom: Policy allows more than expected (e.g., allows entire namespace) Cause: Pods may lack specific labels, so kguardian falls back to broader selectors. Solution:- Add specific labels to your pods
- Manually edit generated policies to tighten selectors
- Re-generate after labeling
Policy Breaks Communication
Symptom: After applying policy, some connections fail Cause: Incomplete observation period - not all traffic was captured Solution:- Check pod logs for connection errors
- Identify missing ingress/egress rules
- Manually add missing rules or extend observation period