Redirect HTTP to HTTPS on kubernetes 1.19 using AWS LoadBalancer Controller

The backstory

Lately we upgraded our Kubernetes cluster to v1.19 just to get this warning when deploying Ingress:

Warning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress

Well, usually I’m doing what I’m told, so I changed extensions/v1beta1 to networking.k8s.io/v1 Ingress just to find out that there are some breaking changes in the way the rules are being set up.

Here’s an example of how you should have done HTTP to HTTPS redirection with extensions/v1beta1 API version:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  namespace: default
  name: ingress
  annotations:
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/certificate-arn: arn:aws:acm:us-west-2:xxxx:certificate/xxxxxx
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP": 80}, {"HTTPS":443}]'
    alb.ingress.kubernetes.io/actions.ssl-redirect: '{"Type": "redirect", "RedirectConfig": { "Protocol": "HTTPS", "Port": "443", "StatusCode": "HTTP_301"}}'
spec:
  rules:
    - http:
        paths:
         - path: /*
           backend:
             serviceName: ssl-redirect
             servicePort: use-annotation
         - path: /users/*
           backend:
             serviceName: user-service
             servicePort: 80
         - path: /*
           backend:
             serviceName: default-service
             servicePort: 80

What’s the problem? I had a couple.

  1. pathType is now required after the “path” key, in order to specify if the path you provided is a prefix or an exact path that needs to be matched.
  2. serviceName and servicePort are not welcome anymore, and both should be provide using a map type.

So what are the changes

If map is what they want, map is what they should get.

Here’s the same yaml with the requested changes:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  namespace: default
  name: ingress
  annotations:
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/certificate-arn: arn:aws:acm:us-west-2:xxxx:certificate/xxxxxx
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP": 80}, {"HTTPS":443}]'
    alb.ingress.kubernetes.io/actions.ssl-redirect: '{"Type": "redirect", "RedirectConfig": { "Protocol": "HTTPS", "Port": "443", "StatusCode": "HTTP_301"}}'
spec:
  rules:
    - http:
        paths:
         - path: /*
           pathType: Prefix
           backend:
             service:
               name: ssl-redirect
               port:
                 name: use-annotation
         - path: /users/*
           pathType: Prefix
           backend:
             service:
               name: user-service
               port:
                 number: 80
         - path: /*
           pathType: Prefix
           backend:
             service:
               name: default-service
               port:
                 number: 80

The files are also located on my Github.

Leave a comment