我有一個 cronjob 可以清理一些作業,因為我的 kubernetes 是舊版本,所以不能使用ttlafterfinished. 如何獲取部署了此作業的命名空間并動態傳遞命名空間名稱,而不是多次重復相同的命令?
這是我的 cronjob:
kind: CronJob
metadata:
name: jobs-cleanup
spec:
schedule: "*/30 * * * *"
successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 1
jobTemplate:
spec:
template:
spec:
serviceAccountName: cleanup-operator
containers:
- name: jobs-cleanup
image: google.io/test/job:1.0
args:
- -c
- |
for i in $(seq 9);do
kubectl get jobs --sort-by=.metadata.creationTimestamp -n dev$i | grep "job-init" | cut -d' ' -f1 | tac | tail -n 2 | xargs -I % kubectl delete jobs % -n dev$i;done
kubectl get jobs --sort-by=.metadata.creationTimestamp -n stg1 | grep "fusionauth-job" | cut -d' ' -f1 | tac | tail -n 2 | xargs -I % kubectl delete jobs % -n stg1
kubectl get jobs --sort-by=.metadata.creationTimestamp -n pt1 | grep "fusionauth-job" | cut -d' ' -f1 | tac | tail -n 2 | xargs -I % kubectl delete jobs % -n pt1
kubectl get jobs --sort-by=.metadata.creationTimestamp -n sit1 | grep "fusionauth-job" | cut -d' ' -f1 | tac | tail -n 2 | xargs -I % kubectl delete jobs % -n sit1
command:
- /bin/sh
restartPolicy: Never
imagePullSecrets:
- name: secret
uj5u.com熱心網友回復:
您現在正在做的更靈活的替代方法是將要洗掉的作業名稱串列存盤在串列/陣列中。然后,您可以遍歷作業名稱串列并執行命令。
以下是您的命令的更簡單 (IMO) 版本,它使用-o=jsonpath支持kubectl來指定搜索條件。
# The list of jobs you want to delete from any/all namespaces.
jobs_list="job-init fusionauth-job"
for job_name in ${jobs_list}; do
kubectl get jobs -A --sort-by=.metadata.creationTimestamp \
-o=jsonpath="{range .items[?(@.metadata.name == '${job_name}')]}{.metadata.namespace} {.metadata.name}{'\n'}{end}" \
| while read namespace job;do kubectl delete job ${job} -n ${namespace};done;
done
由于下面的評論/對話,添加了以下內容。
如果集群中的作業名稱具有遞增的后綴,例如fusionauth-job-01,fusionauth-job-02等,并且您只想在特定命名空間中保留該作業的最新實體,那么您將需要利用類似的東西,以便jq您可以進行一些正則運算式匹配. kubectl的jsonpath功能不支持正則運算式。
例如,假設您的集群具有如下所示的作業。
NAMESPACE NAME
default fusionauth-job-01
team1 fusionauth-job-01
team1 fusionauth-job-02
team1 job-init-01
team1 job-init-02
team2 fusionauth-job-01
team2 fusionauth-job-02
team2 fusionauth-job-03
此外,作業按時間戳順序列出,這意味著fusionauth-job-03是namespace 中的最新作業(其名稱匹配),是fusionauth-job*namespaceteam2中job-init-02的最新作業(其名稱匹配job-init*),是 namespaceteam1中fusionauth-job-02的最新作業(該名稱模式)team1,并且fusionauth-job-01是在命名空間中運行的唯一作業default。
運行您的jobs-cleanup作業后,預計會留下以下作業:
NAMESPACE NAME
default fusionauth-job-01
team1 fusionauth-job-02
team1 job-init-02
team2 fusionauth-job-03
生成這些結果的腳本如下所示:
# The list of job name prefixes you want to delete from any/all namespaces.
jobs_list="job-init fusionauth-job"
for job_name in ${jobs_list}; do
ns_observed=""
echo "Searching for ${job_name}*"
kubectl get jobs -A --sort-by=.metadata.creationTimestamp -o json \
| jq -r '.items[] | select(.metadata.name | test('\"${job_name}\"')) | .metadata.namespace " " .metadata.name' \
| tac \
| while read namespace job;do
if test "${ns_observed#*$namespace}" != "$ns_observed";then
# If we've seen this namespace already for this job, then delete the
# job, since the one we found previously is the newest.
kubectl delete job ${job} -n ${namespace};
else
# Otherwise, if this is the first time observing this namespace, then
# this is the newest job that starts with this job name in this namespace.
ns_observed="$ns_observed $namespace";
fi;
done;
done
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/474918.html
標籤:Kubernetes Kubernetes-cronjob
上一篇:Helm圖表和Ingress資源
下一篇:如何在氣流中匯入秘密?
