Wednesday, 7 June 2017

JSONPath Cheat Sheet

I'll keep adding to this post. JSONPath.sh is required.

  1. List all containers in a Kubernetes system Pod
  2. Get the Internal IP Address of all Kubernetes Nodes
  3. Show all the Volume Mounts for a Pod
  4. Modify the Number of Replicas in a Kubernetes Deployment

List all containers in a Kubernetes system Pod

Get the list of System Pods

$ kubectl get -n kube-system pods
NAME                                    READY     STATUS    RESTARTS   AGE
etcd-k8s1                               1/1       Running   0          26d
kube-apiserver-k8s1                     1/1       Running   1          26d
kube-controller-manager-k8s1            1/1       Running   0          26d
kube-dns-3913472980-mbxbg               3/3       Running   0          26d
kube-proxy-7r5p1                        1/1       Running   0          26d
kube-proxy-l6dq4                        1/1       Running   0          26d
kube-proxy-rsdbd                        1/1       Running   0          26d
kube-scheduler-k8s1                     1/1       Running   0          26d
kubernetes-dashboard-2039414953-bb1jx   1/1       Running   0          13d
weave-net-pfv92                         2/2       Running   0          22d
weave-net-tnwjt                         2/2       Running   35         22d
weave-net-xnfk9                         2/2       Running   34         22d


Find the names of all the containers in the weave-net-pfv92 Pod

$ kubectl get -n kube-system pods weave-net-pfv92 -o json \
    | JSONPath.sh '..containers[*].name' -b

weave
weave-npc

Get the Internal IP Address of all Kubernetes Nodes

 $ kubectl get nodes -o json | JSONPath.sh -b \
    '..addresses[?(@.type=="InternalIP")].address'
172.42.42.1
172.42.42.2
172.42.42.3

Show all the Volume Mounts for a Pod

$ kubectl get -n kube-system pods etcd-k8s1 -o json \
    | JSONPath.sh -j '..[volumeMounts,volumes]'

{
    "spec":
    {
        "containers":
        [
            {
                "volumeMounts":
                [
                    {
                        "mountPath":"/etc/ssl/certs",
                        "name":"certs"
                    },
                    {
                        "mountPath":"/var/lib/etcd",
                        "name":"etcd"
                    },
                    {
                        "mountPath":"/etc/kubernetes/",
                        "name":"k8s",
                        "readOnly":true
                    }
                ]
            }
        ],
        "volumes":
        [
            {
                "hostPath":
                {
                    "path":"/etc/ssl/certs"
                },
                "name":"certs"
            },
            {
                "hostPath":
                {
                    "path":"/var/lib/etcd"
                },
                "name":"etcd"
            },
            {
                "hostPath":
                {
                    "path":"/etc/kubernetes"
                },
                "name":"k8s"
            }
        ]
    }
}

Modify the Number of Replicas in a Kubernetes Deployment


The following "one-liner" will change 'spec.replicas' to 2 for hello-deployment:
$ kubectl get deployment hello-deployment -o json | \
    JSONPath.sh | \
    sed 's/\["spec","replicas"\].*/["spec","replicas"]\t2/' | \
    JSONPath.sh -p | \
    kubectl replace deployment hello-deployment -f -

deployment "hello-deployment" replaced
Or you could use a bash function and reuse at will.
Set the bash function:
$ change_replicas() { kubectl get deployment $1 -o json | \
JSONPath.sh | \
sed 's/\["spec","replicas"\].*/["spec","replicas"]\t'"$2"'/' | \
JSONPath.sh -p | \
kubectl replace deployment $2 -f - ;}
Then use it:
$ change_replicas hello-deployment 4
deployment "hello-deployment" replaced
$ change_replicas hello-deployment 1
deployment "hello-deployment" replaced



No comments:

Post a Comment