访问cron作业kubernetes中的日志



我正在kubernetes中运行CronJob,作业成功完成,我将输出记录到(path: storage/logs)内的日志文件中,但由于容器处于完成状态,我无法访问该文件。

这是我的工作yaml:

apiVersion: v1
items:
- apiVersion: batch/v1beta1
kind: CronJob
metadata:
labels:
chart: cronjobs-0.1.0
name: cron-cronjob1
namespace: default
spec:
concurrencyPolicy: Forbid
failedJobsHistoryLimit: 1
jobTemplate:      
spec:
template:
metadata:          
labels:
app: cron
cron: cronjob1
spec:
containers:
- args:
- /usr/local/bin/php
- -c
- /var/www/html/artisan bulk:import
env:
- name: DB_CONNECTION
value: postgres
- name: DB_HOST
value: postgres
- name: DB_PORT
value: "5432"
- name: DB_DATABASE
value: xxx
- name: DB_USERNAME
value: xxx
- name: DB_PASSWORD
value: xxxx
- name: APP_KEY
value: xxxxx
image: registry.xxxxx.com/xxxx:2ecb785-e927977
imagePullPolicy: IfNotPresent
name: cronjob1
ports:
- containerPort: 80
name: http
protocol: TCP              
imagePullSecrets:
- name: xxxxx
restartPolicy: OnFailure          
terminationGracePeriodSeconds: 30
schedule: '* * * * *'
successfulJobsHistoryLimit: 3

我是否可以通过kubectl log <podname>命令或其他选项来显示日志文件内容?

Cronjob根据spec.schedule运行pod。任务完成后,pod的状态将设置为completed,但cronjob控制器在完成后不会删除pod。日志文件内容仍然存在于pod的容器文件系统中。所以你需要做:

# here  you can get the pod_name from the stdout of the cmd `kubectl get pods`
$ kubectl logs -f -n default <pod_name>

我想您已经知道,pod是像successfulJobsHistoryLimit: 3一样保留在周围的。假设您的观点是,您的日志记录将记录到一个文件,而不是stdout,因此您在kubectl logs中看不到它。如果是这样的话,也许您还可以登录到stdout,或者在作业中放入一些东西,以便在最后记录文件的内容,例如在PreStop钩子中。

最新更新