LoginSignup
10
6

More than 3 years have passed since last update.

[小ネタ]kubectl の jsonpath を使って Node から ExternalIP を取得する

Last updated at Posted at 2019-03-05

JSONPath Support

kubectl で結果を取得するときに JSONPath を使って特定の内容のみ取得する事ができる。

# node 一覧
$kubectl get nodes
NAME                                               STATUS    ROLES     AGE       VERSION
ip-172-31-19-51.ap-northeast-1.compute.internal    Ready     <none>    3d        v1.11.5
ip-172-31-22-177.ap-northeast-1.compute.internal   Ready     <none>    2d        v1.11.5
ip-172-31-28-5.ap-northeast-1.compute.internal     Ready     <none>    3d        v1.11.5
ip-172-31-4-131.ap-northeast-1.compute.internal    Ready     <none>    3d        v1.11.5

# 特定ノード情報
$kubectl get node ip-172-31-19-51.ap-northeast-1.compute.internal
NAME                                              STATUS    ROLES     AGE       VERSION
ip-172-31-19-51.ap-northeast-1.compute.internal   Ready     <none>    3d        v1.11.5

ここから色々やってみる

取れる情報を確認する

-o=jsonオプションを付与してどのような JSON の情報が取得できるか確認する。

$kubectl get node ip-172-31-19-51.ap-northeast-1.compute.internal -o=json
{
    "apiVersion": "v1",
    "kind": "Node",
    "metadata": {
        "annotations": {
            "node.alpha.kubernetes.io/ttl": "0",
            "volumes.kubernetes.io/controller-managed-attach-detach": "true"
        },
        "creationTimestamp": "2019-03-01T08:08:50Z",
        "labels": {
            "beta.kubernetes.io/arch": "amd64",
            "beta.kubernetes.io/instance-type": "t3.medium",
            "beta.kubernetes.io/os": "linux",
            "failure-domain.beta.kubernetes.io/region": "ap-northeast-1",
            "failure-domain.beta.kubernetes.io/zone": "ap-northeast-1a",
            "kubernetes.io/hostname": "ip-172-31-19-51.ap-northeast-1.compute.in
ternal"
        },
        "name": "ip-172-31-19-51.ap-northeast-1.compute.internal",
        "resourceVersion": "9806239",
        "selfLink": "/api/v1/nodes/ip-172-31-19-51.ap-northeast-1.compute.intern
al",
        "uid": "3f388db4-3bf9-11e9-a297-068bea14a4ce"
    },
    "spec": {
        "providerID": "aws:///ap-northeast-1a/i-xxxx"
    },
    "status": {
        "addresses": [
            {
                "address": "172.31.19.51",
                "type": "InternalIP"
            },
            {
                "address": "52.194.84.186",
                "type": "ExternalIP"
            },
            {
                "address": "ip-172-31-19-51.ap-northeast-1.compute.internal",
                "type": "InternalDNS"
            },
            {
                "address": "ec2-52-194-84-186.ap-northeast-1.compute.amazonaws.c
om",
                "type": "ExternalDNS"
            },
            {
                "address": "ip-172-31-19-51.ap-northeast-1.compute.internal",
                "type": "Hostname"
            }
        ],
        "allocatable": {
            "cpu": "2",
            "ephemeral-storage": "19316009748",
(以下略)

上記より取得したい情報がどのように辿れるか、確認する。
今回の場合、以下の順番で探索することで取得できる事が分かる。

  • status
  • addresses[1]
  • address

一つの Node から ExternalIP を取ってみる

先程の JSON で確認した探索順番を -o=jsonpath で指定する。

$kubectl get node ip-172-31-19-51.ap-northeast-1.compute.internal -o=jsonpath='{.status.addresses[1].address}'
52.194.84.186%

ただし、配列を決め打ちしているので本当に毎回これで取れるのかは怪しい。

Operators

[?()] Filter expression. Expression must evaluate to a boolean value.

フィルタがあるようだ。
使えるオペレーターの記載もある

Operators

使ってみる。

$ kubectl get node ip-172-31-19-51.ap-northeast-1.compute.internal -o=jsonpath='{.status.addresses[?(@.type=="ExternalIP")].address}'
52.194.84.186%

取れた。

複数の Node から ExternalIP を取得する

特定 Node だけでなく、すべての Node の ExternalIP を取得する。
これについては kubectl Cheat Sheetに情報があり、以下で取得出来る。

$kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'

kubectl get nodes -o=jsonを見るとそれぞれの Node の情報が items 配下にあるのでそれを回して取ってくるという流れの模様。

kubectl Cheat Sheet に以下のようなコメントが有るのは面白い

"jq" command useful for transformations that are too complex for jsonpath, it can be found at https://stedolan.github.io/jq/

いくつかの例がkubectl Cheat Sheetにはあるのでここらへんを見つつ、実際の JSON を見てどのように組むのか調査する方針が良さそう

10
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
6