Skip to content

e-kostylov/k8s-job-cleaner

 
 

Repository files navigation

k8s-job-cleaner

Build Status Docker Repository on Quay

Clean up completed Kubernetes Jobs

What is this?

This tool provides Job cleaning feature based on k8s 1.6's Job History Limits.

For example, following command deletes completed Jobs and attached Pods, but leaves the last 10 Jobs per job label.

$ k8s-job-cleaner --label-group job --max-count 10

Requirements

Kubernetes 1.3 or above

Installation

From source

$ go get -d github.com/dtan4/k8s-job-cleaner
$ cd $GOPATH/src/github.com/dtan4/k8s-job-cleaner
$ make deps
$ make install

Run in a Docker container

Docker image is available at quay.io/dtan4/k8s-job-cleaner.

# -t is required to colorize logs
$ docker run \
    --rm \
    -t \
    -v $HOME/.kube/config:/.kube/config \
    quay.io/dtan4/k8s-job-cleaner:latest \
      --label-group job

Usage

In Kubernetes cluster

Just add --in-cluster flag.

$ k8s-job-cleaner --label-group job --max-count 10 --in-cluster

CronJob manifest sample:

apiVersion: batch/v2alpha1
kind: CronJob
metadata:
  name: k8s-job-cleaner
  labels:
    job: k8s-job-cleaner
    role: job
spec:
  schedule: "0 * * * *"
  startingDeadlineSeconds: 30
  concurrencyPolicy: Allow
  suspend: false
  jobTemplate:
    metadata:
      name: k8s-job-cleaner
      labels:
        job: k8s-job-cleaner
        role: job
    spec:
      template:
        metadata:
          name: k8s-job-cleaner
          labels:
            job: k8s-job-cleaner
            role: job
        spec:
          containers:
          - name: k8s-job-cleaner
            image: quay.io/dtan4/k8s-job-cleaner:latest
            imagePullPolicy: Always
            command:
              - "/k8s-job-cleaner"
              - "--in-cluster"
              - "--label-group"
              - "job"
              - "--max-count"
              - "20"
          restartPolicy: Never

Local machine

k8s-job-cleaner uses ~/.kube/config as default. You can specify another path by KUBECONFIG environment variable or --kubeconfig option. --kubeconfig option always overrides KUBECONFIG environment variable.

$ KUBECONFIG=/path/to/kubeconfig k8s-job-cleaner
# or
$ k8s-job-cleaner --kubeconfig=/path/to/kubeconfig

Options

Option Description Required Default
--context=CONTEXT Kubernetes context
--dry-run Dry run false
--in-cluster Execute in Kubernetes cluster false
--kubeconfig=KUBECONFIG Path of kubeconfig ~/.kube/config
--label-group=LABELS Label name for groupiung Jobs Required
--max-count=MAXCOUNT Number of Jobs to remain 10
--namespace=NAMESPACE Kubernetes namespace All namespaces
-h, -help Print command line usage
-v, -version Print version

Development

Clone this repository and build using make.

$ go get -d github.com/dtan4/k8s-job-cleaner
$ cd $GOPATH/src/github.com/dtan4/k8s-job-cleaner
$ make

Author

Daisuke Fujita (@dtan4)

License

MIT License

About

Clean up completed Kubernetes Jobs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 88.3%
  • Makefile 11.7%