ASP.Net Core 2.2 Kubernetes Ingress:未找到自定义路径的静态内容

Kok*_*Teh 4 c# kubernetes asp.net-core kubernetes-ingress nginx-ingress

我有以下 ingress.yml:

apiVersion: extensions/v1beta1
kind: Ingress 
metadata:
  name: ingress 
  namespace: default 
  annotations:
    kubernetes.io/ingress.class: "nginx" 
    nginx.ingress.kubernetes.io/ssl-redirect: "false" 
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  labels: 
    app: ingress 
spec:
  rules:  
    - host: 
      http:   
        paths:
          - path: /apistarter(/|$)(.*)
            backend:
              serviceName: svc-aspnetapistarter
              servicePort: 5000
          - path: //apistarter(/|$)(.*)
            backend:
              serviceName: svc-aspnetapistarter
              servicePort: 5000
Run Code Online (Sandbox Code Playgroud)

部署我的 ASP.Net Core 2.2 API 应用程序并导航到 后http://localhost/apistarter/,浏览器调试器控制台显示加载静态内容和 Javascript 的错误。此外,导航到http://localhost/apistarter/swagger/index.html结果

Fetch error Not Found /swagger/v2/swagger.json
Run Code Online (Sandbox Code Playgroud)

我对使用不同路径前缀的多个微服务使用相同的入口。它使用 microk8s 在我的本地 kubernetes 集群上运行。还没有在任何云提供商上。我已经查看了如何配置 ASP.NET Core 多微服务应用程序和 Azure AKS 入口路由,以便它不会破坏 wwwroot 文件夹https://docs.microsoft.com/en-us/aspnet/core 中的资源/host-and-deploy/proxy-load-balancer?view=aspnetcore-2.1但这些都没有帮助。

vla*_*mir 16

请按照以下步骤运行您的代码:

  1. ingress : 从ingress.yml 中删除 URL 重写
apiVersion: extensions/v1beta1
kind: Ingress 
metadata:
  name: ingress 
  namespace: default 
  annotations:
    kubernetes.io/ingress.class: "nginx" 
    nginx.ingress.kubernetes.io/ssl-redirect: "false" 
  labels: 
    app: ingress 
spec:
  rules:  
    - host: 
      http:   
        paths:
          - path: /apistarter # <---
            backend:
              serviceName: svc-aspnetapistarter
              servicePort: 5000
Run Code Online (Sandbox Code Playgroud)
  1. 部署:在ingress.yml 中传递带有路径基础的环境变量
apiVersion: apps/v1
kind: Deployment
# ..
spec:
  # ..
  template:
    # ..
    spec:
      # ..
      containers:
        - name: test01
          image: test.io/test:dev
          # ...
          env:
            # define custom Path Base (it should be the same as 'path' in Ingress-service)
            - name: API_PATH_BASE # <---
              value: "apistarter"
Run Code Online (Sandbox Code Playgroud)
  1. program : 在Program.cs 中启用加载环境参数
var builder = new WebHostBuilder()
    .UseContentRoot(Directory.GetCurrentDirectory())
    // ..
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
        // ..  
        config.AddEnvironmentVariables(); // <---
        // ..
    })
    // ..
Run Code Online (Sandbox Code Playgroud)
  1. 启动:在Startup.cs 中应用UsePathBaseMiddleware
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    private readonly IConfiguration _configuration;

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        var pathBase = _configuration["API_PATH_BASE"]; // <---

        if (!string.IsNullOrWhiteSpace(pathBase))
        {
            app.UsePathBase($"/{pathBase.TrimStart('/')}");
        }

        app.UseStaticFiles(); // <-- StaticFilesMiddleware must follow UsePathBaseMiddleware

        // ..

        app.UseMvc();
    }

    // ..
}
Run Code Online (Sandbox Code Playgroud)