Giter Club home page Giter Club logo

k8s-job-cleaner's Introduction

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

k8s-job-cleaner's People

Contributors

dtan4 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

k8s-job-cleaner's Issues

Error running on local machine

./k8s-job-cleaner --dry-run --label-group=mylabel
failed to retrieve Jobs: Get https://my.ip.add.ress/apis/batch/v1/jobs: error executing access token command "/usr/lib/google-cloud-sdk/bin/gcloud ": exit status 2

My configuration is fine, I did gcloud container clusters get-credentials and can run other gcloud commands on the cluster (such as creating pools), as well as kubectl commands just fine.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.