Get–Pods - anongitmous/k8sShell GitHub Wiki

Get pod objects.

Syntax

Get-Pods

[-CronJobs <Mtf.Kubernetes.Models.V1K8sCronJob[]>]
[-Jobs <Mtf.Kubernetes.Models.V1K8sJob[]>]
[-Nodes <Mtf.Kubernetes.Models.V1K8sNode[]>]
[-Services <Mtf.Kubernetes.Models.V1K8sService[]>]
[<K8sShellCommonParameters>]
[<CommonParameters>]

Description

This retrieves pod objects from the cluster.

It accepts cron job, job, node, and service objects as pipelined or command line input and the resulting object contains the input object incorporated into the pod object.

For example, piping node objects into this cmdlet will mean that for each node, the corresponding pods on that node will be returned.

In such a case, the object returned will not only have the pod object, but also the node object, and the default output will show the relationship between the two.

The same applies additionally to cron job, job, and service objects.

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

See the example output.

Examples

ℹ️ See A Note about the Examples

⚠️ If the default namespace is $null or '', then all matching pods from all namespaces will be returned.

1. Get all of the pods per the currently-defined session variables

Get-Pods

  • For the currently active cluster configuration, return all pods in the default namespace for the cluster configuration.
  • Using aliasing, the above can be shortened to
    ksgp

2. Get all pods that match the given node name and pod status
ℹ️ See supported field selectors

Get-Pods -FieldSelector 'spec.nodeName=ip-172-28-34-132.us-west-3.compute.internal,status.phase=Running'

  • Using aliasing, the above can be shortened to
    ksgp -fs 'spec.nodeName=ip-172-28-34-132.us-west-3.compute.internal,status.phase=Running'

3. Get the pod named 'foo'
ℹ️ See supported field selectors

Get-Pods -FieldSelector 'metadata.name=foo'

  • Using aliasing, the above can be shortened to
    ksgp -fs 'metadata.name=foo'

4. Get the pod with the label with name 'app' and value of 'contoso'

Get-Pods -LabelSelector 'app=contoso'

  • Using aliasing, the above can be shortened to
    ksgp -ls 'app=contoso'

5. Save the logs for every pod with a label key of 'app' and value of 'contoso'

Get-Pods | Where-Object {$_.LabelKeyMatches('app') -match 'contoso'} | Save-Logs

  • Using aliasing, the above can be shortened to
    ksgp | ? {$_.LabelKeyMatches('app') -match 'contoso'} | kssl

6. Show the logs for every pod with a label key of 'app' and value of 'contoso'

Get-Pods | Where-Object {$_.LabelKeyMatches('app') -match 'contoso'} | Save-Logs

  • If the default namespace is $null or '', then all matching pods from all namespaces will be returned.

7. For every node, return the pods that are deployed on it

Get-Nodes | Get-Pods

  • Using aliasing, the above can be shortened to
    ksgn | ksgp

8. For every service, return the corresponding pods

Get-K8sServices | Get-Pods

  • Using aliasing, the above can be shortened to
    ksgsvc | ksgp

9. For every job, return the corresponding pods

Get-K8sJobs | Get-Pods

  • Using aliasing, the above can be shortened to
    ksgj | ksgp

10. Save the logs for every container in every pod that enters the 'running' phase

Get-Pods -Watch Added | ? {$_.Phase -match 'running'} | Get-Containers | Save-Logs

  • Using aliasing, the above can be shortened to
    ksgp -w Added | ? {$_.Phase -match 'running'} | ksgc | kssl
  • To display rather than save the logs, substitute Show–Logs for Save–Logs

11. Save the logs for every container for every job that starts and reaches the running phase

Get-Pods -LabelSelector 'job-name' -Watch Modified | ? {$_.Condition -match 'Ready:False:PodCompleted' -and $_.Phase -match 'running'} | Get-Containers | Save-Logs

  • Using aliasing, the above can be shortened to
    ksgp -ls 'job-name' -w Modified | ? {$_.Condition -match 'Ready:False:PodCompleted' -and $_.Phase -match 'running'} | ksgc | kssl
  • To display rather than save the logs, substitute Show–Logs for Save–Logs
  • To focus on specific job types, names, etc., modify the where clause or supply more criteria to the label selector, add a field selector, filter on specific containers, and so forth.

Parameters

-CronJobs

  • The output from a call to Get–CronJobs.
  • ⚠️ This is an experimental feature which has not been fully tested and depends upon the following heuristic:
    • Validate that metadata.name of the pod starts with the metadata.name of the cronjob
    • Validate that there is a label with a key of 'job-name' and a value that starts with the metadata.name of the cronjob
    • Validate that there is an owner reference for the pod of kind 'job'
    • Validate that there is an owner reference with a name that starts with the metadata.name of the cronjob
    • Validate that the number of containers in the pod spec is the same as the number of containers in the cronjob's spec.jobtemplate.spec.template.spec.containers
    • Validate that the image for each container in the pod spec is the same as the image in the cronjob spec.jobtemplate.spec.template.spec.containers
    • Validate that the name of each container in the pod spec is the same as the name in the cronjob spec.jobtemplate.spec.template.spec.containers
Type: Mtf.Kubernetes.Models.V1K8sCronJob[]
Aliases:
Position: Named
Default value: None
Accept pipeline input: True
Accept wildcard characters: False

-Jobs

Type: Mtf.Kubernetes.Models.V1K8sJob[]
Aliases:
Position: Named
Default value: None
Accept pipeline input: True
Accept wildcard characters: False

-Nodes

Type: Mtf.Kubernetes.Models.V1K8sNode[]
Aliases:
Position: Named
Default value: None
Accept pipeline input: True
Accept wildcard characters: False

-Services

Type: Mtf.Kubernetes.Models.V1K8sService[]
Aliases:
Position: Named
Default value: None
Accept pipeline input: True
Accept wildcard characters: False

Outputs

Mtf.Kubernetes.Models.V1K8sPod
Mtf.Kubernetes.Models.V1K8sPodWatch
Mtf.Kubernetes.Models.V1K8sPodWithCronJob
Mtf.Kubernetes.Models.V1K8sPodWithCronJobWatch
Mtf.Kubernetes.Models.V1K8sPodWithJob
Mtf.Kubernetes.Models.V1K8sPodWithJobWatch
Mtf.Kubernetes.Models.V1K8sPodWithNode
Mtf.Kubernetes.Models.V1K8sPodWithNodeWatch
Mtf.Kubernetes.Models.V1K8sPodWithService
Mtf.Kubernetes.Models.V1K8sPodWithServiceWatch

Example

(default Get-Pods output)
image

Supported Field Selectors

📝 - As of 06/2023

  • metadata.namespace
  • metadata.name
  • spec.nodeName
  • spec.restartPolicy
  • spec.schedulerName
  • spec.serviceAccountName
  • status.phase
  • status.podIP
  • status.nominatedNodeName

Notes

K8sShell includes the following alias for Get-Pods:

  • ksgp

Related