Questo contenuto non è disponibile nella lingua selezionata.
Gateways
Gateways and OpenShift Service Mesh
Abstract
Chapter 1. About gateways Copia collegamentoCollegamento copiato negli appunti!
Manage ingress and egress traffic at the mesh edge by using gateway injection or the Kubernetes Gateway API to deploy standalone Envoy proxies across sidecar and ambient architectures.
1.1. Understanding gateways Copia collegamentoCollegamento copiato negli appunti!
A gateway is a standalone Envoy proxy deployment and an associated Kubernetes service operating at the edge of a service mesh. You can configure a gateway to give fine-grained control over the traffic that enters or leaves the mesh.
In Red Hat OpenShift Service Mesh, you can install gateways by using gateway injection or by using the Gateway API. Red Hat OpenShift Service Mesh supports different gateway configurations based on the deployment mode. You can deploy gateways by using gateway injection and configure them with Istio Gateway and VirtualService resources in sidecar mode or with Kubernetes Gateway API resources in both sidecar and ambient modes.
1.2. About gateway injection Copia collegamentoCollegamento copiato negli appunti!
You can install a gateway by applying labels and annotations to Kubernetes Deployment and Service resources, triggering the Istio control plane to inject and configure the gateway proxy.
Gateway injection relies upon the same mechanism as sidecar injection to inject the Envoy proxy into gateway pods. To install a gateway by using gateway injection, you create a Kubernetes Deployment object and an associated Kubernetes Service object in a namespace that is visible to the Istio control plane. When creating the Deployment object, you apply labels and annotations so the Istio control plane injects and configures a proxy to act as a gateway. After installing the gateway, you configure it to control ingress and egress traffic by using the Istio Gateway and VirtualService resources.
1.2.1. Installing a gateway by using gateway injection Copia collegamentoCollegamento copiato negli appunti!
Install a gateway by using gateway injection to deploy and configure an Envoy proxy for managing traffic at the edge of the mesh.
You can use this procedure to create ingress or egress gateways.
Prerequisites
- You have installed the OpenShift Service Mesh Operator version 3.0 or later.
- You have created an Istio control plane.
-
You have created an
IstioCNIresource.
Procedure
Create a namespace that you will use to install the gateway.
$ oc create namespace <gateway_namespace>NoteInstall the gateway and the Istio control plane in different namespaces.
You can install the gateway in a dedicated gateway namespace. This approach allows many applications in different namespaces to share the same gateway. Or, you can install the gateway in an application namespace. In this approach, the gateway acts as a dedicated gateway for the application in that namespace.
Create a YAML file named
secret-reader.ymlthat defines the service account, role, and role binding for the gateway deployment, similar to the following example:apiVersion: v1 kind: ServiceAccount metadata: name: secret-reader namespace: <gateway_namespace> --- apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: secret-reader namespace: <gateway_namespace> rules: - apiGroups: [""] resources: ["secrets"] verbs: ["get", "watch", "list"] --- apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: secret-reader namespace: <gateway_namespace> roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: secret-reader subjects: - kind: ServiceAccount name: secret-readerThese settings enable the gateway to read secrets so that it can obtain TLS credentials.
Apply the YAML file by running the following command:
$ oc apply -f secret-reader.ymlCreate a YAML file named
gateway-deployment.ymlthat defines the KubernetesDeploymentobject for the gateway, similar to the following example:apiVersion: apps/v1 kind: Deployment metadata: name: <gateway_name> namespace: <gateway_namespace> spec: selector: matchLabels: istio: <gateway_name> template: metadata: annotations: inject.istio.io/templates: gateway labels: istio: <gateway_name> sidecar.istio.io/inject: "true" spec: containers: - name: istio-proxy image: auto securityContext: capabilities: drop: - ALL allowPrivilegeEscalation: false privileged: false readOnlyRootFilesystem: true runAsNonRoot: true ports: - containerPort: 15090 protocol: TCP name: http-envoy-prom resources: limits: cpu: 2000m memory: 1024Mi requests: cpu: 100m memory: 128Mi securityContext: sysctls: - name: net.ipv4.ip_unprivileged_port_start value: "0" serviceAccountName: secret-reader-
spec.template.annotations.inject.istio.io/templatesindicates that the Istio control plane uses the gateway injection template instead of the default sidecar template. -
spec.template.labels.istioensures that you set a unique label for the gateway deployment. IstioGatewayresources require a unique label to select gateway workloads. -
spec.template.labels.sidecar.istio.io/injectenables gateway injection by setting this label totrue. If the name of the Istio resource is notdefaultyou must use theistio.io/rev: <istio_revision>label instead, where the revision represents the active revision of the Istio resource. -
spec.template.spec.containers.imagesets the image field toautoso that the image automatically updates each time the pod starts. -
spec.template.spec.serviceAccountNamesets theserviceAccountNameto the name of theServiceAccountcreated previously.
-
Apply the YAML file by running the following command:
$ oc apply -f gateway-deployment.ymlVerify that the gateway
Deploymentrollout was successful by running the following command:$ oc rollout status deployment/<gateway_name> -n <gateway_namespace>You should see output similar to the following:
You will get an output similar to the following example:
Waiting for deployment "<gateway_name>" rollout to finish: 0 of 1 updated replicas are available... deployment "<gateway_name>" successfully rolled outCreate a YAML file named
gateway-service.ymlthat has the KubernetesServiceobject for the gateway.apiVersion: v1 kind: Service metadata: name: <gateway_name> namespace: <gateway_namespace> spec: type: ClusterIP1 selector: istio: <gateway_name>2 ports: - name: status-port port: 15021 protocol: TCP targetPort: 15021 - name: http2 port: 80 protocol: TCP targetPort: 80 - name: https port: 443 protocol: TCP targetPort: 443-
spec.typesets the type of the gatewayServiceobject. Settingspec.typetoClusterIPrestricts gateway Service access to the internal cluster network. If the gateway has to handle ingress traffic from outside the cluster, setspec.typetoLoadBalancer. Or, you can use OpenShift Routes. -
spec.selectorsets theselectorto the unique label or set of labels specified in the pod template of the gateway deployment that you created previously.
-
Apply the YAML file by running the following command:
$ oc apply -f gateway-service.ymlVerify that the gateway service is targeting the endpoint of the gateway pods by running the following command:
$ oc get endpoints <gateway_name> -n <gateway_namespace>You should see output similar to the following example:
Example output
NAME ENDPOINTS AGE <gateway_name> 10.131.0.181:8080,10.131.0.181:8443 1mOptional: Create a YAML file named
gateway-hpa.ymlthat defines a horizontal pod autoscaler for the gateway. The following example sets the minimum replicas to2and the maximum replicas to5and scales the replicas up when average CPU usage exceeds 80% of the CPU resource limit. The gateway deployment’s pod template specifies this limit.apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: <gateway_name> namespace: <gateway_namespace> spec: minReplicas: 2 maxReplicas: 5 metrics: - resource: name: cpu target: averageUtilization: 80 type: Utilization type: Resource scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: <gateway_name>-
spec.scaleTargetRef.namespecifies the name of the gateway deployment created previously.
-
Optional: Apply the YAML file by running the following command:
$ oc apply -f gateway-hpa.ymlOptional: Create a YAML file named
gateway-pdb.ymlthat defines a pod disruption budget for the gateway. The following example allows gateway pods eviction only when at least 1 healthy gateway pod will remain on the cluster after the eviction:apiVersion: policy/v1 kind: PodDisruptionBudget metadata: name: <gateway_name> namespace: <gateway_namespace> spec: minAvailable: 1 selector: matchLabels: istio: <gateway_name>-
spec.selector.matchLabelsspecifies the unique label or set of labels specified in the pod template of the gateway deployment created previously.
-
Optional: Apply the YAML file by running the following command:
$ oc apply -f gateway-pdb.yml
Chapter 2. Getting traffic into a mesh Copia collegamentoCollegamento copiato negli appunti!
Route external traffic to mesh services by configuring Istio gateway proxies and exposing them through LoadBalancer services or OpenShift routes.
2.1. About configuring a gateway installed using gateway injection to accept ingress traffic Copia collegamentoCollegamento copiato negli appunti!
Red Hat OpenShift Service Mesh offers two approaches to configure ingress traffic routing to services in the mesh. The approach depends on the service mesh deployment mode and traffic management requirements.
In the following example an Istio Gateway resource configures a gateway proxy to act as an entry point for external traffic. This configuration exposes port 443 (HTTPS) for the host, bookinfo.com. The example configuration applies to pods with the istio: ingressgateway label. The tls mode is configured as SIMPLE, which terminates the incoming HTTPS traffic using the certificate and private key the example provides.
Sample configuration
apiVersion: networking.istio.io/v1
kind: Gateway
metadata:
name: bookinfo-gateway
namespace: bookinfo
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 443
name: https-443
protocol: HTTPS
hosts:
- bookinfo.com
tls:
mode: SIMPLE
serverCertificate: /etc/certs/servercert.pem
privateKey: /etc/certs/privatekey.pem
The following VirtualService is bound to the Istio Gateway resource shown in the previous example configuration. The specification defines rules to route traffic with the /reviews/ path prefix to the reviews service in the bookinfo namespace. The VirtualService explicitly references the Gateway resource shown previously. This ensures that the routing rules are only applied to the traffic that enters through the specified gateway.
Sample configuration
kind: VirtualService
metadata:
name: bookinfo-rule
namespace: bookinfo
spec:
hosts:
- bookinfo.com
gateways:
- bookinfo/bookinfo-gateway
http:
- match:
- uri:
prefix: /reviews/
route:
- destination:
port:
number: 9080
host: reviews.bookinfo.svc.cluster.local
While Istio Gateway and VirtualService resources support certain ingress use cases in ambient mode, the recommended approach is to use the Kubernetes Gateway API, which provides full support and integration with ambient. You can also use the Gateway API with sidecar-based deployments.
2.2. Exposing a service by using the Istio Gateway and VirtualService resources Copia collegamentoCollegamento copiato negli appunti!
Expose mesh services to external traffic by configuring injected gateways with Istio Gateway and VirtualService resources to route traffic from outside the cluster.
You can set the gateway Service type to LoadBalancer to allow traffic from outside the cluster.
Prerequisites
- You have installed an Istio gateway using gateway injection.
Procedure
Create namespace called
httpbinby running the following command:$ oc create namespace httpbinEnable sidecar injection in the namespace. If you are using the
InPlaceupgrade strategy, run the following command:$ oc label namespace httpbin istio-injection=enabledNoteIf you are using the
RevisionBasedupgrade strategy, run the following commands:To find your
<revision-name>, run the following command:$ oc get istiorevisions.sailoperator.ioYou will get an output similar to the following example:
NAME TYPE READY STATUS IN USE VERSION AGE default Local True Healthy True v1.24.3 3m33sLabel the namespace with the revision name to enable sidecar injection:
$ oc label namespace httpbin istio.io/rev=default
Deploy a sample service named
httpbinby running the following command:$ oc apply -n httpbin -f https://raw.githubusercontent.com/openshift-service-mesh/istio/refs/heads/master/samples/httpbin/httpbin.yamlCreate a YAML file named
httpbin-gw.yamlthat defines an IstioGatewayresource. This resource configures gateway proxies to expose port 80 (HTTP) for the host,httpbin.example.com.You can see the following example configuration for reference:
apiVersion: networking.istio.io/v1 kind: Gateway metadata: name: httpbin-gateway namespace: httpbin spec: selector: istio: <gateway_name> servers: - port: number: 80 name: http protocol: HTTP hosts: - httpbin.example.com-
spec.selectorspecifies the unique label or set of labels in the pod template of the gateway proxyDeployment. By default, the IstioGatewayresource configuration will apply to matching gateway pods in all namespaces. -
spec.servers.hostsspecifies a list of addresses that the clients use when attempting to access a mesh service at the associated port.
-
Apply the YAML file by running the following command:
$ oc apply -f httpbin-gw.yamlCreate a YAML file named
httpbin-vs.yamlfor aVirtualService. TheVirtualServicedefines the rules that route traffic from the gateway proxy to thehttpbinservice.You can see the following example configuration for reference:
apiVersion: networking.istio.io/v1 kind: VirtualService metadata: name: httpbin namespace: httpbin spec: hosts: - httpbin.example.com gateways: - httpbin-gateway http: - match: - uri: prefix: /status - uri: prefix: /headers route: - destination: port: number: 8000 host: httpbin-
spec.hoststhe destinationhostsfor theVirtualServicerouting rules. The IstioGatewayresource must expose the hosts that you bind to theVirtualService. -
spec.gatewaysbinds theVirtualServiceto the IstioGatewayresource created in the previous step by adding theGatewayname to the list of gateways. -
spec.http.routeroute matching traffic to thehttpbinservice deployed earlier by defining adestinationthat includes thehostandportof thehttpbinService.
-
Apply the YAML file by running the following command:
$ oc apply -f httpbin-vs.yamlFor verification purposes, create a namespace for a
curlclient by running the following command:$ oc create namespace curlDeploy the
curlclient by running the following command:$ oc apply -n curl -f https://raw.githubusercontent.com/openshift-service-mesh/istio/refs/heads/master/samples/curl/curl.yamlSet a
CURL_PODvariable with the name of thecurlpod by running the following command:$ CURL_POD=$(oc get pods -n curl -l app=curl -o jsonpath='{.items[*].metadata.name}')Using the
curlclient, send a request to the/headersendpoint of thehttpbinapplication through the ingress gatewayServiceresource. Set theHostheader of the request tohttpbin.example.comto match the host that the IstioGatewayandVirtualServiceresources specify. Run the followingcurlcommand to send the request:$ oc exec $CURL_POD -n curl -- \ curl -s -I \ -H Host:httpbin.example.com \ <gateway_name>.<gateway_namespace>.svc.cluster.local/headersThe response should have a
200 OK HTTPstatus indicating that the request was successful.Example output
HTTP/1.1 200 OK server: istio-envoy ...Send a curl request to an endpoint that does not have a corresponding URI prefix match defined in the
httpbinVirtualServiceby running the following command:$ oc exec $CURL_POD -n curl -- \ curl -s -I \ -H Host:httpbin.example.com \ <gateway_name>.<gateway_namespace>.svc.cluster.local/getThe response should return a
404 Not Foundstatus as thehttpbinVirtualServiceresource lacks a matching URI prefix for the/getendpoint.Example output
HTTP/1.1 404 Not Found server: istio-envoy ...Expose the gateway proxy to traffic outside the cluster by setting the
Servicetype toLoadBalancer:$ oc patch service <gateway_name> -n <gateway_namespace> -p '{"spec": {"type": "LoadBalancer"}}'NoteOpenShift Routes can also expose a gateway to traffic outside the cluster. For more information, see "Exposing a gateway to traffic outside the cluster using OpenShift Routes".
Verify that you can access the
httpbinservice from outside the cluster when using the external hostname or IP address of the gatewayServiceresource. Ensure that you set theINGRESS_HOSTvariable appropriately for the environment that your cluster is running in.If the cluster runs on AWS, set the
INGRESS_HOSTvariable by running the following command:$ INGRESS_HOST=$(oc get service <gateway_name> -n <gateway_namespace> -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')If the cluster runs on GCP or Azure, set the
INGRESS_HOSTvariable by running the following command:$ INGRESS_HOST=$(oc get service <gateway_name> -n <gateway_namespace> -o jsonpath='{.status.loadBalancer.ingress[0].ip}')Send a
curlrequest to thehttpbinservice using the host of the gateway by running the following command:$ curl -s -I -H Host:httpbin.example.com http://$INGRESS_HOST/headers
-
Verify that the response has the
HTTP/1.1 200 OKstatus, which indicates that the request was successful.
2.3. About exposing services to traffic outside a cluster Copia collegamentoCollegamento copiato negli appunti!
To enable traffic from outside an OpenShift cluster to access services in a mesh, you must expose a gateway proxy by either setting its Service type to LoadBalancer or by using the OpenShift Router.
Using Kubernetes load balancing to handle incoming traffic directly through the inbound gateway can reduce latency associated with data encryption. By managing encryption at the inbound gateway, you avoid the intermediate decryption and re-encryption steps within the mesh that often add latency. This approach encrypts and decrypts mesh traffic only once, which is generally more efficient.
The OpenShift Router provides a standard approach for managing ingress traffic, and you can use the router to manage certificates for all cluster ingress traffic by using the same methods. However, the OpenShift Router introduces an additional hop between the inbound traffic and the mesh applications. Typically, you route the traffic by decrypting it at the router and then re-encrypting it at the service mesh ingress gateway, which introduces latency.
2.3.1. Exposing a gateway to traffic outside the cluster by using OpenShift Routes Copia collegamentoCollegamento copiato negli appunti!
You can expose a gateway to traffic outside the cluster by using OpenShift Routes. This approach provides an alternative to using Kubernetes LoadBalancer service when you have to expose gateways to traffic outside the cluster.
Prerequisites
-
You have completed the procedure, "Exposing a Service by using the Istio
GatewayandVirtualServiceresources".
Procedure
Ensure that you set the
Servicetype toClusterIPby running the following command:$ oc patch service <gateway_name> -n <gateway_namespace> -p '{"spec": {"type": "ClusterIP"}}'Create a YAML file named
httpbin-route.yamlthat defines aRoutefor thehttpbinservice similar to the following example:apiVersion: route.openshift.io/v1 kind: Route metadata: name: httpbin namespace: <gateway_namespace> spec: host: httpbin.example.com port: targetPort: http2 to: kind: Service name: <gateway_name> weight: 100 wildcardPolicy: NoneApply the YAML file by running the following command:
$ oc apply -f httpbin-route.yamlVerify that you can access the
httpbinservice from outside the cluster through the ingress router. Ensure that you set theINGRESS_HOSTvariable appropriately for the environment that your cluster is running in.If the cluster runs on AWS, set the
INGRESS_HOSTvariable by running the following command:$ INGRESS_HOST=$(oc get service router-default -n openshift-ingress -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')If the cluster runs on GCP or Azure, set the
INGRESS_HOSTvariable by running the following command:$ INGRESS_HOST=$(oc get service router-default -n openshift-ingress -o jsonpath='{.status.loadBalancer.ingress[0].ip}')Send a
curlrequest to thehttpbinservice using the host of the ingress router by running the following command:$ curl -s -I -H Host:httpbin.example.com http://$INGRESS_HOST/headers
-
Verify that the response has the
HTTP/1.1 200 OKstatus, which indicates that the request was successful.
2.4. Exposing a service by using the Kubernetes Gateway API Copia collegamentoCollegamento copiato negli appunti!
You can use the Kubernetes Gateway API to create Gateway and HTTPRoute resources and deploy a gateway. The resources configure the gateway to expose a service in the mesh to traffic outside the mesh.
Prerequisites
-
You have logged in to the OpenShift Container Platform web console as a user with the
cluster-adminrole. - You installed the Red Hat OpenShift Service Mesh Operator.
- You deployed the Istio resource.
Procedure
Create a namespace called
httpbinby running the following command:$ oc create namespace httpbinDeploy a sample service named
httpbinby running the following command:$ oc apply -n httpbin -f https://raw.githubusercontent.com/openshift-service-mesh/istio/refs/heads/master/samples/httpbin/httpbin.yamlCreate a YAML file named
httpbin-gw.yamlthat defines a Kubernetes Gateway resource. This resource configures gateway proxies to expose port 80 (HTTP) for the host,httpbin.example.com.Example gateway resource file
apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: httpbin-gateway namespace: httpbin spec: gatewayClassName: istio listeners: - name: default hostname: "httpbin.example.com"1 port: 80 protocol: HTTP allowedRoutes: namespaces: from: AllApply the YAML file by running the following command:
$ oc apply -f httpbin-gw.yamlCreate a YAML file named
httpbin-hr.yamlthat defines anHTTPRouteresource. TheHTTPRouteresource specifies the rules that route traffic from the gateway proxy to thehttpbinservice.Example HTTPRoute file
apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin namespace: httpbin spec: parentRefs:1 - name: httpbin-gateway namespace: httpbin rules: - matches: - path: type: PathPrefix value: /status - path: type: PathPrefix value: /headers backendRefs:2 - name: httpbin port: 8000-
spec.parentRefsbinds theHTTPROUTEresource to the KubernetesGatewayresource you created in the previous step. -
spec.rules.backendRefsroutes the matching traffic to thehttpbinservice by defining abackendRefsthat includes the name and port of thehttpbinservice.
The
HTTPRouteresource specifies the rules that route traffic from the gateway proxy to thehttpbinservice.-
Apply the YAML file by running the following command:
$ oc apply -f httpbin-hr.yamlEnsure that the Gateway API service is ready and has an allocated address by running the following command:
$ oc wait --for=condition=programmed gtw httpbin-gateway -n httpbin
Verification
Create a namespace for a
curlclient by running the following command:$ oc create namespace curlDeploy a
curlclient by running the following command:$ oc apply -n curl -f https://raw.githubusercontent.com/openshift-service-mesh/istio/refs/heads/master/samples/curl/curl.yamlSet a
CURL_PODvariable with the name of thecurlpod by running the following command:$ CURL_POD=$(oc get pods -n curl -l app=curl -o jsonpath='{.items[*].metadata.name}')Using the
curlclient, send a request to the/headersendpoint of thehttpbinapplication through the ingress gatewayServiceresource. Set the Host header of the request tohttpbin.example.comto match the host that the Kubernetes Gateway andHTTPROUTEresources specify. Send thecurlrequest by running the following command:$ oc exec $CURL_POD -n curl -- \ curl -s -I \ -H Host:httpbin.example.com \ <gateway_name>-istio.<gateway_namespace>.svc.cluster.local/headersThe response should return a
200 OKHTTP status, which indicates that the request was successful.Example output
HTTP/1.1 200 OK server: istio-envoy ...Send a
curlrequest to an endpoint that does not have a corresponding Uniform Resource Identifier (URI) prefix match defined in thehttpbinHTTPROUTEby running the following command:$ oc exec $CURL_POD -n curl -- \ curl -s -I \ -H Host:httpbin.example.com \ <gateway_name>-istio.<gateway_namespace>.svc.cluster.local/getThe response returns a
404 Not Foundstatus. This is expected because the/getendpoint does not have a matching URI prefix in thehttpbinHTTPROUTEresource.Example output
HTTP/1.1 404 Not Found server: istio-envoy ...Expose the gateway proxy to traffic outside the cluster by setting the
Servicetype toLoadBalancer. Run the following command:$ oc patch service <gateway_name>-istio -n <gateway_namespace> -p '{"spec": {"type": "LoadBalancer"}}'NoteOpenShift Routes can also expose a gateway to traffic outside the cluster. For more information, see "Exposing a gateway to traffic outside the cluster using OpenShift Routes".
Verify that you can access the
httpbinservice from outside the cluster when using the external hostname or IP address of the gateway Service resource. Ensure that you set theINGRESS_HOSTvariable appropriately for the environment in which your cluster is running.Set the
INGRESS_HOSTvariable by running the following command:$ export INGRESS_HOST=$(oc get gtw <gateway_name> -n <gateway_namespace> -o jsonpath='{.status.addresses[0].value}')Set the
INGRESS_PORTvariable by running the following command:$ INGRESS_PORT=$(oc get gtw <gateway_name> -n <gateway_namespace> -o jsonpath='{.spec.listeners[?(@.name=="http")].port}')Using the gateway host, send a
curlrequest to thehttpbinservice by running the following command:$ curl -s -I -H Host:httpbin.example.com http://$INGRESS_HOST:$INGRESS_PORT/headers
-
Verify that the response has the
HTTP/1.1 200 OKstatus, which indicates that the request was successful.
Chapter 3. Directing outbound traffic through a gateway Copia collegamentoCollegamento copiato negli appunti!
Using Istio APIs, you configure gateway proxies, installed through gateway injection, to direct traffic bound for an external service.
3.1. About directing egress traffic through a gateway Copia collegamentoCollegamento copiato negli appunti!
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 acts as a forward proxy and serves as the exit point for traffic exiting the service mesh toward 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, you deploy gateway proxies on dedicated egress nodes that can access external services. You can then enforce strict network policies or add monitoring to these nodes 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:
-
The
ServiceEntryadds the external service to the Istio service registry. You can then apply Istio features, such as monitoring and routing rules, to traffic heading toward that 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.
-
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 route mesh traffic to destinations outside the cluster. Service Mesh fully supports the Gateway API in ambient mode, and you can also use it with sidecar-based deployments, providing a consistent configuration model for both ingress and egress routing.
3.2. Directing egress traffic through a gateway using Istio APIs Copia collegamentoCollegamento copiato negli appunti!
Use Istio APIs to direct outbound HTTP traffic through a gateway that you 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 curlDepending 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=enabledIf you are using the
RevisionBasedupdate strategy, run the following commands:Display the revision name by running the following command:
$ oc get istiorevisions.sailoperator.ioYou will get an output similar to the following example:
NAME TYPE READY STATUS IN USE VERSION AGE default Local True Healthy True v1.24.3 3m33sLabel the namespace with the revision name to enable sidecar injection by running the following command:
$ oc label namespace curl istio.io/rev=default
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.yamlExport a
CURL_PODenvironment variable and initialize it with the name of the curl pod by running the following command:$ export CURL_POD=$(oc get pod -n curl -l app=curl -o jsonpath='{.items[0].metadata.name}')Create a YAML file named
http-se.yamlthat directs traffic from the mesh to an external service. The following example defines aServiceEntryfor a URL:Example configuration
apiVersion: networking.istio.io/v1 kind: ServiceEntry metadata: name: egress-se namespace: curl spec: hosts: - docs.redhat.com ports: - number: 80 name: http-port protocol: HTTP location: MESH_EXTERNAL resolution: DNSApply the YAML file by running the following command:
$ oc apply -f http-se.yamlEnsure that you applied the
ServiceEntryconfiguration 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.comThis 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, similar to the following example:Example configuration
apiVersion: networking.istio.io/v1alpha3 kind: Gateway metadata: name: egress-gw namespace: <gateway_namespace> # Namespace where the egress gateway is deployed spec: selector: istio: <gateway_name> # Selects the egress-gateway instance to handle this traffic servers: - port: number: 80 name: http protocol: HTTP hosts: - docs.redhat.com # External service host, not a full URL. --- apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: egress-dr namespace: <gateway_namespace> # Namespace where the egress gateway is deployed spec: host: <gateway_name>.<gateway_namespace>.svc.cluster.local subsets: - name: rh-docsApply the YAML file by running the following command:
$ oc apply -f http-gtw.yamlCreate 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, similar to the following example:Example configuration
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: egress-vs namespace: curl # Namespace where the curl pod is running spec: hosts: - docs.redhat.com # External service host, not a full URL. gateways: - mesh - <gateway_namespace>/egress-gw # Egress gateway name defined in the file that you used in the previous step. http: - match: - gateways: - mesh port: 80 route: - destination: host: <gateway_name>.<gateway_namespace>.svc.cluster.local subset: rh-docs port: number: 80 weight: 100 - match: - gateways: - <gateway_namespace>/egress-gw # Egress gateway name defined in the file that you used in the previous step. port: 80 route: - destination: host: docs.redhat.com port: number: 80 weight: 100Apply the YAML file by running the following command:
$ oc apply -f http-vs.yamlResend the HTTP request to the URL:
$ oc exec "$CURL_POD" -n curl -c curl -- curl -sSL -o /dev/null -D - http://docs.redhat.comThe terminal should display information similar to the following output:
Example output
... HTTP/1.1 301 Moved Permanently ... location: <example_url> ... HTTP/2 200 Content-Type: text/html; charset=utf-8Ensure that the gateway routed the request by running the following command:
$ oc logs deployment/<gateway_name> -n <gateway_namespace> | tail -1NoteYou must enable access logging 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:
Example 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 -
3.3. Directing egress traffic through a gateway by using the Kubernetes Gateway API Copia collegamentoCollegamento copiato negli appunti!
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 {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 enabled the CRDs before you can use them. OpenShift Container Platform 4.19 and later versions enable the CRDs by default.
Create a YAML file named
gateway-cr.yamlthat enables the Kubernetes Gateway API CRDs, similar to the following example:Example Kubernetes Gateway Custom Resource (CR) file
apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: bookinfo-gateway spec: gatewayClassName: istio listeners: - name: http port: 80 protocol: HTTP allowedRoutes: namespaces: from: Same --- apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: bookinfo spec: parentRefs: - name: bookinfo-gateway rules: - matches: - path: type: Exact value: /productpage - path: type: PathPrefix value: /static - path: type: Exact value: /login - path: type: Exact value: /logout - path: type: PathPrefix value: /api/v1/products backendRefs: - name: productpage port: 9080Apply the YAML file by running the following command:
$ oc apply -f gateway-cr.yaml
Create a namespace called
egress-gatewayby running the following command:$ oc create namespace egress-gatewayApply the
istio-injectionlabel to the namespace by running the following command:$ oc label namespace egress-gateway istio-injection=enabledCreate a YAML file named
egress-gateway-cr.yamlthat defines the egress gateway, similar to the following example:Example egress gateway CR file
# ServiceEntry to allow traffic to httpbin.org apiVersion: networking.istio.io/v1 kind: ServiceEntry metadata: name: httpbin-ext spec: hosts: - httpbin.org ports: - number: 80 name: http protocol: HTTP location: MESH_EXTERNAL resolution: DNS --- # Gateway API Gateway for egress apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: httpbin-egress-gateway annotations: networking.istio.io/service-type: ClusterIP spec: gatewayClassName: istio listeners: - name: http hostname: httpbin.org port: 80 protocol: HTTP allowedRoutes: namespaces: from: Same --- # HTTPRoute to direct traffic from sidecars to the egress gateway apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: direct-httpbin-to-egress-gateway spec: parentRefs: - kind: ServiceEntry group: networking.istio.io name: httpbin-ext rules: - backendRefs: - name: httpbin-egress-gateway-istio port: 80 --- # HTTPRoute to forward traffic from the egress gateway to httpbin.org apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: forward-httpbin-from-egress-gateway spec: parentRefs: - name: httpbin-egress-gateway hostnames: - httpbin.org rules: - backendRefs: - kind: Hostname group: networking.istio.io name: httpbin.org port: 80Apply the YAML file by running the following command:
$ oc apply -f egress-gateway-cr.yaml
Verification
Verify the status of the gateway configuration by running the following command:
$ oc describe gateway -n egress-gatewayThe
Statuscolumn displaysProgrammedto indicate the required output.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 -- shBy using the
curlclient, verify that you can accesshttpbin.orgthrough the egress gateway by entering following command:$ curl -v http://httpbin.org/getThe required output shows a response from
httpbin.orgthat indicates egress traffic routes through the configured gateway.