Jack Moore

Email: jack(at)jmoore53.com
Project Updates

Kubernetes and JQ!

21 Feb 2022 » kubernetes, golang, programming, software

Storage on Nodes

I needed a way to look at the storage available on each node and how much disk space was being used for docker images on each node. Below is what I came up with:

# Get ephemeral-storage on all nodes
k get nodes -o json | jq '.items[] | (.metadata.name, .status.allocatable."ephemeral-storage")'

# Get Nodes and Image sizes
k get nodes -o json | jq '.items[] | (.metadata.name, .status.images[].sizeBytes)'

# Get All the storage in the cluster
k get nodes -o json | jq '[.items[].status.images[].sizeBytes] | add'

# Get Bytes in use of images on a specific node
k get nodes -o json | jq '[.items[0].status.images[].sizeBytes] | add'

# Get Nodes and total image sizes
# This is the most valuable one and shows gb
k get nodes -o json | jq '.items[] | (.metadata.name, (([.status.images[].sizeBytes] | add)/1000000000|tostring) + "gb")'

# The Best
k get nodes -o json | jq '.items[] | (.metadata.name, (([.status.images[].sizeBytes] | add)/1000000000|tostring|(split(".")[:2] | [.[0], .[1][:2]])| join(".")) + "gb")'

Get All LoadBalancer IPs

# Get the IPs
k get svc --all-namespaces -o json | jq .items[].status.loadBalancer.ingress[0].ip | grep -v -e '^$' -e null | sort

# Get the Service and the IP Address (unsorted, split with `:`)
k get svc --all-namespaces -o json | jq '.items[] | select(.status.loadBalancer.ingress[0].ip) | ([.metadata.name, .status.loadBalancer.ingress[0].ip]) | join(":")'

# Get the Service and IP Address and sort based on IP Address:
k get svc --all-namespaces -o json | jq '.items[] | select(.status.loadBalancer.ingress[0].ip) | ([.metadata.name, .status.loadBalancer.ingress[0].ip]) | join(" ")' | sort -k 2 -V

Listing all pods in JSON Format

# All pods and there namespaces in sorted order
k get pods --all-namespaces --output json | jq -c -S '.items[] | {name: .metadata.name, namespace: .metadata.namespace}'
© Jack Moore - This site was last built Fri 30 Aug 2024 12:31:24 PM EDT