入口、重写目标、前端和 API 相同的 URL。版本 0.22.0+

Dan*_*iel 2 yaml kubernetes kubernetes-ingress nginx-ingress

我正在尝试进行这样的设置:

  • example.com #前端
  • example.com/api
  • example.com/authentication

显然他们每个人都独立的应用程序并且应该能够继续自己的道路,ex. http://example.com/api/v1/test?v=ok

现在我有一个像这样的yaml:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
 name: test-ingress
 annotations:
   nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
 rules:
 - http:
     paths:
     - path: /
       backend:
         serviceName: frontend-service
         servicePort: 80
     - path: /api(/|$)(.*)
       backend:
         serviceName: backend-service
         servicePort: 80
     - path: /authentication(/|$)(.*)
       backend:
         serviceName: identityserver-service
         servicePort: 80
Run Code Online (Sandbox Code Playgroud)

/api 和 /authentication 的行为符合我想要的方式,但前端的子路径不起作用。所以例如 http://example.com/css/bootstrap.css找不到。

到目前为止我已经尝试过

1-(/|$)(.*)在前端路径的and处添加

2-添加具有相同支持和端口和路径的前端路径的副本/.*

他们都没有解决问题。

这是描述结果:

Name:             test-ingress
Namespace:        default
Address:          127.0.0.1
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host        Path  Backends
  ----        ----  --------
  *
              /                          frontend-service:80 (10.1.80.38:80,10.1.80.43:80,10.1.80.50:80)
              /api(/|$)(.*)              backend-service:80 (10.1.80.39:80,10.1.80.42:80,10.1.80.47:80)
              /authentication(/|$)(.*)   identityserver-service:80 (10.1.80.40:80,10.1.80.41:80,10.1.80.45:80)
Annotations:  nginx.ingress.kubernetes.io/rewrite-target: /$2
Events:
  Type    Reason  Age                 From                      Message
  ----    ------  ----                ----                      -------
  Normal  UPDATE  43s (x14 over 13h)  nginx-ingress-controller  Ingress default/test-ingress
Run Code Online (Sandbox Code Playgroud)

PS:我发现了一些似乎早于版本 0.22.0 并且不再工作的答案。 具有多个目标重写的 kubernetes 入口

aci*_*uji 6

您的情况的问题在于正则表达式不正确。如果启用了多行标志,则该$运算符匹配字符串的结尾或行的结尾。对于第一组,您正在捕获/$但您的字符串没有结束并且不匹配。

我测试了这个,它适用于这个正则表达式:

 paths:
 - path: /()(.*)
   backend:
     serviceName: frontend-service
     servicePort: 80
Run Code Online (Sandbox Code Playgroud)