y-ohgi's blog

しがないSREなフリーランスの徒然

sternを触ってみる

概要

Kubernetesのpodsログ閲覧用コマンド

kubectl logsだといちいちpodの指定をしたり、過去のログを見れなかったりでしんどい
それを解決してくれたのが wercker/stern

How To Use

Install

brew でサクッと

$ brew install stern

monitoring log

$ stern <POD NAME>

Example

basic usage

$ cat web.yaml 
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: main
    spec:
      containers:
        - name: web
          image: nginx
          ports:
            - containerPort: 80
$ kubectl get pods
NAME                   READY     STATUS    RESTARTS   AGE
web-667bdcb4d8-nlh6b   1/1       Running   0          1h
$ kubectl port-forward web-667bdcb4d8-nlh6b 8080:80 &
$ stern web
+ web-667bdcb4d8-nlh6b › web
web-667bdcb4d8-nlh6b web 127.0.0.1 - - [09/May/2018:11:31:31 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4" "-"
web-667bdcb4d8-nlh6b web 127.0.0.1 - - [09/May/2018:11:31:32 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4" "-"
web-667bdcb4d8-nlh6b web 127.0.0.1 - - [09/May/2018:11:31:33 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4" "-"
web-667bdcb4d8-nlh6b web 127.0.0.1 - - [09/May/2018:11:31:33 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4" "-"
web-667bdcb4d8-nlh6b web 127.0.0.1 - - [09/May/2018:11:31:34 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4" "-"
web-667bdcb4d8-nlh6b web 127.0.0.1 - - [09/May/2018:11:31:34 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4" "-"
web-667bdcb4d8-nlh6b web 127.0.0.1 - - [09/May/2018:11:35:40 +0000] "GET /robots.txt HTTP/1.1" 404 572 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36" "-"

f:id:y-ohgi:20180509205414g:plain

10分以内に吐き出されたログの閲覧

$ stern web --since 10m

正規表現を用いてpodの指定

$ stern web-.*

labelを指定して閲覧

$ stern -lapp=main

参考