在 Azure Kubernetes 服务 (AKS) 上监视 C# 容器 运行
Monitoring a C# container running on Azure Kubernetes Service (AKS)
我在 AKS 上有一个 DotNet Core docker 容器 运行,它需要处理来自队列的消息并将遥测数据发送到 Application Insights 或 Log Analytics。我有一个运行良好的 ASP.NetCore 应用程序,但我无法从我的控制台应用程序中看到任何日志。
这就是我启动应用程序的方式:
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddEnvironmentVariables();
})
.ConfigureLogging((hostBuilderContext, loggingBuilder) =>
{
loggingBuilder.AddConsole(consoleLoggerOptions => consoleLoggerOptions.TimestampFormat = "[HH:mm:ss]");
})
.ConfigureServices(services =>
{
services.AddHostedService<RunsQueueProcessor>();
services.AddApplicationInsightsTelemetryWorkerService();
services.AddApplicationInsightsKubernetesEnricher();
});
这是我的applicationsettings.json
{
"Logging": {
"IncludeScopes": true,
"LogLevel": {
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
}
},
"ApplicationInsights": {
"InstrumentationKey": ".....",
"LogLevel": {
"Default": "Information"
}
},
"ServiceBus": {
"ConnectionString": "..."
},
"ConnectionStrings": {
"DatabaseContext": "...",
}
向这些类型的应用程序添加监控的正确方法是什么?
[编辑]
这是我的部署 yaml 文件。我是 运行 它来自 Azure DevOps 管道
apiVersion: apps/v1
kind: Deployment
metadata:
name: <project>-deployment
spec:
replicas: 1
selector:
matchLabels:
run: <project>-worker
template:
metadata:
labels:
run: <project>-worker
spec:
containers:
- name: <container>
image: <acr>.azurecr.io/<container>:{tag}
这是我的部署任务。我正在执行 -f 并传递上一个文件
- task: Kubernetes@1
inputs:
connectionType: 'Azure Resource Manager'
azureSubscriptionEndpoint: '<>'
azureResourceGroup: '<azure-rg>'
kubernetesCluster: '<aks-cluster>'
useClusterAdmin: true
command: 'apply'
arguments: '-f src/<...>/deployment.yaml'
secretType: 'dockerRegistry'
containerRegistryType: 'Azure Container Registry'
要查看信息级别消息,您应该更改 applicationsettings.json 中的设置:
将 ApplicationInsights
部分放入 Logging
部分。如下图:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
}
}
},
//other settings.
}
我在 AKS 上有一个 DotNet Core docker 容器 运行,它需要处理来自队列的消息并将遥测数据发送到 Application Insights 或 Log Analytics。我有一个运行良好的 ASP.NetCore 应用程序,但我无法从我的控制台应用程序中看到任何日志。 这就是我启动应用程序的方式:
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddEnvironmentVariables();
})
.ConfigureLogging((hostBuilderContext, loggingBuilder) =>
{
loggingBuilder.AddConsole(consoleLoggerOptions => consoleLoggerOptions.TimestampFormat = "[HH:mm:ss]");
})
.ConfigureServices(services =>
{
services.AddHostedService<RunsQueueProcessor>();
services.AddApplicationInsightsTelemetryWorkerService();
services.AddApplicationInsightsKubernetesEnricher();
});
这是我的applicationsettings.json
{
"Logging": {
"IncludeScopes": true,
"LogLevel": {
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
}
},
"ApplicationInsights": {
"InstrumentationKey": ".....",
"LogLevel": {
"Default": "Information"
}
},
"ServiceBus": {
"ConnectionString": "..."
},
"ConnectionStrings": {
"DatabaseContext": "...",
}
向这些类型的应用程序添加监控的正确方法是什么?
[编辑]
这是我的部署 yaml 文件。我是 运行 它来自 Azure DevOps 管道
apiVersion: apps/v1
kind: Deployment
metadata:
name: <project>-deployment
spec:
replicas: 1
selector:
matchLabels:
run: <project>-worker
template:
metadata:
labels:
run: <project>-worker
spec:
containers:
- name: <container>
image: <acr>.azurecr.io/<container>:{tag}
这是我的部署任务。我正在执行 -f 并传递上一个文件
- task: Kubernetes@1
inputs:
connectionType: 'Azure Resource Manager'
azureSubscriptionEndpoint: '<>'
azureResourceGroup: '<azure-rg>'
kubernetesCluster: '<aks-cluster>'
useClusterAdmin: true
command: 'apply'
arguments: '-f src/<...>/deployment.yaml'
secretType: 'dockerRegistry'
containerRegistryType: 'Azure Container Registry'
要查看信息级别消息,您应该更改 applicationsettings.json 中的设置:
将 ApplicationInsights
部分放入 Logging
部分。如下图:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
}
}
},
//other settings.
}