Ashikur Rahman Rashid
Ashikur Rahman Rashid

Reputation: 182

Redirect HTTP to HTTPS automatically on GKE

I used Google-managed SSL certificate on GKE for set up SSL for my website. This is working example.com (that is insecure) and working also https://example.com(that is secure). when i access my website example.com why it's not redirect to https://example.com ? I used below yaml file to set up ssl.

apiVersion: networking.gke.io/v1
kind: ManagedCertificate
metadata:
  name: certificate-name
spec:
  domains:
    - domain-name1
    - domain-name2

Upvotes: 3

Views: 1018

Answers (2)

Andrej Palicka
Andrej Palicka

Reputation: 1011

There is now a supported way of doing this in GKE with Frontend Config: https://cloud.google.com/kubernetes-engine/docs/how-to/ingress-features#https_redirect

You will need GKE version at least 1.18.10-gke.600.

EDIT: As per comment by @mltsy, it also works in 1.17.17.

How we have it set up is:

apiVersion: networking.gke.io/v1beta2
kind: ManagedCertificate
metadata:
  name: <certificate-name>
spec:
  domains:
    - example.com
---
apiVersion: networking.gke.io/v1beta1
kind: FrontendConfig
metadata:
  name: <config-name>
spec:
  redirectToHttps:
    enabled: true
    responseCodeName: MOVED_PERMANENTLY_DEFAULT
---
kind: Ingress
apiVersion: networking.k8s.io/v1beta1
metadata:
  name: <ingress-name>
  annotations:
    kubernetes.io/ingress.class: "gce"
    networking.gke.io/managed-certificates: <certificate-name>
    kubernetes.io/ingress.global-static-ip-name: <ip-name>
    networking.gke.io/v1beta1.FrontendConfig: <config-name>
  labels:
    <labels-to-specify-app>
spec:
  rules:
    - host: example.com
      http:
        paths:
          - path: /*
            backend:
              serviceName: <service-name>
              servicePort: 80

Upvotes: 6

Milad Tabrizi
Milad Tabrizi

Reputation: 498

GKE ingress doesn't support HTTP to HTTPS redirect at this time. You can take a look at this Stack post. However, you can create a redirection rule for your Ingress resource using setting up an HTTP-to-HTTPS redirect for external HTTP(S) load balancers.

As per document you can disable HTTP by including the kubernetes.io/ingress.allow-http annotation in your Ingress manifest.

annotations:
kubernetes.io/ingress.allow-http: "false"

Upvotes: 0

Related Questions