How to print InternalIP of nodes using jq in k8s?

11/6/2019

I want to print the internal IP of all nodes in the same line separated by a space using jq in k8s. How can I do this?

Using jsonpath I can filter using .addresses[?(@.type=="InternalIP")]. How to achieve the same with jq?

-- Naxi
jq
kubernetes

2 Answers

11/6/2019

You can achieve this using the following command

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

Checkout kubectl Cheat sheet for more examples

-- Shawlz
Source: StackOverflow

11/6/2019

You could use select and pipe to achieve desired output.

below command shows the internal ip separated by new-line

kubectl get nodes -o json | jq '.items[].status.addresses[] | select(.type=="InternalIP") | .address'

for space separated internal-ips:

kubectl get nodes -o json | jq '.items[].status.addresses[] | select(.type=="InternalIP") | .address' | tr -d '\"' | tr '\n' ' '
-- Abu Hanifa
Source: StackOverflow