Kubernetes Loadbalancer redirecting to HTTPs when application is on port 80

12/19/2018

I've deployed a series of deployments and services to a Kubernetes cluster with a load balancer. When I try to access my app this does not work as my application is exposed on port 80 but the URL is always redirected to port 443 (HTTPS). I suspect this is to do with the fact that the cluster IP is on port 443.

Any ideas on how I can fix this?

db           NodePort       10.245.175.203   <none>           5432:30029/TCP   25m
kubernetes   ClusterIP      10.245.0.1       <none>           443/TCP          8m
redis        NodePort       10.245.197.157   <none>           6379:31277/TCP   25m
web          LoadBalancer   10.245.126.122   123.12.123.123   80:31430/TCP     25m
-- Rutnet
kubernetes

1 Answer

12/19/2018

This is likely due to your application itself redirecting to port 443. What type of application is it?

This service exposed on port 443 has nothing to do with your application:

kubernetes   ClusterIP      10.245.0.1       <none>           443/TCP          8m

It's basically an internal service that allows you to access the kube-apiserver within your cluster.

You could try just setting up the LoadBalancer to listen on port 443 directly. Only you would have to port 80 traffic wouldn't work. If you want the port 80 redirects to work I suggest you use an Ingress controller like nginx. Something like this:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: your-ingress
  annotations:
     nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
spec:
  tls:
  - hosts:
    - yourhostname.com
    secretName: tls-secret
  rules:
  - host: yourhostname.com
    http:
      paths:
      - path: /
        backend:
          serviceName: web
          servicePort: 443

You will also have to create a TLS secret holding your cert and key:

$ kubectl create secret tls tls-secret --key /tmp/tls.key --cert /tmp/tls.crt
-- Rico
Source: StackOverflow