Kubernetes Ingress vs Egress: A Practical NetworkPolicy Guide
Kubernetes Ingress vs Egress: A Practical NetworkPolicy Guide You deploy an application to Kubernetes, configure HTTPS and create a route through your Gateway. External users can reach the application. Everything loo
Kubernetes Ingress vs Egress: A Practical NetworkPolicy Guide
You deploy an application to Kubernetes, configure HTTPS and create a route through your Gateway.
External users can reach the application.
Everything looks good.
But what prevents that application from connecting directly to an unrelated database or external server?
That's a different networking problem, and it requires a different control.
Understanding traffic direction
Ingress is traffic entering a workload.
Egress is traffic initiated by that workload toward another destination.
Consider:
Frontend → API → Database
The frontend's request to the API is egress from the frontend and ingress to the API.
When policies isolate both workloads in the relevant directions, both sides must permit the connection.
Response traffic for permitted connections is implicitly allowed.
Ingress resources versus NetworkPolicy
The Kubernetes Ingress resource and Gateway API provide mechanisms for routing traffic toward Services.
NetworkPolicy defines which network connections selected Pods are permitted to establish or receive.
These mechanisms address different responsibilities.
| Mechanism | Responsibility |
|---|---|
| Ingress | HTTP and HTTPS routing |
| Gateway API | Role-oriented traffic routing |
| NetworkPolicy | Workload network access |
| CNI-specific policy | Additional network security capabilities |
A working Gateway configuration does not automatically restrict outbound Pod connections.
Start with the actual application dependencies
Suppose a cart service requires access to a PostgreSQL database.
Both workloads run in the shop namespace.
The cart Pods have the label app=cart.
The database Pods have the label app=payments-db.
Before applying restrictive policies, inventory every required connection, including DNS and monitoring dependencies.
Allow DNS
Many Kubernetes clusters use CoreDNS Pods carrying the k8s-app=kube-dns label.
Here is an illustrative DNS allowance:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns-egress
namespace: shop
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
podSelector:
matchLabels:
k8s-app: kube-dns
ports:
- protocol: UDP
port: 53
- protocol: TCP
port: 53
Verify the actual DNS configuration in your cluster.
NodeLocal DNSCache and other DNS architectures may require different allowances.
An important detail: this policy itself isolates selected Pods for egress. Applying it alone can block other application connections.
Prepare the complete dependency policy set before enabling enforcement in production.
Allow the cart service to reach PostgreSQL
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: cart-egress
namespace: shop
spec:
podSelector:
matchLabels:
app: cart
policyTypes:
- Egress
egress:
- to:
- podSelector:
matchLabels:
app: payments-db
ports:
- protocol: TCP
port: 5432
This policy permits matching cart Pods to establish PostgreSQL connections toward matching database Pods in the same namespace.
Protect the database's ingress
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: payments-db-ingress
namespace: shop
spec:
podSelector:
matchLabels:
app: payments-db
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: cart
ports:
- protocol: TCP
port: 5432
The database now has an explicit inbound access policy.
Other applicable policies may grant additional access because standard NetworkPolicies combine additively.
These examples assume a compatible CNI and matching workload labels.
Understand default deny
A namespace-wide egress isolation policy looks like this:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-egress
namespace: shop
spec:
podSelector: {}
policyTypes:
- Egress
It selects every Pod in the namespace without introducing any permitted destinations.
Existing allow policies still contribute their permitted connections.
Test the entire configuration in staging before applying it to production workloads.
External APIs and domain names
Standard Kubernetes NetworkPolicy supports IP ranges, ports and workload selectors.
It does not provide native domain-name matching.
This becomes inconvenient when an external payment provider frequently changes IP addresses.
Cilium provides FQDN-based policies that can authorize connections using addresses learned from DNS responses.
These require appropriate DNS proxy configuration.
For environments that need centralized outbound monitoring or fixed public IP addresses, an egress gateway may be useful.
The architecture must also prevent workloads from bypassing that gateway.
Verify the actual behavior
Read the installed policies:
kubectl get networkpolicy -n shop
kubectl describe networkpolicy cart-egress -n shop
kubectl get pods -n shop --show-labels
From an authorized test workload with representative policy labels and networking configuration, check:
nslookup payments-db.shop.svc.cluster.local
nc -vz -w 5 payments-db 5432
curl --max-time 5 https://example.com
The first two operations should succeed if DNS and PostgreSQL access are permitted.
The final operation should fail if the external destination is intentionally excluded.
Replace names, cluster DNS suffixes and destinations with those appropriate to your test environment.
A failed connection alone does not prove NetworkPolicy enforcement. Establish a working baseline and review CNI observations where available.
Final thoughts
Kubernetes networking becomes easier to reason about when routing and authorization are treated separately.
Design the routes applications need, declare their network dependencies and verify that unnecessary connections are actually blocked.
For the complete Gateway API, Cilium FQDN, egress gateway and production troubleshooting walkthrough, read:
Kubernetes Ingress vs Egress: How to Lock Both Cluster Doors
Official reference:
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.