This lesson demonstrates how to control external egress traffic in Istio using ServiceEntry resources and (optionally) an Istio egress Gateway. You’ll learn how to:Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
- Install Istio with a demo profile configured to
REGISTRY_ONLYoutbound policy so only hosts registered in Istio are reachable. - Run a test pod and observe how an Envoy sidecar enforces egress restrictions.
- Create a ServiceEntry to allow
www.wikipedia.org. - Configure an egress Gateway, DestinationRule, and VirtualService to force traffic through the egress gateway and understand why the
meshgateway entry is required.
Label any namespaces that should be managed by Istio (for example:
kubectl label namespace default istio-injection=enabled) and recreate pods so the Envoy sidecar can be injected.Prerequisites
- A Kubernetes cluster (minikube, Kind, or cloud).
- curl and kubectl configured to talk to the cluster.
- istioctl (we use Istio 1.18.2 in examples below).
1. Install istioctl and prepare a demo profile
Download Istio and addistioctl to your PATH:
demo profile into a YAML file for editing:
meshConfig section and set the outbound traffic policy to REGISTRY_ONLY so the mesh only allows egress to hosts known to Istio (via ServiceEntry):
2. Create a test pod and observe default egress behavior
Run a simple test pod with curl installed:3. Enable sidecar injection and observe REGISTRY_ONLY effect
Label the namespace for automatic sidecar injection (using thedefault namespace in this guide), restart the pod, and confirm the sidecar is injected:
meshConfig.outboundTrafficPolicy.mode is REGISTRY_ONLY and www.wikipedia.org is not yet registered in Istio, Envoy will block the request and return:
REGISTRY_ONLY prevents egress unless a ServiceEntry (or other Istio configuration) explicitly allows the external host.
To permit pods in an Istio-enabled namespace to reach external services, add ServiceEntry resources (or configure egress gateways) for those hosts.
4. Create a ServiceEntry for Wikipedia
Registerwww.wikipedia.org with Istio by creating a ServiceEntry in the default namespace. Save this as serviceentry-wikipedia.yaml:
5. Route external traffic through an Istio egress gateway
Routing external traffic through an egress gateway centralizes outbound traffic, enabling monitoring, logging, and centralized policy enforcement. To route traffic through the egress gateway we need:- A Gateway resource that selects the egress gateway pods.
- A DestinationRule pointing to the egress gateway service (and a subset).
- A VirtualService that (a) routes mesh-originating traffic to the egress gateway and (b) routes traffic from the egress gateway to the external host.
istio-system, so create the Gateway in istio-system to match pods by label.
First confirm the egress gateway pod and labels:
gateway.yaml):
dr.yaml):
vs.yaml). This resource has two important HTTP blocks:
- A
meshgateway match for port 80: routes traffic originating inside the mesh to the egress gateway subsetwikipedia. - An
istio-system/istio-egressgatewaygateway match: routes traffic arriving at the egress gateway to the external hostwww.wikipedia.org.
istio-system, reference it in the VirtualService as istio-system/istio-egressgateway.
mesh gateway entry is required so that traffic originating from inside the mesh (pod → external host) matches the first HTTP rule and is routed to the egress gateway subset wikipedia. When the egress gateway receives the request, the VirtualService matches the istio-system/istio-egressgateway gateway rule and forwards the request to www.wikipedia.org. If you omit the mesh match, internal traffic will bypass the egress gateway and will not be centrally controlled.
If you omit the
mesh gateway entry in the VirtualService, traffic from inside the mesh will bypass the egress gateway, removing centralized control and visibility.6. Observe traffic through the egress gateway
Tail logs for the egress gateway pod in another terminal:Quick reference: resources for egress control
| Resource Type | Purpose | Example / Notes |
|---|---|---|
| ServiceEntry | Register external host(s) with Istio to allow egress under REGISTRY_ONLY | See serviceentry-wikipedia.yaml above |
| Gateway | Selects egress gateway pods (namespace-specific) and exposes ports/hosts | Create in istio-system to select istio=egressgateway pods |
| DestinationRule | Points to the egress gateway service and defines subsets | Host should be istio-egressgateway.istio-system.svc.cluster.local |
| VirtualService | Routes mesh-originating traffic to egress gateway and gateway→external host | Include both mesh and istio-system/istio-egressgateway in gateways |
Additional ServiceEntry examples
Simple external HTTP ServiceEntry:Troubleshooting tips
- If your VirtualService isn’t taking effect, confirm the Gateway exists in the correct namespace and the
gatewaysentry in the VirtualService is properly namespace-qualified (istio-system/istio-egressgateway). - If traffic is still blocked after creating a ServiceEntry, ensure the ServiceEntry is in the same namespace as the client pod (or exported appropriately) and that DNS resolution is working as expected.
- Use
kubectl get virtualservices,destinationrules,gateways,serviceentries -Ato verify configuration across namespaces.
Summary
- Setting
meshConfig.outboundTrafficPolicy.modetoREGISTRY_ONLYblocks egress to hosts not registered in Istio. - Use ServiceEntry to register external hosts so Istio allows egress.
- To centralize and control external traffic, create an egress Gateway, a DestinationRule pointing to the egress gateway service, and a VirtualService that routes mesh-originating traffic to the egress gateway and the gateway to the external host.
- The
meshgateway entry in the VirtualService is essential to route traffic originating from inside the mesh through the egress gateway.