⎈ Multi pod and container log tailing for Kubernetes -- Friendly fork of https://github.com/wercker/stern
toTimestamp
The toTimestamp
function takes in an object, a layout, and optionally a timezone. This allows for more custom time parsing, for instance, if a user doesn't care about seeing the date of the log and only the time (in their own timezone) they can use a template such as:
--template '{{ with $msg := .Message | tryParseJSON }}[{{ toTimestamp $msg.time "15:04:05" "Local" }}] {{ $msg.msg }}{{ end }}{{ "\n" }}'
stern now has the generic options that kubectl has, and a new --show-hidden-options
option.
$ stern --show-hidden-options
The following options can also be used in stern:
--as string Username to impersonate for the operation. User could be a regular user or a service account in a namespace.
--as-group stringArray Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
--as-uid string UID to impersonate for the operation.
--cache-dir string Default cache directory (default "/home/ksuda/.kube/cache")
--certificate-authority string Path to a cert file for the certificate authority
--client-certificate string Path to a client certificate file for TLS
--client-key string Path to a client key file for TLS
--cluster string The name of the kubeconfig cluster to use
--disable-compression If true, opt-out of response compression for all requests to the server
--insecure-skip-tls-verify If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure
--request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
--server string The address and port of the Kubernetes API server
--tls-server-name string Server name to use for server certificate validation. If it is not provided, the hostname used to contact the server is used
--token string Bearer token for authentication to the API server
--user string The name of the kubeconfig user to use
The number of kubectl generic options is so large that it makes it difficult to see stern's own list of options, so we usually hide them. Use --show-hidden-options
if you want to list.
The following template functions have been added in v1.26.0:
extractJSONParts
: Parse string as JSON and concatenate the given keystryExtractJSONParts
: Attempt to parse string as JSON and concatenate the given keys, returning text on failureYou can now use the config file to change the default values of stern options. The default config file path is ~/.config/stern/config.yaml
.
# <flag name>: <value>
tail: 10
max-log-requests: 999
timestamps: short
You can change the config file path with --config
flag or STERNCONFIG
environment variable.
--timestamps
flag now accepts a format, one of default
or short
.
default
: the original format 2006-01-02T15:04:05.000000000Z07:00
(RFC3339Nano with trailing zeros)short
: the new format 01-02 15:04:05
(time.DateTime without year).If --timestamps
is specified but without value, default
is used to maintain backward compatibility.
$ stern --timestamps=short -n kube-system ds/kindnet --no-follow --tail 1 --only-log-lines
kindnet-hqn2k kindnet-cni 03-12 09:29:53 I0312 00:29:53.620499 1 main.go:250] Node kind-worker3 has CIDR [10.244.1.0/24]
kindnet-5f4ms kindnet-cni 03-12 09:29:53 I0312 00:29:53.374482 1 main.go:250] Node kind-worker3 has CIDR [10.244.1.0/24]
--node
flag to filter on a specific nodeNew --node
flag allows you to filter pods on a specific node. This flag will be helpful when we debug pods on the specific node.
# Print a DaemonSet pod on the specific node
stern --node <NODE_NAME> daemonsets/<DS_NAME>
# Print all pods on the specific node
stern --node <NODE_NAME> --all-namespaces --no-follow --max-log-requests 1 .
--include
flag now highlight matched strings in the log lines.
all
option to --container-state
flag--container-state
flag now accepts all
that is the same with specifying running,waiting,terminated
. This change is helpful when we debug CrashLoopBackoff containers.
# Before
stern --container-state running,waiting,terminated <QUERY>
# After
stern --container-state all <QUERY>`
--max-log-requests
flag to limit concurrent requestsNew --max-log-requests
flag allows you to limit concurrent requests to prevent unintentional load to a cluster. The behavior and the default are different depending on the presence of the --no-follow
flag.
--no-follow |
default | behavior |
---|---|---|
specified | 5 | limits the number of concurrent logs to request |
not specified | 50 | exits with an error when if it reaches the concurrent limit |
If you want to change to the same behavior as before, specify a sufficiently large value for --max-log-requests
.
--container-state
flag to all
The default value of --container-state
has been changed to all
from running
. With this change, stern will now show logs of completed (terminated
) and CrashLoopBackoff (waiting
) pods in addition to running pods by default.
If you want to change to the same behavior as before, explicitly specify --container-state
to running
.
all
(#225) 2502c91 (Takashi Kusumi)all
option to --container-state flag (#222) 6e0d5fc (Takashi Kusumi)--no-follow
flag to exit when all logs have been shownNew --no-follow
flag allows you to exit when all logs have been shown.
stern --no-follow .
<resource>/<name>
form as a queryStern now supports a Kubernetes resource query in the form <resource>/<name>
. Pod query can still be used.
stern deployment/nginx
The following Kubernetes resources are supported:
Shell completion of stern already supports this feature.
New --verbosity
flag allows you to set the log level verbosity of Kubernetes client-go. This feature is useful when you want to know how stern interacts with a Kubernetes API server in troubleshooting.
stern --verbosity=6 .
New --only-log-lines
flag allows you to print only log lines (and errors if occur). The difference between not specifying the flag and specifying it is as follows:
$ stern . --tail=1 --no-follow
+ nginx-cfbcb7b98-96xsv › nginx
+ nginx-cfbcb7b98-29wn7 › nginx
nginx-cfbcb7b98-96xsv nginx 2023/01/27 13:20:48 [notice] 1#1: start worker process 46
- nginx-cfbcb7b98-96xsv › nginx
nginx-cfbcb7b98-29wn7 nginx 2023/01/27 13:20:45 [notice] 1#1: start worker process 46
- nginx-cfbcb7b98-29wn7 › nginx
$ stern . --tail=1 --no-follow --only-log-lines
nginx-cfbcb7b98-96xsv nginx 2023/01/27 13:20:48 [notice] 1#1: start worker process 46
nginx-cfbcb7b98-29wn7 nginx 2023/01/27 13:20:45 [notice] 1#1: start worker process 46
<resource>/<name>
form as a query (#208) 7bc45f0 (Takashi Kusumi)You can now use the following two new output modes:
extjson
is an extended json mode where the whole output is embeded in json and the namespace/pod/container values are color-coded as in the default mode.ppextjson
is the same as the above with pretty-print, so each field is on its own line.Both of those modes can be piped into jq if you want full pretty-print, but you'll lose the original color-coding.
$ stern --output extjson .
$ stern --output extjson . | jq '.'
$ stern --output ppextjson .
See https://github.com/stern/stern/pull/190 for more details.
You can now use krew, the kubectl plugin manager, to install stern :tada:
kubectl krew install stern
stern has support for darwin/arm64 and windows/arm64 🥳
You can now parse JSON logs by using parseJSON
function in --template
flag. See https://github.com/stern/stern/pull/160 for more details.
stern --template='{{.PodName}}/{{.ContainerName}} {{with $d := .Message | parseJSON}}[{{$d.level}}] {{$d.message}}{{end}}{{"\n"}}' backend
Since this version, the archive files will no longer be wrapped in a directory.
Before
$ tar -tf stern_1.20.1_darwin_amd64.tar.gz
stern_1.20.1_darwin_amd64/LICENSE
stern_1.20.1_darwin_amd64/stern
After
$ tar -tf stern_1.21.0_darwin_amd64.tar.gz
LICENSE
stern
parseJSON
to --template flag (#160) 60e2a77 (Kazuki Suda)docker pull ghcr.io/stern/stern:1
docker pull ghcr.io/stern/stern:1.21
docker pull ghcr.io/stern/stern:1.21.0
docker pull ghcr.io/stern/stern:1
docker pull ghcr.io/stern/stern:1.20
docker pull ghcr.io/stern/stern:1.20.0