2017-02-09 01:03:17 +00:00
|
|
|
---
|
2017-06-08 20:29:52 +00:00
|
|
|
title: List All Container Images Running in a Cluster
|
2018-05-05 16:00:51 +00:00
|
|
|
content_template: templates/task
|
2018-05-20 03:40:51 +00:00
|
|
|
weight: 100
|
2017-02-09 01:03:17 +00:00
|
|
|
---
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture overview %}}
|
2017-02-09 01:03:17 +00:00
|
|
|
|
|
|
|
This page shows how to use kubectl to list all of the Container images
|
|
|
|
for Pods running in a cluster.
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2017-02-09 01:03:17 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture prerequisites %}}
|
2017-02-09 01:03:17 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
2017-02-09 01:03:17 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2017-02-09 01:03:17 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture steps %}}
|
2017-02-09 01:03:17 +00:00
|
|
|
|
|
|
|
In this exercise you will use kubectl to fetch all of the Pods
|
|
|
|
running in a cluster, and format the output to pull out the list
|
|
|
|
of Containers for each.
|
|
|
|
|
|
|
|
## List all Containers in all namespaces
|
|
|
|
|
|
|
|
- Fetch all Pods in all namespaces using `kubectl get pods --all-namespaces`
|
|
|
|
- Format the output to include only the list of Container image names
|
|
|
|
using `-o jsonpath={..image}`. This will recursively parse out the
|
|
|
|
`image` field from the returned json.
|
|
|
|
- See the [jsonpath reference](/docs/user-guide/jsonpath/)
|
|
|
|
for further information on how to use jsonpath.
|
|
|
|
- Format the output using standard tools: `tr`, `sort`, `uniq`
|
|
|
|
- Use `tr` to replace spaces with newlines
|
|
|
|
- Use `sort` to sort the results
|
|
|
|
- Use `uniq` to aggregate image counts
|
|
|
|
|
|
|
|
```sh
|
|
|
|
kubectl get pods --all-namespaces -o jsonpath="{..image}" |\
|
|
|
|
tr -s '[[:space:]]' '\n' |\
|
|
|
|
sort |\
|
|
|
|
uniq -c
|
|
|
|
```
|
|
|
|
|
|
|
|
The above command will recursively return all fields named `image`
|
|
|
|
for all items returned.
|
|
|
|
|
|
|
|
As an alternative, it is possible to use the absolute path to the image
|
|
|
|
field within the Pod. This ensures the correct field is retrieved
|
2017-08-19 02:54:04 +00:00
|
|
|
even when the field name is repeated,
|
2017-02-09 01:03:17 +00:00
|
|
|
e.g. many fields are called `name` within a given item:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].image}"
|
|
|
|
```
|
|
|
|
|
|
|
|
The jsonpath is interpreted as follows:
|
|
|
|
|
|
|
|
- `.items[*]`: for each returned value
|
|
|
|
- `.spec`: get the spec
|
|
|
|
- `.containers[*]`: for each container
|
|
|
|
- `.image`: get the image
|
|
|
|
|
2018-06-12 23:17:28 +00:00
|
|
|
{{< note >}}
|
2017-04-06 02:17:05 +00:00
|
|
|
**Note:** When fetching a single Pod by name, e.g. `kubectl get pod nginx`,
|
2017-02-09 01:03:17 +00:00
|
|
|
the `.items[*]` portion of the path should be omitted because a single
|
|
|
|
Pod is returned instead of a list of items.
|
2018-06-12 23:17:28 +00:00
|
|
|
{{< /note >}}
|
2017-02-09 01:03:17 +00:00
|
|
|
|
|
|
|
## List Containers by Pod
|
|
|
|
|
|
|
|
The formatting can be controlled further by using the `range` operation to
|
|
|
|
iterate over elements individually.
|
|
|
|
|
|
|
|
```sh
|
|
|
|
kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}' |\
|
|
|
|
sort
|
|
|
|
```
|
|
|
|
|
|
|
|
## List Containers filtering by Pod label
|
|
|
|
|
|
|
|
To target only Pods matching a specific label, use the -l flag. The
|
|
|
|
following matches only Pods with labels matching `app=nginx`.
|
|
|
|
|
|
|
|
```sh
|
|
|
|
kubectl get pods --all-namespaces -o=jsonpath="{..image}" -l app=nginx
|
|
|
|
```
|
|
|
|
|
|
|
|
## List Containers filtering by Pod namespace
|
|
|
|
|
|
|
|
To target only pods in a specific namespace, use the namespace flag. The
|
|
|
|
following matches only Pods in the `kube-system` namespace.
|
|
|
|
|
|
|
|
```sh
|
|
|
|
kubectl get pods --namespace kube-system -o jsonpath="{..image}"
|
|
|
|
```
|
|
|
|
|
|
|
|
## List Containers using a go-template instead of jsonpath
|
|
|
|
|
|
|
|
As an alternative to jsonpath, Kubectl supports using [go-templates](https://golang.org/pkg/text/template/)
|
|
|
|
for formatting the output:
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
|
2017-02-09 01:03:17 +00:00
|
|
|
```sh
|
|
|
|
kubectl get pods --all-namespaces -o go-template --template="{{range .items}}{{range .spec.containers}}{{.image}} {{end}}{{end}}"
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
|
|
|
|
|
|
|
{{% capture discussion %}}
|
2017-02-09 01:03:17 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2017-02-09 01:03:17 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture whatsnext %}}
|
2017-02-09 01:03:17 +00:00
|
|
|
|
|
|
|
### Reference
|
|
|
|
|
|
|
|
* [Jsonpath](/docs/user-guide/jsonpath/) reference guide
|
|
|
|
* [Go template](https://golang.org/pkg/text/template/) reference guide
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
|
|
|
|
2017-02-09 01:03:17 +00:00
|
|
|
|