apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: elasticsearch
namespace: elasticsearch
labels:
app.kubernetes.io/name: elasticsearch
spec:
successfulJobsHistoryLimit: 10
failedJobsHistoryLimit: 10
concurrencyPolicy: Forbid #禁止并发运行
schedule: "* * * * *"
jobTemplate: #运行一个job
spec:
template:
metadata:
name: del-es-index-cronjob
spec:
restartPolicy: OnFailure
containers:
- name: curl-es
image: harbor.xxxx.com/base_image/alpine-date-curl
command:
- "/bin/sh"
- "-c"
- >
curl -X DELETE http://elasticsearch:9200/*`date +%Y.%m.%d -d "-7 days"`?pretty
查看cronjob
$ kubectl get cronjob -n elasticsearch
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
elasticsearch * * * * * False 0 <none> 11s
查看运行的jobs
$ kubectl get jobs -n elasticsearch
NAME DESIRED SUCCESSFUL AGE
elasticsearch-1552553820 1 1 2m
elasticsearch-1552553880 1 1 1m
elasticsearch-1552553940 1 1 46s
查看jobs执行的情况
$ pods=$(kubectl get pods --selector=job-name=elasticsearch-1552553820 --output=jsonpath={.items..metadata.name} -n elasticsearch) $ echo $pods
elasticsearch-1552553820-6cb4r $ kubectl logs elasticsearch-1552553820-6cb4r -n elasticsearch
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
{
"acknowledged" : true
}
100 28 100 28 0 0 10 0 0:00:02 0:00:02 --:--:-- 10