牧场上的Kubernetes - Ingress Path Issue

时间:2018-02-01 16:20:51

标签: kubernetes rancher

我创建了一个带有入口控制器的nod​​ejs / express应用程序,该控制器在带有Rancher的Kubernetes上运行。只有默认后端才能完美运行,我可以访问我使用express创建的任何路由,例如http://1.2.3.4/apihttp://1.2.3.4/api/districts

不起作用的是我定义的路径,例如http://1.2.3.4/gg1/apihttp://1.2.3.4/gg2/api/districts。在Pod日志中,每当我在我定义的一条路径上发出请求时,我都可以看到404。

我一直在寻找解决方案的时间,但没有成功。我看到很多人似乎因为不同的原因而遇到入口和路径问题,但我还没找到解决问题的方法。也许这里有人知道这个问题的解决方案吗?

为了让进入工作,我在这里使用了这个例子:Using Kubernetes Ingress Controller from scratch

这是我部署所有内容的方式:

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

所有请求仅到达默认后端的pod,而/ gg1或/ gg2的路径仅在pod日志记录中提供404或在浏览器中不能提供GET / gg1。

入口-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

部署-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

部署-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

2 个答案:

答案 0 :(得分:0)

正确的路径与节点js资源端点相同!查看入口Kubernetes文档..

“在负载均衡器将流量定向到后端之前,主机和路径都必须与传入请求的内容匹配。”

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

答案 1 :(得分:0)

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

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