Serving static content using Asp.net core hosted in a linux docker container and deployed to Kubernetes

10/14/2019

I'm experimenting with Asp.net api's hosted in an AKS instance. Up until now everything has been going great. I have ingress set up and my api is responding to requests. But now I need to temporarily serve a static html file from the api so my host name can be verified by a load testing service. The html loads just fine when I run locally (either via IIS Express or my local Docker instance). But it simply will not load from my AKS k8s instance. I don’t know where to go from here.

Here is my app configuration…

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseRouting();
            app.UseStaticFiles();
            app.AddMonitorEndpoint("/heartbeat");
            app.AddPathReturnEndpoint();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

Here is my program startup...

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args )
            {
            var root = Path.Combine(Environment.CurrentDirectory, "wwwroot");
            Console.WriteLine($"New Web Root path: {root}");
            return WebHost.CreateDefaultBuilder(args)
                .UseWebRoot(root)
                .UseStartup<Startup>();
            }
    }

As you can see above I'm trying to force Kestrel to use my web content folder (wwroot). I thought this might fix things but it didn't.

Finally, here is my ingress yaml (extracted from my k8s instance)...

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
  creationTimestamp: "2019-10-11T21:58:57Z"
  generation: 6
  labels:
    app.kubernetes.io/instance: falcon-infrastructure
    app.kubernetes.io/managed-by: Tiller
    app.kubernetes.io/name: loader-api
    app.kubernetes.io/version: "1.0"
    helm.sh/chart: loader-api-1.0.0
  name: loader-api
  namespace: falcon
  resourceVersion: "1479507"
  selfLink: /apis/extensions/v1beta1/namespaces/falcon/ingresses/loader-api
  uid: 52eb3832-ec72-11e9-9dc5-de03a35245a0
spec:
  rules:
  - host: localhost
    http:
      paths:
      - backend:
          serviceName: loader-api
          servicePort: http
  - host: << host ommitted >>
    http:
      paths:
      - backend:
          serviceName: loader-api
          servicePort: http
status:
  loadBalancer:
    ingress:
    - {}

Finally Finally, here is a sample from the pod logs showing the heartbeat (an asp.net route) succeeding and the static content request responding with 404.

?[40m?[32minfo?[39m?[22m?[49m: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 GET http://<< host ommitted >>/heartbeat application/json 2
?[40m?[32minfo?[39m?[22m?[49m: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 1699.8789000000002ms 200 application.json
?[40m?[32minfo?[39m?[22m?[49m: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 GET http://<< host ommitted >>/loaderio-c45f11b35aa6a575339d918cf887cc52.html text/html 2
?[40m?[32minfo?[39m?[22m?[49m: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 36.2461ms 404

I'm at a complete loss so I hope some kind soul will be able to help me.

Thanks in advance!

-- yozepi
asp.net-core
azure-kubernetes
docker
kubernetes
kubernetes-helm

0 Answers