简体   繁体   English

Kubernetes nginx 将流量路由到 /api/*

[英]Kubernetes nginx route traffic to /api/*

I have a kubernetes cluster with configured nginx to port traffic to my angular application.我有一个 kubernetes 集群,配置了 nginx 以将流量移植到我的 angular 应用程序。 It is working fine, however when I access the myipaddress/api/v1 - I want nginx to port the traffic to my express application which is listening on port 3000 and the angular application not to look the myipaddress/api/v1 as a route component in angular as it doesn't exist.它工作正常,但是当我访问myipaddress/api/v1 - 我希望 nginx 将流量移植到我正在侦听端口 3000 的 express 应用程序和 angular 应用程序不将myipaddress/api/v1视为路由组件在 angular 因为它不存在。

Here's my kubernetes nginx ingress for express这是我的 kubernetes nginx 入口,用于快递

apiVersion: v1
kind: Service
metadata:
  name: backend
spec:
  type: ClusterIP
  ports:
  - port: 3000
    targetPort: 3000
  selector:
    app: backend

---

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: backend-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /api/?(.*)
        pathType: Prefix
        backend:
          service:
            name: backend
            port:
              number: 3000

Here's my ingress for angular这是我对 angular 的入口

---
apiVersion: v1
kind: Service
metadata:
  name: webapp
spec:
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: webapp

--- 
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: webapp-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: webapp
            port:
              number: 80

What I am trying to achieve for angular我想为 angular 实现什么

myipaddres.com        -> serve angular application`
myipaddress.com/users -> serve angular router for /users

What I am trying to achieve for express:我想要实现的快递:

myipaddress.com/api/v1/users -> call the users v1 endpoint in express
myipaddress.com/api/v2/users -> call the users v2 endpoint in express

Ok I managed to find the solution myself, posting this in case someone needs it.好的,我自己设法找到了解决方案,张贴这个以防有人需要。 The newest api for kubernetes (up to date of posting this) supports regex but you should explicitly enable it with annotation.用于 kubernetes 的最新 api(最新发布)支持正则表达式,但您应该使用注释明确启用它。

---
# ingress traffic
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: backend-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true" # enabling regex annotation
spec:
  rules:
  - http:
      paths:
      - path: /api/*      # works fine with regex enabled
        pathType: Prefix
        backend:
          service:
            name: backend
            port:
              number: 3000

The same applies for any other ingress path you need to reverse proxy.这同样适用于您需要反向代理的任何其他入口路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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