nginx ingress Jenkins路径重写配置不工作

8qgya5xd  于 2023-10-17  发布在  Nginx
关注(0)|答案(3)|浏览(151)

我已经在Kubernetes上部署了Jenkins,并且正在尝试为它配置nginx入口。
假设我希望它在https://myip/jenkins上可用
这是我的初始入口配置:

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

当我访问https://myip/jenkins时,我被重定向到http://myip/login?from=%2F
当访问https://myip/jenkins/login?from=%2F时,它停留在该页面上,但没有找到任何静态资源,因为它们是在https://myip/static上查找的。

von4xj4u

von4xj4u1#

这就是我如何解决它配置Jenkins图像上下文路径,而不需要使用ingress重写注解:

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: {}

入口:

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
uubf1zoe

uubf1zoe2#

我使用的是JenkinsCI Helm图表(jenkinsci/jenkins),@codependent的回答让我走上了正确的道路。
基本uri可以在helm安装或helm升级期间设置:

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

或者,如果你已经安装了Jenkins:

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

6tr1vspr3#

上面的解决方案不会直接工作,因为Jenkins控制器pod将在heathchecks上失败(如果有的话)。检查这个部署.yaml下面与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如下“

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

通过应用上述更改,您将能够在http://example.com/jenkins上访问您的Jenkins

相关问题