入口控制器Nginx Kubernetes无法正常工作

时间:2019-10-16 16:36:32

标签: kubernetes

我在minikube中部署的入口控制器nginx kubernetes中遇到问题: 当我包含nginx将用于重定向请求的路由时,它不起作用,但是当我删除该路由时,它起作用: 因此,正如yaml文件中所解释的,当我使用/ category时,它不起作用,但是与just /一起起作用

---- load balancer ---
    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: spare-ingress-dev
      namespace: default
      annotations:
        kubernetes.io/ingress.class: nginx
        nginx.ingress.kubernetes.io/rewrite-target: /
    spec:
      rules:
         - http:
            paths:
              - path: /
                backend:
                  serviceName: cluster-test-ip
                  servicePort: 80
              - path: /category/
                backend:
                  serviceName: cluster-category-ip
                  servicePort: 5200

----service-----

    apiVersion: v1
    kind: Service
    metadata:
      name: cluster-category-ip
    spec:
      selector:
        app: category
      ports:
        - port: 5200
          targetPort: 5200

2 个答案:

答案 0 :(得分:3)

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: spare-ingress-dev
  namespace: default
  annotations:
     kubernetes.io/ingress.class: nginx
     nginx.ingress.kubernetes.io/use-regex: "true"
     nginx.ingress.kubernetes.io/app-root: /
spec:
 rules:
  - host: cluster.io
    http:
       paths:
        - path: /api/v1.0/auth/(.+)
          backend:
            serviceName: cluster-auth-ip
            servicePort: 6000
        - path: /api/v1.0/category/(.+)
          backend:
            serviceName: cluster-category-ip
            servicePort: 5200
        - path: /category/(.+)
          backend:
            serviceName: cluster-category-ip
            servicePort: 5200

我找到了解决方案 我删除了nginx.ingress.kubernetes.io/rewrite-target: $1 并且我添加了

kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/app-root: /

答案 1 :(得分:0)

你能试试吗-

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: spare-ingress-dev
  namespace: default
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: $1
spec:
  rules:
     - http:
        paths:
          - path: /(.*)
            backend:
              serviceName: cluster-test-ip
              servicePort: 80
          - path: /category/(.*)
            backend:
              serviceName: cluster-category-ip
              servicePort: 5200