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-standard-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: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
toCIDR/fromCIDR entries appear only for IPs that can’t be resolved to a pod or Service. Label keys in the generated YAML carry Cilium’s k8s: source prefix.
Cilium vs Kubernetes Policies
Dry Run and Applying
Dry Run (Default)
By default,gen networkpolicy runs in dry-run mode - it saves policies to files without applying them to the cluster:
Applying directly from the CLI is not implemented. Passing
--dry-run=false currently behaves the same as dry-run — policies are only saved to files — and the CLI logs a warning to that effect. Apply the generated files with kubectl apply -f.Recommended Workflow
- Generate policies in dry-run mode
- Review the generated YAML files
- Test in a staging environment
- Apply with kubectl 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 runs default-deny — 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
Generate Seccomp Profiles
Restrict syscalls based on observed behavior
CLI Reference
Complete flag documentation
Policy Gallery
Worked examples for nginx, Postgres, Prometheus, Istio, and more