Kubernetes的“ nginx入口”无法找到服务

时间:2019-02-22 20:35:05

标签: kubernetes nginx-ingress

我已经完成了一个示例应用程序,可以在Kubernetes上部署Jenkins并使用Ingress公开。当我通过NodePort访问jenkins pod时,它正在工作,但是当我尝试通过Ingress / Nginx设置访问它时,我得到404

我在Google周围搜索过,并尝试了一些解决方法,但到目前为止都没有成功。这是文件的详细信息

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose -f ../docker-compose.yml -f ../docker-compose.utils.yml -f
      ../docker-compose.demosite.yml convert
    kompose.version: 1.17.0 (a74acad)
  creationTimestamp: null
  labels:
    io.kompose.service: ci
  name: ci
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: ci
    spec:
      containers:
      - image: jenkins/jenkins:lts
        name: almsmart-ci
        ports:
        - containerPort: 8080
        env:
          - name: JENKINS_USER
            value: admin
          - name: JENKINS_PASS
            value: admin
          - name: JAVA_OPTS
            value: -Djenkins.install.runSetupWizard=false
          - name: JENKINS_OPTS
            value: --prefix=/ci
        imagePullPolicy: Always
        resources: {}
      restartPolicy: Always
status: {}
---

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose -f ../docker-compose.yml -f ../docker-compose.utils.yml -f
      ../docker-compose.demosite.yml convert
    kompose.version: 1.17.0 (a74acad)
  creationTimestamp: null
  labels:
    io.kompose.service: ci
  name: ci
spec:
  type : NodePort
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    io.kompose.service: ci
status:
  loadBalancer: {}

这是我的入口定义

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: demo-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
    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"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
   - http:
      paths:
      - path: /ci
        backend:
          serviceName: ci
          servicePort: 8080

当我在Nginx控制器中检查日志时,看到以下内容

I0222 19:59:45.826062       6 controller.go:172] Configuration changes detected, backend reload required.
I0222 19:59:45.831627       6 event.go:221] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"almsmart-ingress", UID:"444858e5-36d9-11e9-9e29-080027811fa3", APIVersion:"extensions/v1beta1", ResourceVersion:"198832", FieldPath:""}): type: 'Normal' reason: 'DELETE' Ingress default/almsmart-ingress
I0222 19:59:46.063220       6 controller.go:190] Backend successfully reloaded.
[22/Feb/2019:19:59:46 +0000]TCP200000.000
W0222 20:00:00.870990       6 endpoints.go:76] Error obtaining Endpoints for Service "default/ci": no object matching key "default/ci" in local store
W0222 20:00:00.871023       6 controller.go:842] Service "default/ci" does not have any active Endpoint.
I0222 20:00:00.871103       6 controller.go:172] Configuration changes detected, backend reload required.
I0222 20:00:00.872556       6 event.go:221] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"almsmart-ingress", UID:"6fc5272c-36dc-11e9-9e29-080027811fa3", APIVersion:"extensions/v1beta1", ResourceVersion:"198872", FieldPath:""}): type: 'Normal' reason: 'CREATE' Ingress default/almsmart-ingress
I0222 20:00:01.060291       6 controller.go:190] Backend successfully reloaded.
[22/Feb/2019:20:00:01 +0000]TCP200000.000
W0222 20:00:04.205398       6 controller.go:842] Service "default/ci" does not have any active Endpoint.
[22/Feb/2019:20:00:09 +0000]TCP200000.000
10.244.0.0 - [10.244.0.0] - - [22/Feb/2019:20:00:36 +0000] "GET /ci/ HTTP/1.1" 404 274 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0" 498 0.101 [default-ci-8080] 10.244.1.97:8080 315 0.104 404 b5b849647749e2b626f00c011c15bc4e
10.244.0.0 - [10.244.0.0] - - [22/Feb/2019:20:00:46 +0000] "GET /ci HTTP/1.1" 404 274 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0" 497 0.003 [default-ci-8080] 10.244.1.97:8080 315 0.004 404 ac8fbe2faa37413f5e533ed3c8d98a7d
10.244.0.0 - [10.244.0.0] - - [22/Feb/2019:20:00:49 +0000] "GET /ci/ HTTP/1.1" 404 274 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0" 498 0.003 [default-ci-8080] 10.244.1.97:8080 315 0.004 404 865cb82af7f570f2144ef27fdea850c9
I0222 20:00:54.871828       6 status.go:388] updating Ingress default/almsmart-ingress status from [] to [{ }]
I0222 20:00:54.877693       6 event.go:221] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"almsmart-ingress", UID:"6fc5272c-36dc-11e9-9e29-080027811fa3", APIVersion:"extensions/v1beta1", ResourceVersion:"198974", FieldPath:""}): type: 'Normal' reason: 'UPDATE' Ingress default/almsmart-ingress

当我尝试

  

kubectl获取端点,我得到以下内容

NAME                            ENDPOINTS                        AGE
ci                              10.244.1.97:8080                 31m

默认的404页面可用,因此我认为Ingress Controller可以正常运行,但不确定为什么找不到该服务。我只有默认命名空间中的所有对象都可以工作,但是仍然无法使用nginx ingress访问它。

1 个答案:

答案 0 :(得分:2)

+1是这个很好问的问题。

您的设置对我来说似乎还可以。一个问题是,您为Jenkins配置了--prefix=/ci,但为入口配置了nginx.ingress.kubernetes.io/rewrite-target: /。这将导致重写您的路线:xxx/ci => xxx/。我认为404是您的詹金斯归还的。

您可以尝试将您的重写规则修改为nginx.ingress.kubernetes.io/rewrite-target: /ci,然后查看是否适用。