Kubernetes Nginx-ingress如何在Express中处理路由 [英] Kubernetes nginx-ingress how to deal with routing in Express

查看:197
本文介绍了Kubernetes Nginx-ingress如何在Express中处理路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Azure Kubernetes群集,在其上运行几个NodesJS Express应用程序. 我通过nginx-ingress处理传入的请求

I have an Azure Kubernetes Cluster on which a couple of NodesJS Express Applications runs. I handle the incoming request by nginx-ingress

控制器: https://raw.githubusercontent .com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml

服务: https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/cloud-generic.yaml

入口:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    #nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
  - http:
      paths:
      - path: /app1(/|$)(.*)
        backend:
          serviceName: app1-service
          servicePort: 80
      - path: /app2(/|$)(.*)
        backend:
          serviceName: app2-service
          servicePort: 80

示例app1-service

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: app1
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: app1
    spec:
      nodeSelector:
        "beta.kubernetes.io/os": linux
      containers:
      - name: nodejs
        image: xxx.azurecr.io/app1:v1.0
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          protocol: TCP
---
kind: Service
apiVersion: v1
metadata:
  name: app1-service
spec:
  selector:
    app: app1
  ports:
    - port: 80
      protocol: TCP
  type: NodePort

就像您可以看到 kubernetesurl.com/app1 上的所有流量都被转发到app1-service一样.直到一切正常为止.

Like you can see all traffic on kubernetesurl.com/app1 gets forwarded to app1-service. And until there everything works.

Dockerfile: 从节点:8 WORKDIR/opt

Dockerfile: FROM node:8 WORKDIR /opt

COPY package.json /opt
RUN npm install

COPY . /opt

CMD ["npm", "start"]

server.js:

server.js:

app.get('/', function(request, response) {
    response.sendFile(path.join(__dirname + '/login.html'));
});
app.post('/auth', function(request, response) {
    ... some auth stuff
    response.redirect('/home');
});
app.get('/home', function(request, response) {
    response.sendFile(path.join(__dirname + '/home.html'));
})

login.html:

login.html:

<form action="auth" method="POST">
    <input type="text" name="username" placeholder="Username" required>
    <input type="password" name="password" placeholder="Password" required>
    <input type="submit">
</form>

问题

  1. 如果提交表单,我将被重定向到 kubernetesurl.com/auth .

但是我需要kubernetesurl.com/app1/auth.

But I need kubernetesurl.com/app1/auth.

如果我在'/'内使用 response.redirect('/home'),则会重定向到 kubernetesurl.com/home .

If I use a response.redirect('/home') inside '/', I get redirected to kubernetesurl.com/home.

但是我需要kubernetesurl.com/app1/home.

But I need kubernetesurl.com/app1/home.

如果我将带有相对URL的链接添加到home之类的index.html,则会重定向到 kubernetesurl.com/home .

If I add a link with relative url to index.html like home, I get redirected to kubernetesurl.com/home.

但是我需要kubernetesurl.com/app1/home.

But I need kubernetesurl.com/app1/home.

当我在我的VM上开始表示为服务时,一切正常(当然,因为该应用程序在'/'上运行而无需代理).

When I start express as a service on my VM, everything works (of course, because the app runs on '/' without proxying).

我如何通过代理运行它?

How I get it running with proxying?

推荐答案

由于您有两个不同的位置(指向两个不同的服务),因此可以尝试对这些URL进行分组和重写.

Since you have two different locations, pointed to two different services, you can try grouping and rewriting the relative URL.

annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /app$2/$3
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /(location)(1)/(.*)
        backend:
          serviceName: service1
          servicePort: 80
      - path: /(location)(2)/(.*)
        backend:
          serviceName: service2
          servicePort: 80

因此,基本上,您只使用第一个组(location1location2)来路由到Ingress级别的正确服务.

So basically you're only using the first group (location1 and location2) to route to the correct service at Ingress level.

然后,可以通过重写规则来消除该组,该规则将仅保留第二组来重写正确的应用程序编号,而第三组则将您需要传递给URL的所有其他内容进行野匹配.

Then, this group can be dismissed by the rewrite rule which will only keep the second group for rewriting the correct application number and the third to wild-match everything else that you need to pass in your URL.

这篇关于Kubernetes Nginx-ingress如何在Express中处理路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆