1

I know that I can perform this command to see all the pods that have a certain label : kubectl get pods -l importance=normal

Is there a command that would allow me to get the deployments that create the pods with the label importance=normal?

I want to find all deployments what have the label importance=normal found in the spec/template/metadata/labels (see my deployment yaml below).

I found this link(https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/#list-container-images-filtering-by-pod-label) and started to create the following command but I could not get it to work -

kubectl get deployments -namespace prod -o jsonpath='{range .items[*]}{"\n"}{.spec.template.metadata.labels}{":\t"}{", "}{end}{end}' |\ sort```

My deployment yaml looks like the following:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    app.kubernetes.io/managed-by: Helm
    deployment.kubernetes.io/revision: "11"
    meta.helm.sh/release-name: service-qa
    meta.helm.sh/release-namespace: prod
    source: https://gitlab.services.com/service
  creationTimestamp: "2019-04-07T12:51:03Z"
  generation: 22
  labels:
    app.kubernetes.io/managed-by: Helm
    chart: service-0.1.0
  name: service-deployment
  namespace: qa
  resourceVersion: "577488289"
  uid: cd2fdeb4-5933-11e9-ad6d-02211a607320
spec:
  progressDeadlineSeconds: 600
  replicas: 2
  revisionHistoryLimit: 3
  selector:
    matchLabels:
      app: service-selector
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      annotations:
        kubectl.kubernetes.io/restartedAt: "2022-11-13T08:19:36-06:00"
      creationTimestamp: null
      labels:
        app: service-selector
        importance: normal
        version: current
0

1 Answer 1

3

The easiest solution is probably using jq. To get a list of Deployments that deploy Pods with the label importance: normal, you could do this:

kubectl get deploy -o json |
jq '.items[]|select(.spec.template.metadata.labels.importance == "normal")'

I would argue that you should rewrite your manifests so that the Deployment shares labels with the Pod so that you can just run:

kubectl get deploy -l importance=normal

That's much simpler. Using a tool like kustomize can simplify the work involved in keeping these labels consistent.

1
  • Perfect. Thanks so much!!! I will look into kustomize also Commented Jan 5, 2023 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.