Configure path redirection

更新时间:
复制 MD 格式

After you configure path redirection for a service, the service will respond to a specific request with an HTTP 301 status code to redirect the request to another path of the service. This topic describes how to configure path redirection.

Prerequisites

The preparations are completed, and the HTTPBin, sleep, and NGINX services are deployed. For more information, see Preparations.

Procedure

  1. Configure a virtual service for the HTTPBin service to forward requests destined for the HTTPBin service to the NGINX service.

    1. Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.

    2. On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose Traffic Management Center > VirtualService. On the page that appears, click Create.

    3. On the Create page, enter the following YAML configuration. Click Preview, confirm that the YAML is correct, click Submit, and then click Create.

      For more information about the parameters, see Virtual Service CRD.

      apiVersion: networking.istio.io/v1beta1
      kind: VirtualService
      metadata:
        name: httpbin
        namespace: default
      spec:
        hosts:
          - httpbin.default.svc.cluster.local
        http:
          - match:
              - uri:
                  exact: /
            redirect:
              uri: /get
  2. Run the following command to verify the path redirection. Ensure your environment is configured with kubeconfig access to your ACK cluster.

    kubectl exec -it deploy/sleep -- curl httpbin:8000 -I

    Expected output:

    HTTP/1.1 301 Moved Permanently
    location: http://httpbin:8000/get
    date: Mon, 14 Aug 2023 02:51:52 GMT
    server: envoy
    transfer-encoding: chunked

    You can see that a request is sent to the root path of the HTTPBin service in the sleep container and is responded with an HTTP 301 status code. In the response, the location field is set to http://httpbin:8000/get, which indicates that the request is redirected to the /get path of the HTTPBin service.