简体   繁体   中英

Kubernetes on Rancher - Ingress Path Issue

I have created a nodejs/express application with an ingress controller which is running on Kubernetes with Rancher. Only the default backend works perfectly and I can reach any of my routes I created with express, eg http://1.2.3.4/api or http://1.2.3.4/api/districts .

What is not working is the paths that I defined, eg http://1.2.3.4/gg1/api or http://1.2.3.4/gg2/api/districts . In the Pod logs I can see a 404 whenever I make a request on one of the paths that I defined.

I have been looking for hours for a solution now without success. I saw that a lot of people seem to have problems with ingress and paths for different reasons, but I couldn't find the solution for my problem yet. Does maybe someone here know the solution for this problem?

To get ingression to work I used this example here: Using Kubernetes Ingress Controller from scratch

These is how I deploy everything:

kubectl create -f deployment1-config.yaml
kubectl create -f deployment2-config.yaml

kubectl expose deployment test-ingress-node-1 --target-port=5000 --type=NodePort kubectl expose deployment test-ingress-node-2 --target-port=5000 --type=NodePort

kubectl run nginx --image=nginx --port=80 kubectl expose deployment nginx --target-port=80 --type=NodePort

kubectl create -f ingress.yaml

All requests only reach the pod of the default backend, and paths with /gg1 or /gg2 only give 404 in pod logging or Cannot GET /gg1 in browser.

ingress-config.yaml:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-node-adv-ingress
  annotations:
    kubernetes.io/ingress.class: "rancher"
    ingress.kubernetes.io/rewrite-target: /
spec:
  backend:
    serviceName: test-ingress-node-1
    servicePort: 5000
  rules:
  - host:
    http:
      paths:
      - path: /gg1
        backend:
          serviceName: test-ingress-node-1
          servicePort: 5000
      - path: /gg2
        backend:
          serviceName: test-ingress-node-2
          servicePort: 5000

deployment-1.config.yaml:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  generation: 2
  labels:
    run: test-ingress-node-1
  name: test-ingress-node-1
  namespace: default
  resourceVersion: "123456"
  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/test-ingress-node-1
spec:
  replicas: 1
  selector:
    matchLabels:
      run: test-ingress-node-1
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: test-ingress-node-1
    spec:
      containers:
      - image: myProject-service-latest
        imagePullPolicy: Always
        name: test-ingress-node-1
        ports:
        - containerPort: 5000
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  observedGeneration: 2
  replicas: 1
  updatedReplicas: 1

deployment-2.config.yaml:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  generation: 2
  labels:
    run: test-ingress-node-2
  name: test-ingress-node-2
  namespace: default
  resourceVersion: "123456"
  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/test-ingress-node-2
spec:
  replicas: 1
  selector:
    matchLabels:
      run: test-ingress-node-2
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: test-ingress-node-2
    spec:
      containers:
      - image: myProject-service-latest
        imagePullPolicy: Always
        name: test-ingress-node-2
        ports:
        - containerPort: 5000
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  observedGeneration: 2
  replicas: 1
  updatedReplicas: 1

The correct path is the same that node js resource endpoint! Review the ingress Kubernetes documentation..

"Both the host and path must match the content of an incoming request before the loadbalancer directs traffic to the backend."

https://kubernetes.io/docs/concepts/services-networking/ingress/

尝试用以下行替换ingress-config.yaml文件中annotations下的ingress.kubernetes.io/rewrite-target: /行:

nginx.ingress.kubernetes.io/rewrite-target: /

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM