简体   繁体   English

如何收集一个Kubernetes CronJob一段时间内每个作业的日志?

[英]How to collect logs of every job of a Kubernetes CronJob for a period of time?

Assume I have a Kubernetes CronJob假设我有一个 Kubernetes CronJob

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: cron-job-logging
spec:
  schedule: "@hourly"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: cron-job-logging
            image: python:3
            args:
            - /usr/bin/python3
            - -c
            - import random; print('a') if random.random() < 0.5 else print('b')
          restartPolicy: OnFailure

which runs on a GKE cluster 1.14.x with Cloud Operation for GKE activated for "System and workload logging and monitoring".它在 GKE 集群 1.14.x 上运行,并为“系统和工作负载日志记录和监控”激活了 Cloud Operation for GKE。

How can I collect the output for period t (let's say a month) so that I can see whether the pod printed a or b .如何在 t 期间(假设一个月)收集 output 以便我可以查看 pod 打印a还是b

If seen some issues about this request, like https://github.com/kubernetes/kubernetes/issues/27768 .如果看到有关此请求的一些问题,例如https://github.com/kubernetes/kubernetes/issues/27768 The logs seem to be available for some users, but not for others which might be caused by the fact that CronJobs are a beta feature.日志似乎对某些用户可用,但对其他用户不可用,这可能是由于CronJobs是一个 beta 功能。

I've deployed your cronjob, and just for example purposes I set schedule to run each 1 minute, Below there are a few ways on how to access it:我已经部署了您的 cronjob,仅出于示例目的,我将计划设置为每 1 分钟运行一次,下面有几种方法可以访问它:

  • GKE console – In the Google Kubernetes Engine section of Google Cloud Console, select the Kubernetes resources listed in Workloads , and then the Container or Audit Logs links, this method is kind of a shortcut for the next option: cloud logging console GKE 控制台– 在 Google Cloud Console 的Google Kubernetes Engine部分中, select Kubernetes 资源在下一个 Log Workloads选项中列出,然后是控制台的快捷方式,用于审核下一个 Log Workloads 选项:

  • Cloud Logging console – You can see your logs directly from the Cloud Logging console by using the logging filters to select the Kubernetes resources, such as cluster, node, namespace, pod, or container logs. Cloud Logging 控制台– 您可以使用日志过滤器直接从Cloud Logging 控制台查看您的日志,方法是使用 select 和 Kubernetes 资源(例如集群、节点、命名空间、pod 或容器日志)。 Here are sample Kubernetes-related queries to help get you started.以下是与 Kubernetes 相关的示例查询,可帮助您入门。

    • This is the query I used (redacted project details):这是我使用的查询(编辑项目详细信息):
       resource.type="k8s_container" resource.labels.project_id="PROJECT_NAME" resource.labels.location="ZONE" resource.labels.cluster_name="CLUSTER_NAME" resource.labels.namespace_name="NAMESPACE" labels.k8s-pod/job-name:"cron-job-logging-"
    • Here are the result:结果如下: 在此处输入图像描述
  • Cloud Monitoring console – If you have enabled a Cloud Monitoring Workspace, in the Kubernetes Engine section of the Cloud Monitoring console, select your cluster, nodes, pod, or containers to view your logs. Cloud Monitoring 控制台– 如果您启用了 Cloud Monitoring 工作区,请在 Cloud Monitoring 控制台的Kubernetes Engine部分中,select 您的集群、节点、pod 或容器中查看您的日志。

  • gcloud command-line tool – Using the gcloud logging read command, select the appropriate cluster, node, pod, and container logs. gcloud命令行工具– 使用gcloud logging read命令 select 相应的集群、节点、pod 和容器日志。

  • Here an example:这里有一个例子:

$ gcloud logging read "resource.labels.project_id=PROJECT AND resource.labels.cluster_name=CLUSTER_NAME AND labels.k8s-pod/job-name:cron-job-logging-"
---
insertId: 6vorvd43akuvy8fi3
labels:
  k8s-pod/controller-uid: c525bbae-c6c9-11ea-931b-42010a80001f
  k8s-pod/job-name: cron-job-logging-1594838040
logName: projects/PROJECT/logs/stdout
receiveTimestamp: '2020-07-15T18:35:14.937645549Z'
resource:
  labels:
    cluster_name: CLUSTER_NAME
    container_name: cron-job-logging
    location: ZONE
    namespace_name: default
    pod_name: cron-job-logging-1594838040-pngsk
    project_id: PROJECT
  type: k8s_container
severity: INFO
textPayload: |
  a
timestamp: '2020-07-15T18:34:09.907735144Z'

More info here: GKE - Using Logs更多信息: GKE - 使用日志

If you have any question, let me know in the comments.如果您有任何问题,请在评论中告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM