codependent
codependent

Reputation: 24462

nginx ingress Jenkins path rewrite configuration not working

I have deployed Jenkins on Kubernetes and am trying to configure the nginx ingress for it.

Assume I want it to be available at https://myip/jenkins

This is my initial ingress configuration:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: jenkins-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/add-base-url: "true"
spec:
  rules:
  - http:
      paths:
      - path: /jenkins
        backend:
          serviceName: jenkins
          servicePort: 8080

With this when I access https://myip/jenkins I am redirected to http://myip/login?from=%2F.

When accessing https://myip/jenkins/login?from=%2F it stays on that page but none of the static resources are found since they are looked for at https://myip/static...

Upvotes: 7

Views: 5311

Answers (3)

Aditya Kumar
Aditya Kumar

Reputation: 11

the above solution won't work directly as jenkins controller pod will fail on heathchecks if any. check this deployment.yaml below with ingress.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  namespace: devops-tools
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins-server
  template:
    metadata:
      labels:
        app: jenkins-server
    spec:
      securityContext:
            fsGroup: 1000 
            runAsUser: 1000
      serviceAccountName: jenkins-admin
      containers:
        - name: jenkins
          image: jenkins/jenkins:lts
          resources:
            limits:
              memory: "6Gi"
              cpu: "1000m"
            requests:
              memory: "500Mi"
              cpu: "500m"
          env:
            - name: JENKINS_OPTS
              value: "--prefix=/jenkins --httpListenAddress=0.0.0.0"
          ports:
            - name: httpport
              containerPort: 8080
            - name: jnlpport
              containerPort: 50000
          livenessProbe:
            httpGet:
              path: "/jenkins/login"
              port: 8080
            initialDelaySeconds: 90
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 5
          readinessProbe:
            httpGet:
              path: "/jenkins/login"
              port: 8080
            initialDelaySeconds: 60
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
          volumeMounts:
            - name: jenkins-data
              mountPath: /var/jenkins_home         
      volumes:
        - name: jenkins-data
          persistentVolumeClaim:
              claimName: jenkins-pv-claim

ingress.yaml is as follows "

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/add-base-url: "true"
  name: jenkins
  namespace: devops-tools
spec:
  ingressClassName: nginx
  rules:
  - host:  example.com
    http:
      paths:
      - pathType: Prefix
        path: /jenkins
        backend:
          service:
            name: jenkins-service
            port:
              number: 8080

with applying above changes you will be able to access your jenkins on http://example.com/jenkins

Upvotes: 1

Cameron Hudson
Cameron Hudson

Reputation: 3929

I'm using the JenkinsCI Helm chart (jenkinsci/jenkins), and @codependent's answer set me on the right track.

The base uri can be set during a helm install or helm upgrade:

helm install jenkins jenkinsci/jenkins --set controller.jenkinsUriPrefix='/jenkins'

or, if you've already installed Jenkins:

helm upgrade jenkins jenkinsci/jenkins --set controller.jenkinsUriPrefix='/jenkins'

Upvotes: 1

codependent
codependent

Reputation: 24462

This is how I solved it configuring the Jenkins image context path without the need to use the ingress rewrite annotations:

kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: jenkins
  name: jenkins
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: jenkins
    spec:
      securityContext:
        fsGroup: 2000
        runAsUser: 1000
        runAsNonRoot: true
      volumes:
      - name: jenkins-storage
        persistentVolumeClaim:
          claimName: jenkins
      containers:
      - image: jenkins/jenkins:lts
        name: jenkins
        ports:
        - containerPort: 8080
          name: "http-server"
        - containerPort: 50000
          name: "jnlp"
        resources: {}
        env:
        - name: JENKINS_OPTS
          value: --prefix=/jenkins
        volumeMounts:
        - mountPath: "/var/jenkins_home"
          name: jenkins-storage
status: {}

Ingress:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: prfl-apps-devops-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/add-base-url: "true"
spec:
  rules:
  - http:
      paths:
      - path: /jenkins
        backend:
          serviceName: jenkins
          servicePort: 8080

Upvotes: 7

Related Questions