Giter VIP home page Giter VIP logo

krs's Introduction

Go Report Card container repository on Quay

NOTE: this is heavy work in progress and we may introduce breaking, non-backwards compatible changes in any version before 1.0 and also, use it at your own risk, ideally only in dev and test environments.

krs is a command line tool for capturing and serializing Kubernetes resource statistics in OpenMetrics format. It dumps statistics about Kubernetes resources, for example the number of pods in a certain namespace, on a periodic basis to local storage. The kind of resources (pods, services, etc.) as well as the scope, that is, cluster-level or a list of namespaces, is configurable. You can use krs either on the client-side (for example, from your laptop) or in-cluster, like in a deployment. Note that krs leaves the decision where and how long-term storage is carried out up to you. More …


Index:

Install

In order to use krs you must meet the following two prerequisites:

  1. kubectl must be installed.
  2. Access to a Kubernetes cluster must be configured.

Here are my test environments: a v1.9 cluster via OpenShift Online, a v1.10 cluster via AKS, and a v1.11 cluster via Minikube, all with a kubectl@v1.11 client-side, on macOS.

From binaries

You can download binaries for Linux, macOS, and Windows which are available via the release page. To install from binary, for example, on a macOS system, do:

$ curl -L $(curl -s https://api.github.com/repos/mhausenblas/krs/releases/latest | grep macos | grep browser_download_url | cut -d : -f 2,3 | tr -d \") -o krs
$ chmod +x krs
$ sudo mv krs /usr/local/bin

From source

Assuming you've got Go in version 1.10 or above installed you can install krs from source like so:

$ go get -u github.com/mhausenblas/krs

From Kubernetes

You can launch krs in Kubernetes using the launch.sh script as follows. The script will dynamically create the respective permissions for the namespace and run krs as a deployment.
So, in a sense this is a self-test: create and watch a namespace krs-test and view the output like so:

$ kubectl create ns krs-test
namespace/krs-test created

$ ./launch.sh krs-test
serviceaccount/krs created
clusterrole.rbac.authorization.k8s.io/resreader configured
rolebinding.rbac.authorization.k8s.io/allowpodprobes created
deployment.apps/krs created

$ kubectl -n krs-test logs -f $(kubectl -n krs-test get po -l=run=krs --output=jsonpath={.items[*].metadata.name})
# HELP pods Number of pods in any state, for example running
# TYPE pods gauge
pods{namespace="krs-test"} 1
# HELP deployments Number of deployments
# TYPE deployments gauge
deployments{namespace="krs-test"} 1
# HELP services Number of services
# TYPE services gauge
services{namespace="krs-test"} 0
...

Use

krs assumes that kubectl is installed and configured. It writes the OpenMetrics data to stdout which you can then redirect to a file or process further.

For example, to gathers stats of the dev42 namespace and store the OpenMetrics formatted stats in a file called /tmp/krs/2018-10-05.om as well as see the errors on screen (via stdout), do the following:

$ krs --namespace dev42 >> /tmp/krs/2018-10-05.om

If you don't provide a namespace as the first argument, krs will watch the default namespace. Also, by default, the following resources are tracked: pods, deployments, and services.

For example, an excerpt of the output of the end-to-end test looks as follows:

# HELP pods Number of pods in any state, for example running
# TYPE pods gauge
pods{namespace="krs"} 2
# HELP deployments Number of deployments
# TYPE deployments gauge
deployments{namespace="krs"} 2
# HELP services Number of services
# TYPE services gauge
services{namespace="krs"} 1
# HELP pods Number of pods in any state, for example running
# TYPE pods gauge
pods{namespace="krs"} 2
# HELP deployments Number of deployments
# TYPE deployments gauge
deployments{namespace="krs"} 2
# HELP services Number of services
# TYPE services gauge
services{namespace="krs"} 1
...

If you, for example, want to track pods, stateful sets, and persistent volumes in the namespace prod1337 you'd launch krs like so:

$ krs --namespace prod1337 --resources "pods,sts,pv,pvc"

There are two environment variables that krs understands:

  • With KRS_KUBECTL_BIN you can define which kubectl binary krs will use. Note: under Windows this is required, for OpenShift, this is an option to use oc instead of kubectl.
  • With KRS_VERBOSE set (for example, to true but really any value does) you can get debug-level information, down to what kubectl call has been issued.

To do:

krs's People

Contributors

mhausenblas avatar vishnoisuresh 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  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  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  avatar  avatar

krs's Issues

krs_windows and docker's kubectl

Either spaces, or what seems to be cygwin/mingw's-like handling of names, or having "which" in the PATH...

C:\>krs
←[91mexec: "/c/Program Files/Docker/Docker/Resources/bin/kubectl": file does not exist←[0m
←[91munexpected end of JSON input←[0m

could be also due to something local that I have on my windows, where I have plenty of "unix"-like tools, and they might mis-cooperate sometimes:

C:\>krs
←[91mexec: "/c/Program Files/Docker/Docker/Resources/bin/kubectl": file does not exist←[0m
←[91munexpected end of JSON input←[0m

C:\>where kubectl
C:\Program Files\Docker\Docker\Resources\bin\kubectl.exe
C:\ProgramData\chocolatey\bin\kubectl.exe

C:\>which kubectl
/c/Program Files/Docker/Docker/Resources/bin/kubectl

C:\>where which
c:\tools\msys64\usr\bin\which.exe
C:\Program Files\Git\usr\bin\which.exe

C:\>which where
/c/WINDOWS/system32/where

but it's Windows... so I don't blame you :)

Permission issue as a non cluster admin

Running krs as a non cluster admin has some issues (Tested on OpenShift with a developer role):

$ ./krs kafka-confluent-5
exit status 1
unexpected end of JSON input

Handling more resources

Hi,

First, thanks for krs :) .

kubectl get all doesn't get all resources, and krs doesn't count all of them anyway.
Can we change that?

My proposal:
Step 1: replace kubectl get allby kubectl get deploy,po,svc.
Step 2: make toOpenMetrics() more naive by blindy use the Kind value and build a dynamic namespaceStats{}.
Step 3: make the "deploy,po,svc" part of step 1 overridable at runtime with a flag. (Like /app/krs --get 'svc,po,deploy,ing,pdb' $NAMESPACE)

Step 4 for extra points: make another flag for "all we can have", which build an exhaustive list of kinds to get by parsing the return of kubectl api-resources --verbs=list.

I can make the patch if you approve.

Thanks.
--Nicolas

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.