Code Monkey home page Code Monkey logo

k8stail's Introduction

k8stail

GitHub Actions codecov GitHub release

tail -f experience for Kubernetes Pods

As you know, kubectl logs can stream only ONE pod at the same time. k8stail enables you to watch log streams of ALL pods in the specified namespace or labels in real time, like tail -f.

example

Table of Contents

Requirements

Kubernetes 1.3 or above

Installation

Using Homebrew (OS X only)

Formula is available at dtan4/homebrew-dtan4.

$ brew tap dtan4/dtan4
$ brew install k8stail

Precompiled binary

Precompiled binaries for Windows, OS X, Linux are available at Releases.

From source

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

Run in a Docker container

Docker image is no longer provided officially. If you'd like to run k8sec in Docker image, see Dockerfile and build image by yourself.

docker build -t k8stail .

Usage

Logs of all pods, all containers in pod in the specified namespace are streaming. When new pod is added, logs of the pod also appears. To stop streaming and exit, press Ctrl-C.

$ k8stail --namespace awesome-app
Namespace: awesome-app
Labels:
----------
Pod awesome-app-web-4212725599-67vd4 has detected
Pod awesome-app-web-4212725599-6pduy has detected
Pod awesome-app-web-4212725599-lbuny has detected
Pod awesome-app-web-4212725599-mh3g1 has detected
Pod awesome-app-web-4212725599-pvjsm has detected
[awesome-app-web-4212725599-mh3g1][web]  | creating base compositions...
[awesome-app-web-4212725599-zei9h][web]  |    (47.1ms)  CREATE TABLE "schema_migrations" ("version" character varying NOT NULL)
[awesome-app-web-4212725599-zei9h][web]  |    (45.1ms)  CREATE UNIQUE INDEX  "unique_schema_migrations" ON "schema_migrations"  ("version")
[awesome-app-web-4212725599-zei9h][web]  |   ActiveRecord::SchemaMigration Load (1.8ms)  SELECT "schema_migrations".* FROM "schema_migrations"
[awesome-app-web-4212725599-zei9h][web]  | Migrating to CreatePosts (20160218082522)

With --timestamps option, log timestamp is printed together.

$ k8stail --namespace awesome-app --timestamps
Namespace: awesome-app
Labels:
----------
Pod awesome-app-web-4212725599-67vd4 has detected
Pod awesome-app-web-4212725599-6pduy has detected
Pod awesome-app-web-4212725599-lbuny has detected
Pod awesome-app-web-4212725599-mh3g1 has detected
Pod awesome-app-web-4212725599-pvjsm has detected
[awesome-app-web-4212725599-mh3g1][web] 2016-11-15T10:57:22.178667425Z  | creating base compositions...
[awesome-app-web-4212725599-zei9h][web] 2016-11-15T10:57:22.309011520Z  |    (47.1ms)  CREATE TABLE "schema_migrations" ("version" character varying NOT NULL)
[awesome-app-web-4212725599-zei9h][web] 2016-11-15T10:57:22.309053601Z  |    (45.1ms)  CREATE UNIQUE INDEX  "unique_schema_migrations" ON "schema_migrations"  ("version")
[awesome-app-web-4212725599-zei9h][web] 2016-11-15T10:57:22.463700110Z  |   ActiveRecord::SchemaMigration Load (1.8ms)  SELECT "schema_migrations".* FROM "schema_migrations"
[awesome-app-web-4212725599-zei9h][web] 2016-11-15T10:57:22.463743373Z  | Migrating to CreatePosts (20160218082522)

With --labels option, you can filter pods to watch.

$ k8stail --namespace awesome-app --labels name=awesome-app-web
Namespace: awesome-app
Labels:    name=awesome-app-web
----------
Pod awesome-app-web-4212725599-67vd4 has detected
Pod awesome-app-web-4212725599-6pduy has detected
Pod awesome-app-web-4212725599-lbuny has detected
Pod awesome-app-web-4212725599-mh3g1 has detected
Pod awesome-app-web-4212725599-pvjsm has detected
[awesome-app-web-4212725599-mh3g1][web]  | creating base compositions...
[awesome-app-web-4212725599-zei9h][web]  |    (47.1ms)  CREATE TABLE "schema_migrations" ("version" character varying NOT NULL)
[awesome-app-web-4212725599-zei9h][web]  |    (45.1ms)  CREATE UNIQUE INDEX  "unique_schema_migrations" ON "schema_migrations"  ("version")
[awesome-app-web-4212725599-zei9h][web]  |   ActiveRecord::SchemaMigration Load (1.8ms)  SELECT "schema_migrations".* FROM "schema_migrations"
[awesome-app-web-4212725599-zei9h][web]  | Migrating to CreatePosts (20160218082522)

kubeconfig file

k8stail 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 k8stail
# or
$ k8stail --kubeconfig=/path/to/kubeconfig

Options

Option Description Required Default
--context=CONTEXT Kubernetes context
--debug Debug mode using pprof (http://localhost:6060) false
--kubeconfig=KUBECONFIG Path of kubeconfig ~/.kube/config
--labels=LABELS Label filter query (e.g. app=APP,role=ROLE)
--namespace=NAMESPACE Kubernetes namespace default
--timestamps Include timestamps on each line false
-h, -help Print command line usage
-v, -version Print version

Development

Go 1.7 or above is required.

Clone this repository and build using make.

$ go get -d github.com/dtan4/k8stail
$ cd $GOPATH/src/github.com/dtan4/k8stail
$ make

Author

Daisuke Fujita (@dtan4)

License

MIT License

k8stail's People

Contributors

atombender avatar dependabot-preview[bot] avatar dependabot[bot] avatar dtan4 avatar github-actions[bot] avatar renovate-bot avatar renovate[bot] 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  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

k8stail's Issues

Allow processing log message

We log JSON, and it would be really nice if I was able to pass the log message through jq. Can a --processor option be added? Then I could use jq, awk or whatever processing I'd like on it while still getting from multiple pods at once.

Bonus would the be to not print if the log-entry is empty ๐Ÿ˜„

Fails on pods with multiple containers

Sample output:

$ k8stail -namespace staging
Namespace: staging
Labels:    
----------
Pod grove-2249881886-gjekb has detected
a container name must be specified for pod grove-2249881886-gjekb, choose one of: [unicorn memcached]

Shouldn't it just tail all the containers in the pod?

Should use namespace from current context

It looks like the context's namespace is ignored:

$ kubectl config get-contexts staging
CURRENT   NAME      CLUSTER   AUTHINFO  NAMESPACE
*         staging   primary   primary   staging
$ k8stail --labels app=postgresql   
Context:   staging
Namespace: default
Labels:    app=postgresql
Press Ctrl-C to exit.

Authenticate GKE access before executing command

WHAT

Authenticate GKE access before executing k8stail command by kicking GKE credential helper.

WHY

After switching current context to GKE cluster, k8stail fails with the error below.

$ k8stail
Context:   xxxxx
Namespace: default
Labels:
Press Ctrl-C to exit.
----------
Get https://xxxxx/api/v1/watch/namespaces/default/pods: error executing access token command "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/bin/gcloud ": exit status 2

Executing kubectl get po once resolves this. GKE credentials is ephemeral and refreshed periodically.

Add support of container specification

Please add support of container name specification. It may help if some pods has multiple containers.
Example:

k8stail -n test -c backend
k8stail -n test -c nginx

Does not support context

In GKE, gcloud container get-credentials create multiple contexts in the same ~/.kube/config.
If k8stail support the context option, it should be very useful.

Support "kubectl logs --since" flag

Its sometimes nice to start a tail back a few seconds / minutes. Kubectl does this w/ the --since flag

Example:

kubectl logs billwise-disabler-us-deployment-4141292026-339rz -f --since=60s

It would be nice if k8stail had similar behavior

Deleted pods cause tailing to stop with error

Here I am deleting one pod while tailing:

$ k8stail --labels app=picaxe --no-halt
Context:   staging
Namespace: staging
Labels:    app=picaxe
Press Ctrl-C to exit.
----------
Pod:picaxe-1174101182-1gjc6 Container:picaxe has been detected
Pod:picaxe-1174101182-5kxk8 Container:picaxe has been detected
Pod:picaxe-1174101182-7038f Container:picaxe has been detected
Pod:picaxe-1174101182-8n159 Container:picaxe has been detected
Pod:picaxe-1174101182-1gjc6 Container:picaxe has been deleted
Pod:picaxe-1174101182-1gjc6 Container:picaxe has been detected
the server could not find the requested resource (get pods picaxe-1174101182-1gjc6)
$

In my opinion, tailing should never stop.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/release.yaml
  • actions/checkout v4@692973e3d937129bcbf40652eb9f2f61becf3332
  • actions/setup-go v5
  • goreleaser/goreleaser-action v5
.github/workflows/test.yaml
  • actions/checkout v4@692973e3d937129bcbf40652eb9f2f61becf3332
  • actions/setup-go v5
  • codecov/codecov-action v3
  • aquasecurity/trivy-action 0.23.0
gomod
go.mod
  • github.com/fatih/color v1.17.0
  • github.com/spf13/pflag v1.0.5
  • k8s.io/api v0.29.4
  • k8s.io/apimachinery v0.29.4
  • k8s.io/client-go v0.29.4

  • Check this box to trigger a request for Renovate to run again on this repository

Request: Support filtering on pod name

I've been using kubetail lately because it allows me to just do:

$ kubetail postgres
Will tail 1 logs...
postgresql-master-1744499466-r9znc
[...]

However, kubetail is just a shell script and has limited ability to do things like watch for new pods, and doesn't support multiple containers per pod. I'd love to have this filtering ability in k8stail.

It goes without saying, I think, that it should watch for new pods that match the filters (including labels) and also stop tailing old pods that go away.

Detect container recreaction

WHY

Currently recreated container, which pod still remains, is not detected by k8stail. Pod recreation is supported only.

WHAT

Detect container recreation

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.