Exposing a service via Istio Gateway and VirtualService resources
This guide demonstrates using Istio Gateway and VirtualService resources to configure a gateway deployed via gateway injection. These resources set up the gateway to expose a service inside the mesh to traffic from outside. Afterward, you expose the gateway to traffic external to the cluster by changing the gateway's Service to type LoadBalancer.
Use this approach for sidecar-based deployments where you already use Istio Gateway and VirtualService resources and do not plan to migrate to ambient mode. For ambient mode, use the Kubernetes Gateway API instead.
Prerequisites
- An Istio gateway has been installed using gateway injection.
- Your Kubernetes cluster supports external load balancers (i.e., Services of type
LoadBalancer).
Procedure
-
Create a new namespace named
httpbinby executing the command below: -
Enable sidecar injection for the namespace. If your setup uses the
InPlaceupgrade strategy, run this command:NOTEIf you are using the
RevisionBasedupgrade strategy, execute these commands:-
To discover your
<revision-name>, run the following:Sample output:
-
Label the namespace using the revision name to enable sidecar injection:
-
-
Deploy the
httpbinsample service by running the following command: -
Create a file named
httpbin-gw.yamlthat contains an IstioGatewayresource definition. This resource configures the gateway proxies to open port 80 (HTTP) for the hosthttpbin.example.com.- Set the
selectorto match the unique label or labels defined in the pod template of the gateway proxyDeployment. By default, the IstioGatewayconfiguration applies to matching gateway pods across all namespaces. - In the
hostsfield, list the addresses that clients can use to access a mesh service on the corresponding port.
- Set the
-
Apply the YAML file with this command:
-
Create another YAML file named
httpbin-vs.yamlfor aVirtualService. ThisVirtualServicewill define rules to route traffic from the gateway proxy to thehttpbinservice.- Define the
hoststo which theVirtualServicerouting rules will apply. The specifiedhostsmust be exposed by the IstioGatewayresource to which thisVirtualServiceis attached. - Attach the
VirtualServiceto the IstioGatewayresource from the previous step by adding theGateway's name to the gateways list. - Direct matching traffic to the previously deployed
httpbinservice by defining adestinationthat specifies thehostandportof thehttpbinService.
- Define the
-
Apply the YAML file using this command:
Verification
-
Create a namespace for a
curlclient by executing this command: -
Deploy the
curlclient with the following command: -
Store the name of the
curlpod in aCURL_PODvariable by running this command: -
From the
curlclient, send a request to the/headersendpoint of thehttpbinapplication via the ingress gatewayService. Set theHostheader tohttpbin.example.comto align with the host specified in the IstioGatewayandVirtualService. Execute the followingcurlcommand: -
The response should show a
200 OKHTTP status, confirming the request was successful.Example output
-
Send another request to an endpoint that lacks a corresponding URI prefix match in the
httpbinVirtualServiceby running this command:The response should be a
404 Not Foundstatus. This is the expected outcome because the/getendpoint does not have a defined URI prefix match in thehttpbinVirtualService.Example output
-
Expose the gateway proxy to traffic from outside the cluster by changing its
Servicetype toLoadBalancer: -
Confirm that the
httpbinservice is accessible from outside the cluster using the gatewayService's external hostname or IP address. Make sure to set theINGRESS_HOSTvariable correctly for your cluster's environment.a. Set the
INGRESS_HOSTvariable with this command:In certain environments, the load balancer may be exposed using a host name, instead of an IP address. In this case, the ingress gateway's
EXTERNAL-IPvalue will not be an IP address, but rather a host name, and the above command will have failed to set theINGRESS_HOSTenvironment variable. Use the following command to correct theINGRESS_HOSTvalue:b. Send a
curlrequest to thehttpbinservice using the gateway's host by running this command:INFOIf
$INGRESS_HOSTis an IPv6 address, enclose it in square brackets when constructing the URL. For example: -
Check that the response includes the
HTTP/1.1 200 OKstatus, which confirms the request succeeded.
Cleanup
Remove the resources created in this procedure: