Nginx Redirection of Jenkins on Kubernetes

7/18/2020

I am trying to deploy Jenkins on Kubernetes. I have deployed it with ClusterIP along with Nginx Ingress Controller on AKS.

When I access the IP of the Ingress-Controller, the Jenkins login URL (http://ExternalIP/login?from=%2F) comes up. However the UI of the Jenkins page isn't coming up and there is a some sort of redirection happening and keeps growing (http://ExternalIP/login?from=%2F%3Ffrom%3D%252F%253Ffrom%253D%25252F%25253F). I am very new to Ingress controller and annotations. I am not able to figure on what's causing this redirection.

Below are my configuration files. Can anyone please help on what's going wrong ?

ClusterIP-Service.yml

kind: Service
apiVersion: v1
metadata:
  name: jenkins-nodeport-svc
  namespace: jenkins
  labels:
    env: poc
    app: myapp_jenkins
spec:
  ports:
  - name: "http"
    port: 80
    targetPort: 8080
  type: ClusterIP
  selector:
      app: myapp_jenkins

Ingress.yml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: jenkins-ingress
  namespace: jenkins
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/backend-protocol: "HTTP"
    nginx.ingress.kubernetes.io/cors-allow-credentials: "true"
    nginx.ingress.kubernetes.io/cors-allow-headers: Authorization, origin, accept
    nginx.ingress.kubernetes.io/cors-allow-methods: GET, OPTIONS
    nginx.ingress.kubernetes.io/enable-cors: "true"

spec:
  rules:
  - http:
      paths:
       - backend:
          serviceName: jenkins-nodeport-svc
          servicePort: 80
         path: /(.*)
-- rb16
azure-aks
jenkins
kubernetes
kubernetes-ingress

1 Answer

7/18/2020

There's something in your ingress:

path: /(.*)

is a regular expression with a single capturing group that match everything. For example with following url: http://ExternalIP/login?from=myurl your capturing group $1 (the first and only one) would match login?from/myurl.

Now the problem is that nginx.ingress.kubernetes.io/rewrite-target: /$2 annotation is rewriting your url with a non existing capturing group.

You don't need rewriting, you just need to plain forward every request to the service.
Here you can find Rewrite Examples if you interested on it.

But in your case you can set:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: jenkins-ingress
  namespace: jenkins
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/backend-protocol: "HTTP"
    nginx.ingress.kubernetes.io/cors-allow-credentials: "true"
    nginx.ingress.kubernetes.io/cors-allow-headers: Authorization, origin, accept
    nginx.ingress.kubernetes.io/cors-allow-methods: GET, OPTIONS
    nginx.ingress.kubernetes.io/enable-cors: "true"

spec:
  rules:
  - http:
      paths:
       - backend:
          serviceName: jenkins-nodeport-svc
          servicePort: 80
         path: /

and you're good to go.

-- oldgiova
Source: StackOverflow