首页 > 解决方案 > 使用托管在 linux docker 容器中并部署到 Kubernetes 的 Asp.net 核心提供静态内容

问题描述

我正在试验托管在 AKS 实例中的 Asp.net api。到目前为止,一切都进展顺利。我已经设置了入口,并且我的 api 正在响应请求。但是现在我需要从 api 临时提供一个静态 html 文件,以便我的主机名可以通过负载测试服务进行验证。当我在本地运行(通过 IIS Express 或我的本地 Docker 实例)时,html 加载得很好。但它根本不会从我的 AKS k8s 实例中加载。我不知道从这里去哪里。

这是我的应用配置...</p>

        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();
            });
        }

这是我的程序启动...

    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>();
            }
    }

正如您在上面看到的,我试图强制 Kestrel 使用我的 Web 内容文件夹 (wwroot)。我认为这可能会解决问题,但事实并非如此。

最后,这是我的入口 yaml(从我的 k8s 实例中提取)...

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:
    - {}

最后,这是来自 pod 日志的示例,显示心跳(asp.net 路由)成功,静态内容请求以 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

我完全不知所措,所以我希望某个善良的灵魂能够帮助我。

提前致谢!

标签: dockerasp.net-corekuberneteskubernetes-helmazure-aks

解决方案


推荐阅读