サイト案内

運営してるひと: @sters9

妻と娘と猫と神奈川県に住んでいます。最近は Go, Ruby, Rails, Kubernetes, GCP, Datadog あたりをしていますがもっといろいろやりたい!

サイト案内

開発環境の紹介

プライバシーポリシー

tools.gomiba.co

サイト内検索

アーカイブ

2024/04 (7) 2024/03 (4) 2024/01 (3)

2023/12 (1) 2023/11 (3) 2023/10 (1) 2023/09 (1) 2023/08 (2) 2023/05 (4) 2023/04 (4) 2023/03 (4) 2023/02 (2) 2023/01 (1)

2022/12 (1) 2022/11 (4) 2022/10 (3) 2022/09 (2) 2022/08 (4) 2022/07 (5) 2022/06 (4) 2022/05 (9) 2022/04 (8) 2022/03 (10) 2022/02 (21) 2022/01 (8)

2021/12 (11) 2021/11 (1) 2021/10 (4) 2021/09 (2) 2021/08 (1) 2021/07 (2) 2021/06 (5) 2021/05 (10) 2021/04 (1) 2021/03 (8) 2021/02 (12) 2021/01 (8)

2020/05 (2) 2020/04 (2) 2020/02 (2) 2020/01 (1)

2019/12 (3) 2019/11 (2) 2019/10 (5) 2019/09 (3) 2019/07 (6) 2019/06 (4) 2019/04 (3) 2019/01 (2)

2018/12 (6) 2018/10 (4) 2018/09 (6) 2018/08 (7) 2018/07 (16) 2018/06 (7) 2018/05 (7) 2018/04 (5) 2018/03 (3) 2018/02 (10) 2018/01 (6)

2017/12 (8) 2017/11 (6) 2017/10 (10) 2017/09 (12) 2017/08 (12) 2017/07 (3) 2017/06 (1) 2017/01 (4)

2016/12 (5) 2016/10 (3) 2016/09 (1) 2016/07 (2) 2016/06 (1) 2016/04 (1) 2016/02 (1) 2016/01 (2)

2015/12 (1) 2015/10 (1) 2015/09 (3) 2015/06 (1) 2015/01 (1)

2014/08 (2) 2014/07 (3) 2014/05 (1) 2014/01 (7)

2013/12 (2) 2013/11 (4) 2013/10 (1) 2013/09 (1) 2013/08 (3) 2013/07 (4) 2013/06 (5) 2013/05 (2) 2013/04 (7) 2013/03 (1)

kubectl で Service から Pod の紐づけを一発で確認したい

この記事は公開されてから1年以上経過しており、最新の内容に追従できていない可能性があります。

まとめとしては -o json | jq な感じで頑張るのがとりあえずは正解っぽい。

$ kubectl get -o json endpoints | jq '.items[] | [.metadata.name, ([.subsets[].addresses[].targetRef.name] | join(", "))] | @tsv' --raw-output
http-server    http-86d67d8d47-46fm8, http-86d67d8d47-x487n, http-86d67d8d47-nxgdx
http-server-dev    http-dev-58fc975f8f-z2lvw, http-dev-58fc975f8f-tjczr

シェルむけの関数を組んで kubectl にオプション渡せるようにしたら便利、かも、と思ってざっくり書いた。
あたらしく Service を作ったりときにセレクタミスってないかな?とかを見るのに便利、かも。

#!/bin/sh
function kube_lookup_pod() {
    kubectl get -o json endpoints $@ \
    | jq -r \
      '["Service", "Pods"], (.items[] | [.metadata.name, ([.subsets[].addresses[].targetRef.name] | join(", "))]) | @tsv' \
    | column -t
}
$ kube_lookup_pod --namespace foo-namespace
Service               Pods
http-server           http-86d67d8d47-46fm8,      http-86d67d8d47-x487n,      http-86d67d8d47-nxgdx
http-server-dev       http-dev-58fc975f8f-z2lvw,  http-dev-58fc975f8f-tjczr

そこまでの経緯もメモっておく。

Kubernetes の Service の一覧はこうやってみえるが、これはどれに紐付いているかが分からない。

$ kubectl get svc
NAME                   TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)             AGE
http-server            ClusterIP   None         <none>        9100/TCP            10h
http-server-dev        ClusterIP   None         <none>        9100/TCP            10h

ワンチャン describe なら Endpoint がでてくるので、ギリギリわかる。

$ kubectl describe svc http-server
Name:              http-server
Namespace:         foo-namespace
Labels:            <none>
Annotations:       <none>
Selector:          app=http-server
Type:              ClusterIP
IP:                None
Port:              http-server  9100/TCP
TargetPort:        9100/TCP
Endpoints:         10.108.30.146:9100,10.108.35.228:9100,10.108.54.229:9100
Session Affinity:  None
Events:            <none>

Endpoint も get できるがこのままではわからない。

$ kubectl get endpoints
NAME                   ENDPOINTS                                                              AGE
http-server            10.108.30.146:9100,10.108.35.228:9100,10.108.54.229:9100               10h
http-server-dev        10.108.30.149:9100,10.108.32.247:9100                                  10h

describe してもだめ。

$ kubectl describe endpoints http-server
Name:         http-server
Namespace:    foo-namespace
Labels:       <none>
Annotations:  <none>
Subsets:
  Addresses:          10.108.30.146,10.108.35.228,10.108.54.229
  NotReadyAddresses:  <none>
  Ports:
    Name         Port  Protocol
    ----         ----  --------
    http-server  9100  TCP

Events:  <none>

おもむろに -o json やったら実はデータとしては取れることがわかった。

$ kubectl get endpoints http-server -o json
{
    "apiVersion": "v1",
    "kind": "Endpoints",
    "metadata": {
        "name": "http-server",
        ...省略...
    },
    "subsets": [
        {
            "addresses": [
                {
                    ...省略...
                    "ip": "10.108.30.146",
                    "nodeName": ".......",
                    "targetRef": {
                        "kind": "Pod",
                        "name": "http-86d67d8d47-46fm8",
                        "namespace": "foo-namespace",
...省略...

あとは jq でがんばったら取れる。

$ kubectl get -o json endpoints | jq '.items[] | [.metadata.name, ([.subsets[].addresses[].targetRef.name] | join(", "))] | @tsv' --raw-output
http-server    http-86d67d8d47-46fm8, http-86d67d8d47-x487n, http-86d67d8d47-nxgdx
http-server-dev    http-dev-58fc975f8f-z2lvw, http-dev-58fc975f8f-tjczr

書いてるときに試行錯誤してたけど jq の使い方について学びを得た

  • Object ならキーを選んで、配列ならインデックスを選ぶか [] で全選択して、というのをパイプしたら概ね抽出できる
  • join は配列に対してしか使えないので、一覧が出てきそうなやつでも配列にしてからパイプする
  • フラットな配列なら末尾で @csv や @tsv すると便利、そのあと column にパイプするとさらに便利
  • そのときは --raw-output オプションもセットでやると OK
  • あとはマニュアルとにらめっこ jq Manual (development version)