ALB Ingresses support request path rewriting. When a request passes through the ALB instance, it can directly modify the request path and fetch content from the new path on the back-end server, enabling dynamic management of back-end services.
- The alb-ingress-controller add-on has been installed and upgraded to v0.35.0 or later. For details, see Upgrading add-ons.
- Supported configuration methods vary based on different ALB instance types.
- Standard edition: You can configure this type of instance by using the console or YAML.
- Basic edition: You can configure this type of instance only by using YAML.
Configure by using the console
tip
You can only use the console to configure an ALB ingress that uses a Standard edition ALB instance. To use a Basic edition ALB instance, see the following YAML configuration.
- In the left navigation bar, click Cluster.
- On the cluster list page, click the target cluster for which you need to configure an Ingress.
- In the left navigation bar of the cluster management page, click Services and Ingresses > Ingress. On the page that appears, click Create Ingress.
- Select Application Load Balancer (ALB) for Ingress type.
- Enable Custom forwarding rules and configure the details of the forwarding rules.
Configure by using kubectl
ALB Ingresses allow you to configure forward traffic rewriting rules by using annotations. The configuration slightly varies based on whether the ALB Ingress uses a Standard edition ALB instance or a Basic edition ALB instance.
Standard edition ALB
Basic edition ALB
The following content describes the sample configurations and annotation descriptions of the ALB Ingress when it uses a Standard edition ALB instance.
Warning
The {{service-name}} field in the annotation must match the name of the back-end service configured in spec.rules. For example, in this case, the correct annotation is ingress.vke.volcengine.com/loadbalancer-actions.service-demo.
The following table describes fields in the ingress.vke.volcengine.com/loadbalancer-actions.{{service-name}} annotation.
The following content describes the sample configurations and annotation descriptions of the ALB Ingress when it uses a Basic edition ALB instance.
The following table describes fields in the ingress.vke.volcengine.com/loadbalancer-rules-configs annotation.
tip
The details pages of forwarding rules differ between Standard edition ALB instances and Basic edition ALB instances. This topic provides an example of viewing the details page of a forwarding rule for a Standard edition ALB instance.
- Click Instance in the left navigation bar. In the instance list, find the target ALB instance and click Listener in the Actions column.
- On the listener details page, find the target listener. In the Actions column, click Edit forwarding rules to view the existing forwarding rules of the listener. You can view the newly added rewriting forwarding rule.
- For a forwarding rule, click
to view rule details. The rule details should be consistent with the configured ones.
Sending access requests for verification
tip
Before verification, make sure that you have deployed back-end applications and services in the cluster. In this example, an Nginx image is used and the service path is /aaa. For deployment steps, see Using ALB Ingresses for service deployment. Access the service by using the domain name configured in the ALB Ingress. In this example, 14.***.***.*** is the public IP address of the ALB instance.
The expected result is as follows: When the requested domain is example.com and the original path is /, the back-end service is accessible through the ALB Ingress after the path is rewritten to /aaa.
Regular expression matching
When the ALB Ingress uses a Standard edition ALB instance, matching and replacement based on regular expressions are supported. The basic rules are as follows:
- Static paths. Specify the exact target path, for example /aaa.
- Define one or multiple regular expression patterns in the Ingress path field. Multiple variables represented by () are supported. In rewriteConfig.path, reference one or more variables by using ${1}, ${2}, or ${3}. Up to three variables can be referenced.
- Dynamically construct desired rewrite rules by combining matched results based on regular expression patterns.
- Rewrites occur when client requests match the regular expression pattern containing multiple () variables and the rewriteConfig.path annotation contains one or more of placeholders ${1}, ${2}, and ${3}.
The following content describes the matching example of the ALB Ingress when it uses a Standard edition ALB instance.
Warning
The {{service-name}} field in the annotation must match the name of the back-end service configured in spec.rules. For example, in this case, the correct annotation is ingress.vke.volcengine.com/loadbalancer-actions.service-demo.
tip
For the ALB service, regular expressions must start with ~. In addition, Kubernetes requires that the path field of spec for an Ingress resource must start with /. As a result, the regular expression pattern in the path field in spec in the above example is written as /~gateway(/|$)(.*) to meet both requirements. However, the actual pattern applied is ~gateway(/|$)(.*). For details, see Creating forwarding rules. When the ALB Ingress uses a Basic edition ALB instance, matching and replacement based on regular expressions are supported. The basic rules are as follows:
- Host consistency. The host in the annotation must be identical to the host in spec.
- To specify a static path (such as /demo) in spec, set the path in the annotation to the exact path (/demo).
- To specify a regular expression (such as /~/(.*)) as the path in spec, for the path in the annotation, remove the leading / from the regular expression and set the value to ~/(.*).
- Static paths: Specify the exact target path. For example: /aaa.
- Define one or multiple regular expression patterns in the Ingress path field. Multiple variables represented by () are supported. In rewriteURI, reference one or more variables by using ${1}, ${2}, or ${3}. Up to three variables can be referenced.
- Dynamically construct desired rewrite rules by combining matched results based on regular expression patterns.
- Rewrites occur when client requests match a regular expression pattern containing multiple () variables and the rewriteURI annotation contains one or more placeholders ${1}, ${2}, and ${3}.
tip
For example, you configure path in the annotation as ~gateway/(.*)/(.*)/aaa, and rewriteURI as /${1}/${2}. If a client sends a request to the path /gateway/ccc/bbb/aaa, the path matches ~gateway/(.*)/(.*)/aaa, ${1} in rewriteURI is replaced with ccc, and ${2} is replaced with bbb. Ultimately, the back-end server receives the request path /ccc/bbb.
The following content describes the sample configurations of the ALB Ingress when it uses a Basic edition ALB instance.
tip
For the ALB service, regular expressions must start with ~. In addition, Kubernetes requires that the path field of spec for an Ingress resource must start with /. As a result, the regular expression pattern in the path field in spec in the above example is written as /~gateway(/|$)(.*) to meet both requirements. However, the actual pattern applied is ~gateway(/|$)(.*). For details, see Creating forwarding rules.