Home > Mobile >  Can I use kubectl to find deployments that have affinities set?
Can I use kubectl to find deployments that have affinities set?

Time:05-24

I would like to validate, that deployments, which have Pod- and NodeAffinities ( AntiAffinity) are configured according to internal guidelines.

Is there a possibility to get deployments (or Pods) using kubectl and limit the result to Objects, that have such an affinity configured?

I have played around with the jsonpath output, but was unsuccessful so far.

CodePudding user response:

hope you are enjoying your Kubernetes journey !

If you need to use affinities (especially with preferredDuringSchedulingIgnoredDuringExecution (explications below)) and just want to just "find" deployments that actually have affinities, you can use this:

❯ k get deploy  -o custom-columns=NAME:".metadata.name",AFFINITIES:".spec.template.spec.affinity"
NAME                               AFFINITIES
nginx-deployment                   <none>
nginx-deployment-vanilla           <none>
nginx-deployment-with-affinities   map[nodeAffinity:map[preferredDuringSchedulingIgnoredDuringExecution:[map[preference:map[matchExpressions:[map[key:test-affinities1 operator:In values:[test1]]]] weight:1]] requiredDuringSchedulingIgnoredDuringExecution:map[nodeSelectorTerms:[map[matchExpressions:[map[key:test-affinities operator:In values:[test]]]]]]]]

Every <none> pattern indicates that there is no affinity in the deployment.

However, with affinities, if you want to get only the deployments that have affinities without the deployments that don't have affinities, use this:

❯ k get deploy  -o custom-columns=NAME:".metadata.name",AFFINITIES:".spec.template.spec.affinity" | grep -v "<none>"
NAME                               AFFINITIES
nginx-deployment-with-affinities   map[nodeAffinity:map[preferredDuringSchedulingIgnoredDuringExecution:[map[preference:map[matchExpressions:[map[key:test-affinities1 operator:In values:[test1]]]] weight:1]] requiredDuringSchedulingIgnoredDuringExecution:map[nodeSelectorTerms:[map[matchExpressions:[map[key:test-affinities operator:In values:[test]]]]]]]]

And if you just want the names of the deployments that have affinities, consider using this little script:

❯ k get deploy  -o custom-columns=NAME:".metadata.name",AFFINITIES:".spec.template.spec.affinity" --no-headers | grep -v "<none>" | awk '{print $1}'
nginx-deployment-with-affinities

But, do not forget that nodeSelector is the simplest way to constrain Pods to nodes with specific labels. (more info here: https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#affinity-and-anti-affinity). Also remember that (according to the same link) the requiredDuringSchedulingIgnoredDuringExecution type of node Affinity functions like nodeSelector, but with a more expressive syntax ! So If you don't need preferredDuringSchedulingIgnoredDuringExecution when dealing with affinities consider using nodeSelector !

After reading the above link, if you want to deal with nodeSelector you can use the same mechanic I used before:

❯ k get deploy  -o custom-columns=NAME:".metadata.name",NODE_SELECTOR:".spec.template.spec.nodeSelector"
NAME                       NODE_SELECTOR
nginx-deployment           map[test-affinities:test]
nginx-deployment-vanilla   <none>
  • Related