Redirecting HTTP to HTTPS for a LoadBalancer Ingress
Ingresses can forward HTTP access requests to HTTPS listeners. The following is an example for redirecting the requests for accessing example.com/test of an ingress to HTTPS port 443.
Prerequisites
- A CCE standard or Turbo cluster is available.
- An available workload has been deployed in the cluster for external access. If no workload is available, deploy a workload by referring to Creating a Deployment, Creating a StatefulSet, or Creating a DaemonSet.
- A Service for external access has been configured for the workload. Services Supported by LoadBalancer Ingresses lists the Service types supported by LoadBalancer ingresses.
Notes and Constraints
If the cluster version is v1.23.14-r0, v1.25.9-r0, v1.27.6-r0, v1.28.4-r0, or later, HTTP to HTTPS redirection can only be configured for ingresses using a dedicated load balancer.
If the cluster version is v1.27.16-r50, v1.28.15-r40, v1.29.15-r0, v1.30.14-r0, v1.31.10-r0, v1.32.6-r0, v1.33.1-r0, or later, HTTP to HTTPS redirection can also be configured for ingresses using a shared load balancer.
Redirecting HTTP to HTTPS
You can configure HTTP to HTTPS redirection using either the CCE console or kubectl.
- Log in to the CCE console and click the cluster name to access the cluster console.
- Choose Services and Ingresses in the navigation pane, click the Ingresses tab, and click Create Ingress in the upper right corner.
- Configure ingress parameters.
This example explains only key parameters for redirecting HTTP to HTTPS. You can configure other parameters as required. For details, see Creating a LoadBalancer Ingress on the Console.
Table 1 Key parameters Parameter
Description
Example
Name
Enter an ingress name.
ingress-test
Load Balancer
Select a load balancer to be associated with the ingress or automatically create a load balancer.
Dedicated
Listener
- Frontend Protocol: Select HTTP for redirecting HTTP to HTTPS.
- External Port: specifies the port of the load balancer listener. NOTE:
After configuration, this setting cannot be modified in clusters of a version earlier than v1.23.18-r10, v1.25.16-r0, v1.27.16-r0, v1.28.13-r0, v1.29.8-r0, or v1.30.4-r0. In clusters v1.23.18-r10, v1.25.16-r0, v1.27.16-r0, v1.28.13-r0, v1.29.8-r0, v1.30.4-r0, or later, this setting can be modified.
- Redirect to HTTPS: After this function is enabled, you can configure the HTTPS port.
- External Port: Enter an HTTPS port.
- Certificate Source: TLS secret and ELB server certificates are supported.
- Backend Protocol: Select HTTP or HTTPS based on the backend service.
- SSL Authentication: This parameter is available only when Certificate Source is set to ELB server certificate. Clusters v1.28.15-r60, v1.29.15-r20, v1.30.14-r20, v1.31.10-r20, v1.32.6-r20, v1.33.5-r10, or later support this configuration.
- One-way authentication: Only the backend server is authenticated. If you also need to authenticate the identity of the client, select two-way authentication.
- Two-way authentication: Both the clients and the load balancer authenticate each other. This ensures only authenticated clients can access the load balancer. No additional backend server configuration is required if you select this option.
- CA Certificate: If SSL Authentication is set to Two-way authentication, add a CA certificate to authenticate the client. A CA certificate is issued by a Certificate Authority (CA) and is used to verify the issuer of the client's certificate. If HTTPS two-way authentication is enabled, HTTPS connections can be established only if the client provides a certificate issued by a specific CA.
- Server Certificate: Use a certificate created on ELB.
If no certificate is available, go to the ELB console and create one. For details, see Adding a Certificate.
- Frontend Protocol: HTTP
- External Port: 80
- Redirect to HTTPS: enabled
- External Port: 443
- Certificate Source: ELB server certificate
- SSL Authentication: One-way authentication
- Server Certificate: cert-test
Forwarding Policy
- Domain Name: Enter an actual domain name to be accessed. If it is left blank, the ingress can be accessed through an IP address. The domain name must be registered and filed. Once used by a forwarding policy, only that domain name will be accepted for access.
- Path Matching Rule: Choose Prefix match, Exact match, or RegEx match.
- Path: comes from a backend application for external access. It must work in the backend application. Otherwise, forwarding will not take effect.
- Destination Service: Select an existing Service. Only Services that meet the requirements are automatically displayed in the Service list. If no Service meets the requirements, create one by following the operations provided in Services Supported by Ingresses.
- Destination Service Port: Select the access port of the destination Service.
- Domain Name: You do not need to configure this parameter.
- Path Matching Rule: RegEx match
- Path: /
- Destination Service: nginx
- Destination Service Port: 80
Figure 1 Redirecting HTTP to HTTPS
Figure 2 Configuring a port for redirecting HTTP to HTTPS
- Click Create.
You can use annotations to redirect the requests of an ingress to an HTTPS listener. The following is an example:
- Use kubectl to access the cluster. For details, see Accessing a Cluster Using kubectl.
- Create a YAML file named ingress-test.yaml. The file name can be customized.
vi ingress-test.yaml
An example YAML file of an ingress associated with an existing load balancer is as follows:
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: test-redirect-listener namespace: default annotations: kubernetes.io/elb.id: df76342f-e898-402a-bac8-bde5bf974da8 # Load balancer ID kubernetes.io/elb.class: performance # Load balancer type kubernetes.io/elb.listen-ports: '[{"HTTP":80},{"HTTPS":443}]' # Multi-port configuration kubernetes.io/elb.ssl-redirect: 'true' # Enable redirection from HTTP to HTTPS. kubernetes.io/elb.tls-certificate-ids: 6cfb43c9de1a41a18478b868e34b0a82 # HTTPS listener server certificate spec: rules: - host: '' http: paths: - path: / backend: service: name: test-service # Replace it with your target Service name. port: number: 80 # Replace it with your target Service port. property: ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH pathType: ImplementationSpecific ingressClassName: cceTable 2 Key parameters Parameter
Mandatory
Type
Description
kubernetes.io/elb.listen-ports
Yes
String
Multi-port listening configuration. After configuration, this setting cannot be modified in clusters of a version earlier than v1.23.18-r10, v1.25.16-r0, v1.27.16-r0, v1.28.13-r0, v1.29.8-r0, or v1.30.4-r0. In clusters v1.23.18-r10, v1.25.16-r0, v1.27.16-r0, v1.28.13-r0, v1.29.8-r0, v1.30.4-r0, or later, this setting can be modified.
Format: The value is a JSON string, for example:
[{"HTTP":80},{"HTTPS":443}]Parameter: The port number ranges from 1 to 65535.
NOTE:You can configure this annotation along with the one for enabling HTTP/2, but HTTP/2 takes effect only on HTTPS ports.
kubernetes.io/elb.ssl-redirect
Yes
String
Whether to enable redirection from HTTP to HTTPS.
Format: The value can be true or false.
Parameter: true indicates that redirection is enabled. If the value is false or the parameter is unavailable, redirection is disabled.
NOTE:Either this annotation or the annotation of a grayscale release can be configured.
- Create an ingress.
kubectl create -f ingress-test.yaml
If information similar to the following is displayed, the ingress has been created:
ingress.networking.k8s.io/ingress-test created
- Check the created ingress.
kubectl get ingress
If information similar to the following is displayed, the ingress has been created:
NAME CLASS HOSTS ADDRESS PORTS AGE test-redirect-listener cce * 121.**.**.** 80 10s
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot