TransWikia.com

ASP.Net Core 2.2 Kubernetes Ingress: not found static content for custom path

Stack Overflow Asked by Kok How Teh on December 3, 2021

I have the following 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

After deploying my ASP.Net Core 2.2 API application and navigate to http://localhost/apistarter/, browser debugger console shows errors loading the static content and Javascripts. In addition, navigating to http://localhost/apistarter/swagger/index.html results in

Fetch error Not Found /swagger/v2/swagger.json

I am using the SAME ingress for multiple micro-services using different path prefix. It is running on my local kubernetes cluster using microk8s. Not on any cloud provider yet. I have checked out 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 folder and https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-2.1 but none of these helps.

One Answer

Follow these steps to run your code:

  1. ingress: remove URL-rewriting from 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" 
  labels: 
    app: ingress 
spec:
  rules:  
    - host: 
      http:   
        paths:
          - path: /apistarter # <---
            backend:
              serviceName: svc-aspnetapistarter
              servicePort: 5000
  1. deployment: pass environment variable with path base in 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: enable loading environment params in Program.cs
var builder = new WebHostBuilder()
    .UseContentRoot(Directory.GetCurrentDirectory())
    // ..
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
        // ..  
        config.AddEnvironmentVariables(); // <---
        // ..
    })
    // ..
  1. startup: apply UsePathBaseMiddleware in Startup.cs
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();
    }

    // ..
}

Answered by vladimir on December 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP