⎈ Multi pod and container log tailing for Kubernetes

Overview

stern

wercker status

Stern allows you to tail multiple pods on Kubernetes and multiple containers within the pod. Each result is color coded for quicker debugging.

The query is a regular expression so the pod name can easily be filtered and you don't need to specify the exact id (for instance omitting the deployment id). If a pod is deleted it gets removed from tail and if a new pod is added it automatically gets tailed.

When a pod contains multiple containers Stern can tail all of them too without having to do this manually for each one. Simply specify the container flag to limit what containers to show. By default all containers are listened to.

Installation

If you don't want to build from source go grab a binary release

Govendor is required to install vendored dependencies.

mkdir -p $GOPATH/src/github.com/wercker
cd $GOPATH/src/github.com/wercker
git clone https://github.com/wercker/stern.git && cd stern
govendor sync
go install

Homebrew

On macOS, you can also install Stern using Homebrew:

brew install stern

Usage

stern pod-query [flags]

The pod query is a regular expression so you could provide "web-\w" to tail web-backend and web-frontend pods but not web-123.

cli flags

flag default purpose
--container .* Container name when multiple containers in pod (regular expression)
--exclude-container Container name to exclude when multiple containers in pod (regular expression)
--container-state running Tail containers with status in running, waiting or terminated. Default to running.
--timestamps Print timestamps
--since Return logs newer than a relative duration like 52, 2m, or 3h. Displays all if omitted
--context Kubernetes context to use. Default to kubectl config current-context
--exclude Log lines to exclude; specify multiple with additional --exclude; (regular expression)
--namespace Kubernetes namespace to use. Default to namespace configured in Kubernetes context
--kubeconfig ~/.kube/config Path to kubeconfig file to use
--all-namespaces If present, tail across all namespaces. A specific namespace is ignored even if specified with --namespace.
--selector Selector (label query) to filter on. If present, default to .* for the pod-query.
--tail -1 The number of lines from the end of the logs to show. Defaults to -1, showing all logs.
--color auto Force set color output. auto: colorize if tty attached, always: always colorize, never: never colorize
--output default Specify predefined template. Currently support: [default, raw, json] See templates section
template Template to use for log lines, leave empty to use --output flag

See stern --help for details

Stern will use the $KUBECONFIG environment variable if set. If both the environment variable and --kubeconfig flag are passed the cli flag will be used.

templates

stern supports outputting custom log messages. There are a few predefined templates which you can use by specifying the --output flag:

output description
default Displays the namespace, pod and container, and decorates it with color depending on --color
raw Only outputs the log message itself, useful when your logs are json and you want to pipe them to jq
json Marshals the log struct to json. Useful for programmatic purposes

It accepts a custom template through the --template flag, which will be compiled to a Go template and then used for every log message. This Go template will receive the following struct:

property type description
Message string The log message itself
Namespace string The namespace of the pod
PodName string The name of the pod
ContainerName string The name of the container

The following functions are available within the template (besides the builtin functions):

func arguments description
json object Marshal the object and output it as a json text
color color.Color, string Wrap the text in color (.ContainerColor and .PodColor provided)

Examples:

Tail the gateway container running inside of the envvars pod on staging

stern envvars --context staging --container gateway

Tail the staging namespace excluding logs from istio-proxy container

stern -n staging --exclude-container istio-proxy .

Show auth activity from 15min ago with timestamps

stern auth -t --since 15m

Follow the development of some-new-feature in minikube

stern some-new-feature --context minikube

View pods from another namespace

stern kubernetes-dashboard --namespace kube-system

Tail the pods filtered by run=nginx label selector across all namespaces

stern --all-namespaces -l run=nginx

Follow the frontend pods in canary release

stern frontend --selector release=canary

Pipe the log message to jq:

stern backend -o json | jq .

Only output the log message itself:

stern backend -o raw

Output using a custom template:

stern --template '{{.Message}} ({{.Namespace}}/{{.PodName}}/{{.ContainerName}})' backend

Output using a custom template with stern-provided colors:

stern --template '{{.Message}} ({{.Namespace}}/{{color .PodColor .PodName}}/{{color .ContainerColor .ContainerName}})' backend

Completion

Stern supports command-line auto completion for bash or zsh. stern --completion=(bash|zsh) outputs the shell completion code which work by being evaluated in .bashrc, etc for the specified shell. In addition, Stern supports dynamic completion for --namespace and --context. In order to use that, kubectl must be installed on your environment.

If you use bash, stern bash completion code depends on the bash-completion. On the macOS, you can install it with homebrew as follows:

$ brew install bash-completion

Note that bash-completion must be sourced before sourcing the stern bash completion code in .bashrc.

source <(brew --prefix)/etc/bash-completion
source <(stern --completion=bash)

If you use zsh, just source the stern zsh completion code in .zshrc.

source <(stern --completion=zsh)

Contributing to this repository

Oracle welcomes contributions to this repository from anyone. Please see CONTRIBUTING for details.

Issues
  • add exclude-container option

    add exclude-container option

    Tried it locally, it helps a lot :)

    this fixes #78

    This excludes logs from a container matching the exclude-container filter. I updated the docs.

    If no option is passed we set the regexp pointer to nil and nothing is run.

    To validate changes try it, since there are no tests, for me it was: stern -n staging . -E "istio" just like in the doc update I did, and it worked !

    opened by azr 17
  • JSON logs

    JSON logs

    Hi, we output our logs to stdout in JSON format. They're later captured by ELK stack. By using JSON we don't have to create log parsers. The drawback of this solution is that it's not super human readable when read not through Kibana. So my idea is to add some parameter that would allow you to display only specific fields from this JSON in a more human readable way. What do you think?

    opened by tmszdmsk 15
  • support --include in addition to --exclude

    support --include in addition to --exclude

    Since there's --exclude, I found it quite surprising to not see its mirror, --include. The obvious example usecase is

    stern '.*' --include ERROR
    

    to see all errors that occur anywhere.

    opened by majewsky 13
  • Add include feature

    Add include feature

    Should fix #66

    opened by stevenklar 12
  • Improve to be able to place flags after arguments

    Improve to be able to place flags after arguments

    Currently stern does not work if flags are placed after arguments. With this commit, it improve to be able to place flags after arguments like kubectl.

    stern .* -n kube-system
    

    For this change, github.com/spf13/cobra is used instead of gopkg.in/urfave/cli.v1 and help message is changed accordingly. This commit is not breaking change, compatible.

    github.com/spf13/cobra is released under the Apache 2.0 License.

    opened by superbrothers 11
  • Support tailing init containers and passing container state

    Support tailing init containers and passing container state

    The existing watch doesn't listen for init container and it only listens to container in running state. Supporting init containers and container state is useful to users. In order not to bother users passing container state every time, the flag has the default value of "running". https://github.com/wercker/stern/issues/53

    opened by shun0309-zz 8
  • Add support for the GCP and OIDC auth providers

    Add support for the GCP and OIDC auth providers

    This required an upgrade to the 1.5 client.

    Fixes #9

    opened by fd 8
  • Add dynamic completion support for --namespace and --context

    Add dynamic completion support for --namespace and --context

    With this PR, stern supports dynamic completion for --namespace and --context. This feature depends on kubectl. And the name of --kube-config is changed to --kubeconfig and --kube-config is marked deprecated due to using bash completion for kubectl.

    $ source <(stern --completion=zsh)
    $ stern --context=<tab>
    --context=cluster01  --context=minikube
    $ stern --namespace <tab>
    default      kube-system
    
    opened by superbrothers 8
  • Update dependencies.

    Update dependencies.

    The biggest change here is to upgrade client-go to v8.0.0. This adds support for kubeconfig exec plugins as used in the aws-iam-authenticator (confirmed locally).

    This was my first time using govendor so let me know if I messed anything up.

    Fixes https://github.com/wercker/stern/issues/80.

    opened by mattmoyer 7
  • Fixing aws-iam-authenticator / EKS auth issue.

    Fixing aws-iam-authenticator / EKS auth issue.

    Continuing work of @mattmoyer started in https://github.com/wercker/stern/pull/81. Credits also to @guoyongzhang.

    --since must be set to value other than 0.

    Hope it fixes https://github.com/wercker/stern/issues/80, I don't own any EKS cluster to verify. Tested against Kubernetes 1.9.8 and aws-iam-authenticator v0.3.0.

    opened by michalschott 7
  • Create a `stern_darwin_amd64` build for the most recent release

    Create a `stern_darwin_amd64` build for the most recent release

    Now that the Apple M1 devices are getting more prevalent, especially among engineers, it would be really nice to have an arm64 binary pre-built and downloadable for stern.

    opened by spkane 1
  • failed to set up watch: failed to set up watch: Get https://192.168.43.20:16443/api/v1/namespaces/default/pods?watch=true: dial tcp 192.168.43.20:16443: getsockopt: no route to host

    failed to set up watch: failed to set up watch: Get https://192.168.43.20:16443/api/v1/namespaces/default/pods?watch=true: dial tcp 192.168.43.20:16443: getsockopt: no route to host

    My host VM IP got changed.

    After this, when I am running stern command.

    failed to set up watch: failed to set up watch: Get https://192.168.43.20:16443/api/v1/namespaces/default/pods?watch=true: dial tcp 192.168.43.20:16443: getsockopt: no route to host

    Is there anyway to change the config?

    opened by Yavdhesh 0
  • Dead project?

    Dead project?

    First of all, thanks for a great tool. I use it regularly and have thought about contributing multiple times. However, the project seems pretty dead after the acquisition of Wercker by Oracle. Please consider updating the README with the current status of the project. stern/stern seems to be a well maintained fork, so a mention of this or other forks would be welcome.

    Also, please consider archiving this repository so it clearly signals that contributions will not be accepted/processed. As a side effect it would also reduce notification noise for members of the @wercker org.

    opened by evenh 3
  • Support for multi namespace stern

    Support for multi namespace stern

    Stern currently supports --all-namespaces, but sometimes it is required to tail logs from multiple namespaces (not all namespaces) in a cluster with a number of namespaces. Support for a comma-separated namespace list for --namespace would be ideal.

    opened by ramesh-tr 1
  • Update completion.go

    Update completion.go

    replace __kubectl with __stern_kubectl, to not interference with kubectl bash completion

    opened by wtayyeb 1
  • Output is garbled on Windows 10

    Output is garbled on Windows 10

    .\stern_windows_amd64.exe --tail 1 --timestamps nginx

    nginx-6799fc88d8-4ttfk nginx 2020-11-23T20:17:56.455349100Z /docker-entrypoint.sh: Configuration complete; ready for start up

    opened by benathomson 3
  • Forked repository - https://github.com/stern/stern

    Forked repository - https://github.com/stern/stern

    Guys, So you know, we forked this repository to fix bugs and add new features (PR from this repo among other thing).

    https://github.com/stern/stern

    Don't hesitate to drop by and create issue/PR or simply download new releases.

    All credits to wercker for this, but let's hope we'll manage to keep the project alive.

    opened by floryut 0
  • Update README.md

    Update README.md

    opened by jayunit100 0
  • Build fails

    Build fails

    build std/github.com/wercker/stern: cannot load github.com/googleapis/gnostic/OpenAPIv2: module github.com/googleapis/[email protected] found (v0.5.1), but does not contain package github.com/googleapis/gnostic/OpenAPIv2

    opened by larytet 0
Releases(1.11.0)
Owner
wercker
Container-centric Automation Platform
wercker
General Pod Autoscaler(GPA) is a extension for K8s HPA, which can be used not only for serving, also for game.

Introduction General Pod Autoscaler(GPA) is a extension for K8s HPA, which can be used not only for serving, also for game. Features Compatible with a

Open Cloud-native Game-application Initiative 10 Aug 4, 2021
👀 A Kubernetes cluster resource sanitizer

Popeye - A Kubernetes Cluster Sanitizer Popeye is a utility that scans live Kubernetes cluster and reports potential issues with deployed resources an

Fernand Galiana 3.1k Sep 15, 2021
A toolbox for debugging docker container and kubernetes with web UI.

A toolbox for debugging Docker container and Kubernetes with visual web UI. You can start the debugging journey on any docker container host! You can

CloudNativer 7 May 18, 2021
🐶 Kubernetes CLI To Manage Your Clusters In Style!

K9s - Kubernetes CLI To Manage Your Clusters In Style! K9s provides a terminal UI to interact with your Kubernetes clusters. The aim of this project i

Fernand Galiana 13.4k Sep 10, 2021
⎈ Multi pod and container log tailing for Kubernetes

stern Stern allows you to tail multiple pods on Kubernetes and multiple containers within the pod. Each result is color coded for quicker debugging. T

wercker 5.4k Sep 13, 2021
A simple Kubernetes Operator template that uses Golang, use it to build your own operators

A simple programmatic Kubernetes Operator template. Use this to create your own Kubernetes operators with golang. Build with KIND (Kubernetes in Docke

Cloud Native Skunkworks 3 Apr 11, 2021
Simplified network and services for edge applications

English | 简体中文 EdgeMesh Introduction EdgeMesh is a part of KubeEdge, and provides a simple network solution for the inter-communications between servi

KubeEdge 54 Sep 13, 2021
A Kubernetes Network Fabric for Enterprises that is Rich in Functions and Easy in Operations

中文教程 Kube-OVN, a CNCF Sandbox Level Project, integrates the OVN-based Network Virtualization with Kubernetes. It offers an advanced Container Network

null 1k Sep 16, 2021
The k8s-generic-webhook is a library to simplify the implementation of webhooks for arbitrary customer resources (CR) in the operator-sdk or controller-runtime.

k8s-generic-webhook The k8s-generic-webhook is a library to simplify the implementation of webhooks for arbitrary customer resources (CR) in the opera

Norwin Schnyder 3 Sep 10, 2021
Interactive Cloud-Native Environment Client

Fenix-CLI:Interactive Cloud-Native Environment Client English | 简体中文 Fenix-CLI is an interactive cloud-native operating environment client. The goal i

IcyFenix 37 Aug 30, 2021
Kubedd – Check migration issues of Kubernetes Objects while K8s upgrade

Kubedd – Check migration issues of Kubernetes Objects while K8s upgrade

Devtron Labs 10 Sep 10, 2021
kubetnl tunnels TCP connections from within a Kubernetes cluster to a cluster-external endpoint, e.g. to your local machine. (the perfect complement to kubectl port-forward)

kubetnl kubetnl (kube tunnel) is a command line utility to tunnel TCP connections from within a Kubernetes to a cluster-external endpoint, e.g. to you

null 3 Sep 7, 2021
Enterprise-grade container platform tailored for multicloud and multi-cluster management

KubeSphere Container Platform What is KubeSphere English | 中文 KubeSphere is a distributed operating system providing cloud native stack with Kubernete

KubeSphere 6.7k Sep 9, 2021
kubequery is a Osquery extension that provides SQL based analytics for Kubernetes clusters

kubequery powered by Osquery kubequery is a Osquery extension that provides SQL based analytics for Kubernetes clusters kubequery will be packaged as

Uptycs Inc 52 Sep 10, 2021
go-zero 从零到 k8s 部署

启动: 注意事项: dockerfile 文件配置了 LOCAL_HOST 环境变量 1、项目目录下执行 ./docker.sh 脚本生成 rpc服务docker镜像 ./docker.sh 2、docker-compose-db 创建 mysql redis etcd 容器 执行命令

liukai 47 Sep 6, 2021
Production-Grade Container Scheduling and Management

Kubernetes (K8s) Kubernetes, also known as K8s, is an open source system for managing containerized applications across multiple hosts. It provides ba

Kubernetes 80.8k Sep 10, 2021
A Simple and Comprehensive Vulnerability Scanner for Container Images, Git Repositories and Filesystems. Suitable for CI

A Simple and Comprehensive Vulnerability Scanner for Containers and other Artifacts, Suitable for CI. Table of Contents Abstract Features Installation

Aqua Security 8.5k Sep 12, 2021
NVIDIA GPU metrics exporter for Prometheus leveraging DCGM

DCGM-Exporter This repository contains the DCGM-Exporter project. It exposes GPU metrics exporter for Prometheus leveraging NVIDIA DCGM. Documentation

NVIDIA Corporation 17 Sep 6, 2021
Lightweight Kubernetes

K3s - Lightweight Kubernetes Lightweight Kubernetes. Production ready, easy to install, half the memory, all in a binary less than 100 MB. Great for:

null 17.8k Sep 12, 2021