Silly Kubectl Trick #2: LIST RUNNING IMAGES
The standard output of kubectl get pods doesn't help to answer many questions. That's okay, though, because we have the custom-columns output format!
Join the DZone community and get the full member experience.Join For Free
Running Kubernetes in production means taking inventory. A LOT. Are any of our pods running that version of Ubuntu base image affected by the new CVE? Do we even use Alpine Linux anywhere? What versions of MySQL are we currently running (and where)? The standard output of
kubectl get pods doesn't help to answer any of these questions.
That's okay, though, because we have the
custom-columns output format!
Whew! That's a fingerful to type. If you'd like, you can put the details of the format in a file, and reference that file via the
custom-columns-file output format:
Doing so allows you to swap out selectors, like namespace and
-l label filters, while re-using the same format (and not having to retype it!)
We can do so much more than just list off names and namespaces. We can go so far as to list out the images in use by the first container of each pod:
Now we're talkin'!
Keep in mind that the tag reported is whatever was in the pod spec – if it is missing, you can assume
:latest, but that doesn't tell you much about the actual version in use. If you want anything more specific out of Kubernetes, you'll need to use the
status object, instead of
Kubernetes tracks the raw SHA256 checksum of each image it executes, in
status.containerStatuses[n].imageID, and the resolved image name and its tag in
.image. We use those two, above, to get a more complete picture of our running image inventory.
So far we've been using the
-th index, which limits us to the first container in any multi-container pods. We could instead use
kubectl will separate multiple values with commas. This gets unwieldy fast, especially with the
imageID fields, but it is there when you need it.
Pro Tip: Since we know that the only instance of a comma in our output will be to separate multiple image IDs, we can use
column (a POSIX utility) to reformat the table to ease readability:
It's also important to note that our above formats completely ignore any and all init containers, in case those are important to you.
Check Out the Video!
If you like that, check out this video adaptation, wherein I go a bit more in-depth, and touch on some stuff you can do with files holding all your sweet, sweet custom output formats.
Published at DZone with permission of James Hunt. See the original article here.
Opinions expressed by DZone contributors are their own.