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 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.
Redirecting HTTP to HTTPS
You can configure HTTP to HTTPS redirection using either the CCE console or kubectl.
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 kubernetes.io/elb.class: performance 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 port: number: 80 property: ingress.beta.kubernetes.io/url-match-mode: STARTS_WITH pathType: ImplementationSpecific ingressClassName: cce
Table 1 Key parameters Parameter
Mandatory
Type
Description
kubernetes.io/elb.listen-ports
Yes
String
Multi-port listening configuration. After configuration, this setting cannot 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/test-redirect-listener 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.