Get–Containers - anongitmous/k8sShell GitHub Wiki

Get containers from a pod.

Syntax

Get-Containers

-Pods <Mtf.Kubernetes.Models.V1K8sPod[]>
[<K8sShellFilterShow>]
[<CommonParameters>]

Description

This cmdlet takes either by pipeline or via an explicit argument, one or more pod objects, and for each returns the container objects that the pod encapsulates.

While the most typical use case will involve piping pod objects from a single cluster into this cmdlet, because the invocation configuration that was active during retrieval of a pod is saved with the object, it is quite possible to collect pods from a variety of clusters and pipe them all as a single collection into this cmdlet.

This is essentially a utility cmdlet: what it achieves can be achieved via a PowerShell script that manipulates the properties and methods of the Mtf.Kubernetes.Models.V1K8sPod output of Get–Pods.

See the example output.

Examples

ℹ️ See A Note about the Examples

1. Get container objects for all pods

Get-Pods | Get-Containers

  • Will display the container info for every pod returned by Get-Pods.
  • Using aliasing, the above can be shortened to
    ksgp | ksgc

2. Dump container object structure

Get-Pods | Get-Containers -ShowStructure

  • Dumps the structure of the container object.
  • See details for and example output of ShowStructure.
  • Using aliasing, the above can be shortened to
    ksgp | ksgc -ShowStructure

3. Get containers as JSON

Get-Pods | Get-Containers -AsJson -NoNulls

  • The collection of container objects will be output in JSON format with those name/value pairs with a null value being skipped.
  • See as AsJson.
  • Using aliasing, the above can be shortened to
    ksgp | ksgc -AsJson -NoNulls

4. Only get containers that match a certain build sha

Get-Pods | Get-Containers -Filter "$.item[*].env[?(@.name=='BUILD_SHA' && @.value=='12eeu897')]"

  • Only those container objects which match the filter condition will be returned to the client.
  • Using aliasing, the above can be shortened to
    ksgp | ksgc -F "$.item[*].env[?(@.name=='BUILD_SHA' && @.value=='12eeu897')]"

5. Exec into every container

Get-Pods | Get-Containers | Start-KubectlExec

  • This will open a new console window and exec into every container that is pipelined to the Start–KubectlExec cmdlet.
  • Using aliasing, the above can be shortened to
    ksgp | ksgc | ksske
  • To avoid exec'ing into potentially hundreds of pods/containers, the pod and container objects can make use of arbitrarily complex filtering:
    Get-Pods | ? {<optional pod filter>} | Get-Containers | ? {<optional container filter>} | Start-KubectlExec

6. List the unique container images across all namespaces

Get-Pods | Get-Containers | % {$_.GetK8s().Image} | Sort-Object | Get-Unique

  • Using aliasing, the above can be shortened to
    ksgp | ksgc | % {$_.GetK8s().Image} | sort | unique
  • The equivalent Linux-only example from here is:
    kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].image}" | tr -s '[:space:](/anongitmous/k8sShell/wiki/:space:)' '\n' | sort | uniq -c.

7. Copy all files from C:\src to home/dest on every matching container

Get-Pods | Get-Containers | Invoke-CopyLocalToRemote -Source 'C:\src' -Destination 'home/dest'

  • Using aliasing, the above can be shortened to
    ksgp | ksgc | ksclr -s C:\src -d home/dest
  • To avoid copying the source files into potentially hundreds of pods/containers, the pod and container objects can make use of arbitrarily complex filtering:
    Get-Pods | ? {<optional pod filter>} | Get-Containers | ? {<optional container filter>} | Invoke-CopyLocalToRemote -Source 'C:\src' -Destination 'home/dest'

8. Copy all files from home/src on every matching container to C:\dest

Get-Pods | Get-Containers | Invoke-CopyRemoteToLocal -Destination 'C:\dest' -Source 'home/src'

  • Using aliasing, the above can be shortened to
    ksgp | ksgc | kscrl -s home/src -d C:\dest
  • To avoid copying the source files from potentially hundreds of pods/containers, the pod and container objects can make use of arbitrarily complex filtering:
    Get-Pods | ? {<optional pod filter>} | Get-Containers | ? {<optional container filter>} | Invoke-CopyRemoteToLocal -Destination 'C:\dest' -Source 'home/src'

Parameters

-Pods

  • The pod(s) for which container info is to be returned.
Type: Mtf.Kubernetes.Models.V1K8sPod[]
Aliases:
Position: 0
Default value: None
Accept pipeline input: True
Accept wildcard characters: False

Outputs

Mtf.Kubernetes.Models.V1K8sContainer
Mtf.Kubernetes.Models.V1K8sContainerWithJob
Mtf.Kubernetes.Models.V1K8sContainerWithNode
Mtf.Kubernetes.Models.V1K8sContainerWithService

Example

Running the following:
Get-Pods | Get-Containers | sort Restarts -Descending | ft
Returns all matching containers sorted by restart count.

image

Notes

K8sShell includes the following alias for Get-Containers:

  • ksgc

This cmdlet cannot be run by itself. It must have pod objects as input.

Related