Coder Social home page Coder Social logo

kubemem's Introduction

Kubemem

Kubemem is a small binary intended to be used as a liveness probe inside of your Kubernetes applications that will allow graceful failure when the memory starts to get too high. As it is not a shell script, it should be fast and portable for your containers.

Kubernetes already stops containers from using too much memory! What's the point?

Kubernetes does not offer a way to add a graceful handler for OOM killed containers currently, so this is a nice medium. Where I work, we have long running Sidekiq jobs with pods that should try to exit gracefully but are instead immediately trashed once they hit the memory threshold.

How it works

Kubemem has three options:

  • a warning percentage: When your RAM usage hits this threshold, kubemem will log the warning
  • a failure percentage: When your RAM usage hits this threshold, kubemem will exit with a non-zero exit code, causing kubernetes to trigger a graceful kill of your pod.
  • a logfile path

Kubemem will use the cgroup filesystem to get memory info, and then use your options to either log or fail.

Usage

First, pick the tag you'd like to use:

  • glibc: Debian-based builds
  • musl: This is what you'll select for alpine

Simply add this container as another stage in your build:

FROM sixteenbitt/kubemem:glibc AS kubemem

# Do your normal build

COPY --from=kubemem /bin/kubemem /bin/kubemem

And then add a probe to your deployment:

    livenessProbe:
      exec:
        command:
        - kubemem
        - --failure
        - "95" # Fail at 95% memory
        - --warning
        - "85" # Warn at 85% or greater memory usage
        - --logfile
        - /proc/1/fd/1 # Write to PID 1's output, allowing logging
      initialDelaySeconds: 5
      periodSeconds: 5

If you'd like to play around with it, you can run it from the command line:

docker run sixteenbitt/kubemem:musl kubemem --warning 80 --failure 90

A note about logging

Kubernetes currently aggregates probe logs in kubelet, and only if logging is set to verbosity level 4 or higher. See this issue for more info about how kubernetes handles probe logs if you'd like to capture them.

As a workaround, set your logfile parameter to /proc/1/fd/1 to write to the containers PID 1 stdout for logging.

kubemem's People

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.