You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Azure Application Insights关联到AKS?脚本执行遇证书错误求方案

.NET Core项目在AKS中集成Application Insights的替代方案

问题背景

App Service与Application Insights集成后可实现跨服务边界的请求追踪,但AKS默认无此功能。尝试使用实验指南中的init.sh脚本时,出现ERROR: After approving csr app-monitoring-webhook.kube-system, the signed certificate did not appear on the resource. Giving up after 10 attempts错误,即使将脚本中的API版本从v1beta1改为certificates.k8s.io/v1后仍报错。以下是针对.NET Core项目的替代集成方法:


1. 应用代码层面集成Application Insights SDK

  • 直接在.NET Core项目中安装Microsoft.ApplicationInsights.AspNetCore NuGet包
  • 在Program.cs中添加遥测配置:
    var builder = WebApplication.CreateBuilder(args);
    // 注入Application Insights遥测服务
    builder.Services.AddApplicationInsightsTelemetry(
        builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]
    );
    
  • 通过AKS的Secret或ConfigMap存储Application Insights连接字符串,在Deployment中注入为环境变量:
    spec:
      containers:
      - name: your-app
        env:
        - name: APPLICATIONINSIGHTS_CONNECTION_STRING
          valueFrom:
            secretKeyRef:
              name: ai-connection-secret
              key: connection-string
    
  • 此方式无需依赖集群级组件,兼容性强,适用于所有.NET Core版本,且能完整支持跨服务追踪。

2. 利用Azure Monitor Container Insights自动注入

  • 先确保AKS集群已启用Azure Monitor Container Insights
  • 通过Azure门户或CLI配置.NET Core应用的自动遥测采集:指定目标Application Insights资源ID后,系统会自动在Pod中注入代理,无需修改应用代码即可采集请求、依赖项等遥测数据,支持跨服务链路追踪。

3. 基于OpenTelemetry实现遥测采集与导出

  • 安装OpenTelemetry相关NuGet包:OpenTelemetry.Extensions.Hosting、OpenTelemetry.Instrumentation.AspNetCore、OpenTelemetry.Exporter.ApplicationInsights
  • 在Program.cs中配置OpenTelemetry并导出到Application Insights:
    using OpenTelemetry;
    using OpenTelemetry.Trace;
    
    var builder = WebApplication.CreateBuilder(args);
    builder.Services.AddOpenTelemetry()
        .WithTracing(tracerBuilder =>
        {
            tracerBuilder
                .AddAspNetCoreInstrumentation() // 采集ASP.NET Core请求数据
                .AddHttpClientInstrumentation() // 采集HttpClient调用数据
                .AddApplicationInsightsExporter(options =>
                {
                    options.ConnectionString = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
                });
        });
    
  • OpenTelemetry是CNCF标准,除了支持Application Insights,还可兼容其他遥测系统,适合需要多平台兼容的场景。

原CSR错误的排查建议(可选)

若仍想尝试原指南的webhook方案,可检查:

  • 当前用户是否有审批CSR的RBAC权限,手动执行kubectl certificate approve app-monitoring-webhook.kube-system后查看CSR状态(kubectl get csr)
  • 集群中是否有cert-manager等证书管理组件,且运行正常;或确认Kubernetes API Server的证书签名配置是否正确

内容的提问来源于stack exchange,提问作者Ayush Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 23:25:56