此内容没有您所选择的语言版本。
Chapter 3. Directing outbound traffic through a gateway
Using Istio APIs, you can configure gateway proxies that were installed using gateway injection to direct traffic that is bound for an external service.
3.1. About directing egress traffic through a gateway 复制链接链接已复制到粘贴板!
You can configure a gateway installed through gateway injection as an exit point for traffic leaving the service mesh. It acts as a forward proxy for requests sent to services external to the mesh.
- Egress gateway
An egress gateway is configured as an exit point for traffic leaving the service mesh, acting as a forward proxy for requests sent to external services. You can configure an egress gateway to fulfill security requirements:
- Traffic Restrictions: In environments with strict traffic restrictions, an egress gateway ensures all outbound traffic flows through a dedicated set of nodes.
- Network Policy Enforcement: When network policies prevent application nodes from directly accessing external services, the egress gateway handles the external access.
In these scenarios, gateway proxies are deployed on dedicated egress nodes capable of accessing external services. These nodes can then be subjected to strict network policy enforcement or additional monitoring to enhance security.
- Configure egress traffic
You can configure a gateway installed through gateway injection to direct the egress traffic by combining the following Istio resources:
-
Use the
ServiceEntryresource to define the properties of an external service. The external service is added to the Istio service registry for the mesh, which enables you to apply Istio features, such as monitoring and routing rules, to the traffic exiting the mesh that is destined for an external service. -
Use the
Gateway,VirtualService, andDestinationRuleresources to set up rules that route traffic from the mesh to the external service using the gateway proxy.
-
Use the
- Egress routing in ambient mode
If your deployment uses ambient mode, you must configure egress routing using the Kubernetes Gateway API instead of Istio
GatewayandVirtualServiceresources. The Kubernetes Gateway API provides a standardized, Kubernetes-native method for defining how traffic exits the mesh and reaches external services.You can use
GatewayandHTTPRoute(orGRPCRoute) resources to control how mesh traffic is routed to destinations outside the cluster. The Gateway API is fully supported in ambient mode and can also be used with sidecar-based deployments, providing a consistent configuration model for both ingress and egress routing.
Use Istio APIs to direct outbound HTTP traffic through a gateway that was installed using gateway injection.
Prerequisites
- You have installed a gateway using gateway injection.
Procedure
Create a namespace called
curlby running the following command:oc create namespace curl
$ oc create namespace curlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Depending on the update strategy you are using, enable sidecar injection in the namespace by running the appropriate commands:
If you are using the
InPlaceupdate strategy, run the following command:oc label namespace curl istio-injection=enabled
$ oc label namespace curl istio-injection=enabledCopy to Clipboard Copied! Toggle word wrap Toggle overflow If you are using the
RevisionBasedupdate strategy, run the following commands:Display the revision name by running the following command:
oc get istiorevisions.sailoperator.io
$ oc get istiorevisions.sailoperator.ioCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
NAME TYPE READY STATUS IN USE VERSION AGE default Local True Healthy True v1.24.3 3m33s
NAME TYPE READY STATUS IN USE VERSION AGE default Local True Healthy True v1.24.3 3m33sCopy to Clipboard Copied! Toggle word wrap Toggle overflow Label the namespace with the revision name to enable sidecar injection by running the following command:
oc label namespace curl istio.io/rev=default
$ oc label namespace curl istio.io/rev=defaultCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Deploy a
curlapplication by running the following command:oc apply -n curl -f https://raw.githubusercontent.com/openshift-service-mesh/istio/refs/heads/master/samples/curl/curl.yaml
$ oc apply -n curl -f https://raw.githubusercontent.com/openshift-service-mesh/istio/refs/heads/master/samples/curl/curl.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Export a
CURL_PODenvironment variable that has been initialized with the name of the curl pod:export CURL_POD=$(oc get pod -n curl -l app=curl -o jsonpath='{.items[0].metadata.name}')$ export CURL_POD=$(oc get pod -n curl -l app=curl -o jsonpath='{.items[0].metadata.name}')Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a YAML file named
http-se.yamlthat directs traffic from the mesh to an external service. The following example defines aServiceEntryfor a URL.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the YAML file by running the following command:
oc apply -f http-se.yaml
$ oc apply -f http-se.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Ensure the
ServiceEntryconfiguration was applied correctly. Send an HTTP request to the host that you specified in the previous step by running the following command:oc exec "$CURL_POD" -n curl -c curl -- curl -sSL -o /dev/null -D - http://docs.redhat.com
$ oc exec "$CURL_POD" -n curl -c curl -- curl -sSL -o /dev/null -D - http://docs.redhat.comCopy to Clipboard Copied! Toggle word wrap Toggle overflow This command should return HTTP status codes, such as
301(redirect) or200(success), indicating that the connection works.Create a YAML file named
http-gtw.yamlthat creates an egressGatewayand routes traffic from the mesh to the host specified for the external service.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the YAML file by running the following command:
oc apply -f http-gtw.yaml
$ oc apply -f http-gtw.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a YAML file named
http-vs.yamlthat sets up aVirtualServiceto manage the flow of traffic from the application sidecars through the egress gateway to the external host.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the YAML file by running the following command:
oc apply -f http-vs.yaml
$ oc apply -f http-vs.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Resend the HTTP request to the URL:
oc exec "$CURL_POD" -n curl -c curl -- curl -sSL -o /dev/null -D - http://docs.redhat.com
$ oc exec "$CURL_POD" -n curl -c curl -- curl -sSL -o /dev/null -D - http://docs.redhat.comCopy to Clipboard Copied! Toggle word wrap Toggle overflow The terminal should display information similar to the following output:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Ensure that the request was routed through the gateway by running the following command:
oc logs deployment/<gateway_name> -n <gateway_namespace> | tail -1
$ oc logs deployment/<gateway_name> -n <gateway_namespace> | tail -1Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteAccess logging must be enabled for this verification step to work. You can enable access logging to the standard output by setting the
spec.values.meshConfig.accessLogFilefield to/dev/stdoutin the Istio resource.The terminal should display information similar to the following output:
[2024-11-07T14:35:52.428Z] "GET / HTTP/2" 301 - via_upstream - "-" 0 0 24 24 "10.128.2.30" "curl/8.11.0" "79551af2-341b-456d-b414-9220b487a03b" "docs.redhat.com" "23.55.176.201:80" outbound|80||docs.redhat.com 10.128.2.29:49766 10.128.2.29:80 10.128.2.30:38296 -
[2024-11-07T14:35:52.428Z] "GET / HTTP/2" 301 - via_upstream - "-" 0 0 24 24 "10.128.2.30" "curl/8.11.0" "79551af2-341b-456d-b414-9220b487a03b" "docs.redhat.com" "23.55.176.201:80" outbound|80||docs.redhat.com 10.128.2.29:49766 10.128.2.29:80 10.128.2.30:38296 -Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Use the Kubernetes Gateway API to direct outbound HTTP traffic through an egress gateway.
Prerequisites
- You installed an Istio control plane.
-
You configured the
IstioandIstioCNIresources.
Procedure
Optional: Enable the Kubernetes Gateway API custom resource definitions (CRDs).
NoteAs of Kubernetes 1.28 and OpenShift Container Platform 4.18 or earlier version of Red Hat OpenShift Service Mesh, the Kubernetes Gateway API CRDs are not available by default and you must install the CRDs before you can use them. OpenShift Container Platform 4.19 and later versions install the CRDs by default.
Create a YAML file named
gateway-cr.yamlthat enables the Kubernetes Gateway API CRDs.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the YAML file by running the following command:
oc apply -f gateway-cr.yaml
$ oc apply -f gateway-cr.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Create a namespace called
egress-gatewayby running the following command:oc create namespace egress-gateway
$ oc create namespace egress-gatewayCopy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the
istio-injectionlabel to the namespace by running the following command:oc label namespace egress-gateway istio-injection=enabled
$ oc label namespace egress-gateway istio-injection=enabledCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a YAML file named
egress-gateway-cr.yamlthat defines the egress gateway.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the YAML file by running the following command:
oc apply -f egress-gateway-cr.yaml
$ oc apply -f egress-gateway-cr.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Verify the status of the gateway configuration by running the following command:
oc describe gateway -n egress-gateway
$ oc describe gateway -n egress-gatewayCopy to Clipboard Copied! Toggle word wrap Toggle overflow Desired output is indicated by
Programmedshowing in theStatuscolumn.Create a
curlpod in theegress-gatewaynamespace by running the following command:oc run test-pod --image=curlimages/curl:latest -n egress-gateway --rm -it --restart=Never -- sh
$ oc run test-pod --image=curlimages/curl:latest -n egress-gateway --rm -it --restart=Never -- shCopy to Clipboard Copied! Toggle word wrap Toggle overflow By using the
curlclient, verify that you can accesshttpbin.orgthrough the egress gateway by entering following command:curl -v http://httpbin.org/get
$ curl -v http://httpbin.org/getCopy to Clipboard Copied! Toggle word wrap Toggle overflow Desired output shows a response from
httpbin.orgthat indicates egress traffic routes through the configured gateway.
Use the Kubernetes Gateway API and waypoint proxy to direct outbound HTTP traffic through an egress gateway.
Prerequisites
- You have installed the OpenShift Service Mesh Operator version 3.2 or later.
-
You configured the
IstioandIstioCNIresources with ambient profile. -
You have created a
Ztunnelresource.
Procedure
Optional: Enable the {k8} Gateway API custom resource definitions (CRDs).
NoteAs of Kubernetes 1.28 and OpenShift Container Platform 4.18 or earlier version of Red Hat OpenShift Service Mesh, the Kubernetes Gateway API CRDs are not available by default and you must install the CRDs before you can use them. OpenShift Container Platform 4.19 and later versions install the CRDs by default.
Create a namespace called
egress-gatewayby running the following command:oc create namespace egress-gateway
$ oc create namespace egress-gatewayCopy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the ambient mode label to the namespace by running the following command:
oc label namespace egress-gateway istio.io/dataplane-mode=ambient
$ oc label namespace egress-gateway istio.io/dataplane-mode=ambientCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a YAML file named
egress-se.yamlthat defines theServiceEntry.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the YAML file by running the following command:
oc apply -f egress-se.yaml
$ oc apply -f egress-se.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a YAML file named
waypoint.yamlthat creates a waypoint proxy inegress-gatewaynamespace similar to the following example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the YAML file by running the following command:
oc apply -f waypoint.yaml
$ oc apply -f waypoint.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
As an alternate to creating the waypoint.yaml YAML file, you can also set up waypoint proxy by running the following command:
istioctl waypoint apply --enroll-namespace --name waypoint --namespace egress-gateway
$ istioctl waypoint apply --enroll-namespace --name waypoint --namespace egress-gateway
When you use the --enroll-namespace option, all services in the egress-gateway namespace (including ServiceEntries), will route their traffic through the waypoint.
Verification
Verify the status of the gateway configuration by running the following command:
oc get gateways.gateway.networking.k8s.io waypoint -n egress-gateway
$ oc get gateways.gateway.networking.k8s.io waypoint -n egress-gatewayCopy to Clipboard Copied! Toggle word wrap Toggle overflow The
PROGRAMMEDcolumn showsTruewhen the configuration succeeds, similar to the following example:NAME CLASS ADDRESS PROGRAMMED AGE waypoint istio-waypoint 172.30.227.148 True 9s
NAME CLASS ADDRESS PROGRAMMED AGE waypoint istio-waypoint 172.30.227.148 True 9sCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
curlpod in theegress-gatewaynamespace by running the following command:oc run test-pod --image=curlimages/curl:latest -n egress-gateway --rm -it --restart=Never -- sh
$ oc run test-pod --image=curlimages/curl:latest -n egress-gateway --rm -it --restart=Never -- shCopy to Clipboard Copied! Toggle word wrap Toggle overflow By using the
curlclient, verify that you can accesshttpbin.orgthrough the egress gateway by running the following command:curl -v http://httpbin.org/get
$ curl -v http://httpbin.org/getCopy to Clipboard Copied! Toggle word wrap Toggle overflow The output shows a response from
httpbin.orgservice that indicates egress traffic routes through the configured gateway. The ztunnel logs should show traffic routed through the waypoint. The terminal should display information similar to the following output:2025-10-24T08:08:35.242159Z info access connection complete src.addr=[fd01:0:0:5::b0]:56288 src.workload="test-pod" src.namespace="egress-gateway" src.identity="spiffe://cluster.local/ns/egress-gateway/sa/default" dst.addr=[fd01:0:0:5::af]:15008 dst.hbone_addr=[2001:2::2]:80 dst.service="httpbin.org" dst.workload="waypoint-5b668759d5-vrnx8" dst.namespace="egress-gateway" dst.identity="spiffe://cluster.local/ns/egress-gateway/sa/waypoint" direction="outbound" bytes_sent=78 bytes_recv=540 duration="957ms"
2025-10-24T08:08:35.242159Z info access connection complete src.addr=[fd01:0:0:5::b0]:56288 src.workload="test-pod" src.namespace="egress-gateway" src.identity="spiffe://cluster.local/ns/egress-gateway/sa/default" dst.addr=[fd01:0:0:5::af]:15008 dst.hbone_addr=[2001:2::2]:80 dst.service="httpbin.org" dst.workload="waypoint-5b668759d5-vrnx8" dst.namespace="egress-gateway" dst.identity="spiffe://cluster.local/ns/egress-gateway/sa/waypoint" direction="outbound" bytes_sent=78 bytes_recv=540 duration="957ms"Copy to Clipboard Copied! Toggle word wrap Toggle overflow