Kubernetes Ingress path rewrites

1N5*_*818 7 url-rewriting kubernetes kubernetes-ingress

I am running Ingress for some services on Kubernetes, and although services are re-directing to those cluster IPs (somewhat correctly), I am not sure how to correctly resolve paths after just those basic paths, for example, if I have Tomcat in path / and Nginx on path /nginx, any Tomcat path after / does not resolve and neither does any Nginx path resolve for even /. I already have nginx.ingress.kubernetes.io/rewrite-target: / set in my ingress.yaml:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: tomcat-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
  namespace: kube-system
spec:
  rules:
  - http:
      paths:
      - path: /
        backend:
          serviceName: tomcat-deployment-service 
          servicePort: 8080
      - path: /nginx
        backend:
          serviceName: nginx-deployment-service 
          servicePort: 80
Run Code Online (Sandbox Code Playgroud)

If Tomcat requires a /main.css file, for example, the browser would try to fetch /main.css, but this returns the default-backend.

Also, if I try to visit /nginx (attempting to hit /) on the Nginx server, Nginx says /nginx is not found (obviously).

2019/03/02 08:12:04 [error] 8#8: *343 open() "/usr/share/nginx/html/nginx" failed (2: No such file or directory), client: 10.128.0.7, server: localhost, request: "GET /nginx HTTP/1.1", host: "REDACTED_SERVER_IP"
10.128.0.7 - - [02/Mar/2019:08:12:04 +0000] "GET /nginx HTTP/1.1" 404 153 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:65.0) Gecko/20100101 Firefox/65.0" "REDACTED_CLIENT_IP, REDACTED_SERVER_IP"
10.40.1.1 - - [02/Mar/2019:08:12:05 +0000] "GET /nginx HTTP/1.1" 404 153 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:65.0) Gecko/20100101 Firefox/65.0" "REDACTED_CLIENT_IP, REDACTED_SERVER_IP"
2019/03/02 08:12:05 [error] 8#8: *344 open() "/usr/share/nginx/html/nginx" failed (2: No such file or directory), client: 10.40.1.1, server: localhost, request: "GET /nginx HTTP/1.1", host: "REDACTED_SERVER_IP"
Run Code Online (Sandbox Code Playgroud)

How can I make paths after a specified path in ingress.yaml resolve considering the beginning part of the path as viewed from the service?

i.e.

  1. If Tomcat was ingress pathed to /tomcat, how could I make /tomcat/main.css appear as /main.css to the Tomcat server (and not resolve to default backed)?
  2. If Nginx was ingress pathed to /nginx, how could I make /nginx appear as / to the Nginx server (and not appear as /nginx)?

I thought this was what /rewrite-target was suppose to do.

Would I need to use wildcards or something?

My ingress is the following:

Name:             tomcat-ingress
Namespace:        kube-system
Address:          REDACTED_SERVER_IP
Default backend:  default-http-backend:80 (10.40.0.6:8080)
Rules:
  Host  Path  Backends
  ----  ----  --------
  *         
        /        tomcat-deployment-service:8080 (10.40.2.15:8080)
        /nginx   nginx-dep-ser:80 (10.40.0.26:80,10.40.1.46:80)
Annotations:
  ingress.kubernetes.io/url-map:                     k8s-um-kube-system-tomcat-ingress--b0fc8aa23db1001d
  kubectl.kubernetes.io/last-applied-configuration:  {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"nginx.ingress.kubernetes.io/rewrite-target":"/"},"name":"tomcat-ingress","namespace":"kube-system"},"spec":{"rules":[{"http":{"paths":[{"backend":{"serviceName":"tomcat-deployment-service","servicePort":8080},"path":"/"},{"backend":{"serviceName":"nginx-dep-ser","servicePort":80},"path":"/nginx"}]}}]}}

  nginx.ingress.kubernetes.io/rewrite-target:  /
  ingress.kubernetes.io/backends:              {"k8s-be-30985--b0fc8aa23db1001d":"HEALTHY","k8s-be-31229--b0fc8aa23db1001d":"HEALTHY","k8s-be-32736--b0fc8aa23db1001d":"HEALTHY"}
  ingress.kubernetes.io/forwarding-rule:       k8s-fw-kube-system-tomcat-ingress--b0fc8aa23db1001d
  ingress.kubernetes.io/target-proxy:          k8s-tp-kube-system-tomcat-ingress--b0fc8aa23db1001d
Events:
  Type    Reason  Age                From                      Message
  ----    ------  ----               ----                      -------
  Normal  CREATE  33m                nginx-ingress-controller  Ingress kube-system/tomcat-ingress
  Normal  ADD     33m                loadbalancer-controller   kube-system/tomcat-ingress
  Normal  UPDATE  3m (x65 over 33m)  nginx-ingress-controller  Ingress kube-system/tomcat-ingress
  Normal  CREATE  3m (x31 over 33m)  loadbalancer-controller   ip: REDACTED_SERVER_IP
Run Code Online (Sandbox Code Playgroud)

VAS*_*VAS 6

我假设您拥有最新版本的入口控制器,并且根据文档

从版本 0.22.0 开始,使用注释的入口定义nginx.ingress.kubernetes.io/rewrite-target不向后兼容以前的版本。在版本 0.22.0 及更高版本中,请求 URI 中需要传递到重写路径的任何子字符串都必须在捕获组中明确定义。

例如:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
  name: tomcat-ingress
  namespace: default 
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: tomcat-deployment-service
          servicePort: 8080
        path: /tomcat/?(.*)
      - backend:
          serviceName: nginx-deployment-service
          servicePort: 80
        path: /nginx/?(.*)
Run Code Online (Sandbox Code Playgroud)

您当前的配置仅适用于最新版本的入口控制器//nginx目的地。

另一个重要的事情是,Ingress 对象应该与它所引用的服务在同一个命名空间中创建。否则 ingress-nginx 通常会返回503 Service Temporarily Unavailable错误。

并且不允许引用不同命名空间中的服务serviceName

DNS-1035 标签必须由小写字母数字字符或“-”组成,以字母字符开头,以字母数字字符结尾(例如“my-name”或“abc-123”,用于验证的正则表达式为“ az?')