Define routing rules with the Gateway API
The Gateway API is an open-source project managed by the SIG-NETWORK community. It enhances service networking by providing expressive, extensible, and role-oriented interfaces. You can use the Gateway API to apply conditional restrictions to the routing rules for accessing applications within a cluster.
Prerequisites
-
You have added a cluster to an ASM instance, and the ASM instance is version 1.18 or later.
-
You have deployed an ingress gateway service, and ports 80 and 443 are enabled on the gateway.
-
You have deployed the httpbin application. For more information, see Step 1 in Deploy the httpbin application.
Notes
-
Version compatibility:
-
ASM 1.18 supports Gateway API v0.6.0.
-
ASM 1.22 and later supports Gateway API v1.1 and adds support for GRPCRoute.
-
ASM 1.24 and later supports Gateway API v1.2.0.
-
-
In multi-cluster mode, if you apply Gateway API resources with the same name to the same namespace in multiple data plane clusters, the last resource applied overwrites the previous ones.
Step 1: Verify Gateway API CRDs
The Gateway API component CRDs are installed by default in Container Service for Kubernetes (ACK) clusters v1.24 and later.
To verify that the CRDs exist in your ACK cluster, run the following command.
kubectl get crds | grep gateway.networking.k8s.io
-
If the CRDs are installed, the output is similar to the following:
gatewayclasses.gateway.networking.k8s.io 2023-05-10T02:51:33Z gateways.gateway.networking.k8s.io 2023-05-10T02:51:33Z httproutes.gateway.networking.k8s.io 2023-05-10T02:51:33Z referencegrants.gateway.networking.k8s.io 2023-05-10T02:51:33ZRun the following command to check the CRD version.
kubectl get crds -o yaml | grep 'gateway.networking.k8s.io/bundle-version'Expected output:
gateway.networking.k8s.io/bundle-version: v0.6.0 gateway.networking.k8s.io/bundle-version: v0.6.0 gateway.networking.k8s.io/bundle-version: v0.6.0 gateway.networking.k8s.io/bundle-version: v0.6.0 -
If the output does not include the expected CRDs, log on to the ACK console, go to the Add-ons page, and install the Gateway API component. For more information, see Manage add-ons.
Step 2: Enable Gateway API support
In the kubeconfig environment for your ASM instance, add the enableGatewayAPI: true field to the ASMMeshConfig resource named default.
apiVersion: istio.alibabacloud.com/v1beta1
kind: ASMMeshConfig
metadata:
name: default
spec:
enableGatewayAPI: true
After you enable the enableGatewayAPI switch, the control plane generates the relevant Gateway API CRDs. Both the Gateway API and Istio define a resource named Gateway, which can cause conflicts when you use kubectl. To resolve this, use the kubectl get gtw command to retrieve Gateway resources from the Gateway API and use the kubectl get gw command to retrieve Gateway resources from Istio.
Step 3: Configure an HTTP routing rule
Use the Gateway API to configure an HTTP routing rule that exposes the httpbin application via the gateway. You need to create Gateway and HTTPRoute resources in your ACK cluster.
-
Create a Gateway resource.
-
Create a file named gateway.yaml with the following content.
This configuration applies the Gateway resource to the specified ASM gateway. It creates a listener on port 80 that uses the HTTP protocol and sets the
hostnameto*.aliyun.com, which allows routing rules from all namespaces to attach to this listener. In the YAML file, replace${ASM_GATEWAY_NAME}with the name of your ASM gateway. -
In the kubeconfig environment for your ACK cluster, run the following command to deploy the Gateway.
kubectl apply -f gateway.yaml
-
-
Create an HTTPRoute resource.
-
Create a file named http-route.yaml with the following content.
This configuration attaches the HTTPRoute to the
gatewayresource in theistio-systemnamespace. Because no specific listener is named, the rule applies to all compatible listeners on the gateway. Requests with the/getpath prefix are routed to port 8000 of the httpbin service in the same namespace. -
In the kubeconfig environment for your ACK cluster, run the following command to deploy the HTTPRoute.
kubectl apply -f http-route.yaml
-
-
Run the following command to access the httpbin application through the ingress gateway and verify that the HTTP routing rule is active. Replace {INGRESS_GATEWAY_IP} with the IP address of your ingress gateway.
curl -I -HHost:httpbin.aliyun.com "http://{INGRESS_GATEWAY_IP}:80/get"Expected output:
HTTP/1.1 200 OK server: istio-envoy date: Fri, 12 May 2023 08:16:30 GMT content-type: application/json content-length: 516 access-control-allow-origin: * access-control-allow-credentials: true x-envoy-upstream-service-time: 4A
200 OKresponse confirms that the HTTP routing rule is active.
Step 4: Configure an HTTPS routing rule
Use the Gateway API to configure an HTTPS routing rule. This rule exposes the httpbin application and performs TLS termination at the ASM gateway. You need to create Gateway and HTTPRoute resources in the ACK cluster.
-
Use the global certificate management feature of ASM to create an HTTPS certificate for
a.aliyun.com. Name the certificatemyexample-credential. For more information, see Step 1: Prepare server certificates and private keys for multiple hosts. -
Create a Gateway resource.
-
Create a file named gateway-https.yaml with the following content.
In the YAML file, replace
${ASM_GATEWAY_NAME}with the name of your ASM gateway. -
In the kubeconfig environment for your ACK cluster, run the following command to deploy the Gateway.
kubectl apply -f gateway-https.yaml
-
-
Create an HTTPRoute resource.
-
Create a file named httpbin-https.yaml with the following content.
-
In the kubeconfig environment for your ACK cluster, run the following command to deploy the HTTPRoute.
kubectl apply -f httpbin-https.yaml
-
-
Run the following command to access the httpbin application through the ingress gateway and verify that the HTTPS routing rule is active. Replace {INGRESS_GATEWAY_IP} with the IP address of your ingress gateway.
curl -k -H Host:a.aliyun.com --resolve a.aliyun.com:443:{INGRESS_GATEWAY_IP} https://a.aliyun.com/status/418Expected output:
-=[ teapot ]=- _...._ .' _ _ `. | ."` ^ `". _, \_;`"---"`|// | ;/ \_ _/ `"""`The output confirms that the HTTPS routing rule is active.