如何使用Python获取Azure容器应用作业的执行控制台日志
无延迟获取Azure容器应用作业控制台日志的有效方法求助
我通过Python SDK部署ContainerJob,完成begin_create_or_update操作后启动作业,但无法找到无延迟获取已执行作业控制台日志的简便方法,目前遇到以下问题:
AppContainersManagementClient不适用于当前场景,因为不存在容器实例组;- 执行AZ CLI命令
az containerapp job logs show -n my-job -g my-ressource-groupe --container my-job时,提示No replicas found for execution; - 仅能通过
LogsQueryClient获取日志,但存在5-10分钟的延迟。
部署及启动作业代码
from azure.identity import DefaultAzureCredential from azure.mgmt.appcontainers import ContainerAppsAPIClient client = ContainerAppsAPIClient(credential=DefaultAzureCredential(), subscription_id="xxxxxxxxxxxxxxxx") job_parameters = { "location": "West Europe", "properties": { "environmentId": "/subscriptions/xxxxx/resourceGroups/xxxxxxxx/providers/Microsoft.App/managedEnvironments/xxxxxxxx", "workloadProfileName": "Consumption", "configuration": { "secrets": [ { "name": "xxxx", "value": "xxxx" } ], "triggerType": "Manual", "replicaTimeout": 1800, "replicaRetryLimit": 0, "manualTriggerConfig": { "replicaCompletionCount": 1, "parallelism": 1 }, "registries": [ { "server": "artifacts.xxx.com", "username": "xxxxx", "passwordSecretRef": "xxxxx", }, ] }, "template": { "containers": [ { "image": "artifacts.xxx.com/my_image", "name": "job_name", "env": [ { "name": "MY_VAR_ENV", "value": "toto" } ], "resources": { "cpu": 0.25, "memory": "0.5Gi" }, "command": [ "echo", "'Hello World'" ] } ] } } } response = client.jobs.begin_create_or_update(rg, job_name, job_parameters).result() job_execution_template = { "containers" : [ { "image": "artifacts.xxx.com/my_image", "name": "other_name", "env": [ { "name": "CONTAINER_APP_JOB_NAME", "value": val } ], "resources": { "cpu": 0.5, "memory": "1Gi", }, "command": [ "/bin/bash", "-c", f"echo {val}; env" ] } ] } response = client.jobs.begin_start("my-ressource-group", "job_name", job_execution_template).result()
使用LogsQueryClient获取日志的代码
from azure.identity import DefaultAzureCredential from azure.monitor.query import LogsQueryClient from datetime import timedelta # Define your resource group and subscription ID rg = "my-ressource-group" subscription_id = "xxxxxxxxxx" workspace_id = "xxxxxxxxx" # Initialize the client credential = DefaultAzureCredential() client = LogsQueryClient(credential) # Define the query query = """ ContainerAppConsoleLogs | where ContainerGroupName startswith 'my-job' """ # Query the logs response = client.query_workspace( workspace_id=workspace_id, query=query, timespan=timedelta(days=1) ) # Print the logs for table in response.tables: for row in table.rows: print(row[10]) # Log column
现寻求无延迟获取Azure容器应用作业执行控制台日志的有效方法。
内容的提问来源于stack exchange,提问作者BeGreen
相关产品推荐
相关产品推荐

