gpt4 book ai didi

azure - 如何自动将 AKS 日志上传到 Azure 存储帐户

转载 作者:行者123 更新时间:2023-12-02 23:35:06 25 4
gpt4 key购买 nike

我有一项任务,将 AKS 日志(控制平面和工作负载)自动上传到 Azure 存储帐户,以便以后可以查看它们,或者在出现任何故障时可以向电子邮件/团队 channel 发出警报通知。如果使用日志分析工作区,这将是一项简单的任务,但是,为了节省成本,我们将其禁用。

我尝试使用下面的 cronjob 定期将 Pod 日志上传到存储帐户,但它引发了以下错误[1]

apiVersion: batch/v1
kind: CronJob
metadata:
name: log-uploader
spec:
schedule: "0 0 * * *" # Run every day at midnight
jobTemplate:
spec:
template:
spec:
containers:
- name: log-uploader
image: mcr.microsoft.com/azure-cli:latest
command:
- bash
- "-c"
- |
az aks install-cli
# Set environment variables for Azure Storage Account and Container
export AZURE_STORAGE_ACCOUNT=test-101
export AZURE_STORAGE_CONTAINER=logs-101
# Iterate over all pods in the cluster and upload their logs to Azure Blob Storage
for pod in $(kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.metadata.name} {.metadata.namespace}{"\n"}{end}'); do
namespace=$(echo $pod | awk '{print $2}')
pod_name=$(echo $pod | awk '{print $1}')
# Use the Kubernetes logs API to retrieve the logs for the pod
logs=$(kubectl logs -n $namespace $pod_name)
# Use the Azure CLI to upload the logs to Azure Blob Storage
echo $logs | az storage blob upload --file - --account-name $AZURE_STORAGE_ACCOUNT --container-name $AZURE_STORAGE_CONTAINER --name "$namespace/$pod_name_`date`.log"
done
restartPolicy: OnFailure

错误[1]

error: expected 'logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER]'.
POD or TYPE/NAME is a required argument for the logs command
See 'kubectl logs -h' for help and examples

The same commands are running fine outside the container.

任何想法/建议将不胜感激。

问候,

皮尤什

最佳答案

实现这一目标的更好方法是部署 fluentd daemonset在您的集群中并使用 azure storage plugin将日志上传到存储帐户。

此工具是为此特定目的而构建的,并且可能会为此目的更好地为您服务。

关于azure - 如何自动将 AKS 日志上传到 Azure 存储帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74937101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com