ASP.NET Core 2.2 Kubernetes Inress:未找到自定义路径的静态内容 [英] ASP.Net Core 2.2 Kubernetes Ingress: not found static content for custom path

查看:23
本文介绍了ASP.NET Core 2.2 Kubernetes Inress:未找到自定义路径的静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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

部署ASP.NET Core2.2API应用程序并导航到http://localhost/apistarter/后,浏览器调试器控制台在加载静态内容和Java脚本时显示错误。此外,导航到http://localhost/apistarter/swagger/index.html会导致

Fetch error Not Found /swagger/v2/swagger.json
对于使用不同路径前缀的多个微服务,我使用相同的入口。它运行在我本地的Kubernetes集群上,使用的是microk8。目前还没有出现在任何云提供商身上。我已签出How to configure an ASP.NET Core multi microservice application and Azure AKS ingress routes so that it doesn't break resources in the wwwroot folderhttps://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-2.1,但这些都没有帮助。

推荐答案

按照以下步骤运行代码:

  1. 入口:从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
  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"
  1. 程序:启用Program.cs中的环境参数加载
var builder = new WebHostBuilder()
    .UseContentRoot(Directory.GetCurrentDirectory())
    // ..
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
        // ..  
        config.AddEnvironmentVariables(); // <---
        // ..
    })
    // ..
  1. 启动:在Startup.cs中应用UsePath BaseMiddleware
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();
    }

    // ..
}

这篇关于ASP.NET Core 2.2 Kubernetes Inress:未找到自定义路径的静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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