Querying pods by multiple labels - kubernetes

I need to retrieve a list of pods by selecting their corresponding labels.
When the pods have a simple label app=foo, k8s-app=bar, the selection is quite easy:
kubectl get po -l 'app in (foo), k8s-app in (bar)'
The complexity comes with labels that contain special characters, for example: app.kubernetes.io/name=foo
So when I query only this label, I don't have a problem, but if I try to add this label to the existing query, it will end by returning no resources were found.
kubectl get po -l app.kubernetes.io/name=foo,app=bar
kubectl get po -l 'app.kubernetes.io/name in (foo), app in (bar)'
Any idea how can I join the two labels in a single query?

Currently, Kubernetes does not support OR in label selectors. You can only OR different values of the same label (like kubectl get pods -l 'app in (foo,bar)').
See also How can the OR selector be used with labels in Kubernetes?

You can use below command for retrieving a list of pods by selecting their corresponding labels.
kubectl get pods --selector app=foo,k8s-app=bar

Related

How to get the full name of a pod by both its creation date and part of its name?

In my namespace, I have several pods named with the same prefix, followed by the random string. There are also other pods, named differently. The result of kubectl get pods would look something like this:
service-job-12345abc
service-job-abc54321
other-job-54321cba
I need to find the nameof the most recently created pod starting with "service-job-".
I found this thread, which helps getting the name of the most recent pod in general. This one gets me the complete names of pods starting with a specific prefix.
What I struggle with is combining these two methods. With each one, I seem to lose the information I need to perform the other one.
Note: I am not an administrator of the cluster, so I cannot change anything about the naming etc. of the pods. The pods could also be in any possible state.
This works as you expect:
kubectl get pods --sort-by=.metadata.creationTimestamp --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}' | grep service-job- | head -1

How can i use kubectl labelselector with or contidion?

I want to get pod with label
app=vovo
component=db
When i get a pod with label app=vovo and component=db, i can get the result with below command.
kubectl get pod -l app=vovo,component=db
However, when i want to get the result
app=vovo or component=db
How can i get the result with one kubectl command?
OR operations for label selection is not supported as per the documentation
Caution: For both equality-based and set-based conditions there is no
logical OR (||) operator. Ensure your filter statements are structured
accordingly
Here is a close hack you could do:
kubectl get pod -l app=volvo && kubectl get pod -l component=db --no-headers
This will run two kubectl queries for two different labels.

How to get the custom attributes in Kubernetes?

How to list the current deployments running in Kubernetes with custom columns displayed as mentioned below:
DEPLOYMENT CONTAINER_IMAGE READY_REPLICAS NAMESPACE
The data should be sorted by the increasing order of the deployment name.
Look a the -o custom-columns feature. https://kubernetes.io/docs/reference/kubectl/overview/#custom-columns shows the basics. The hard one would be container_image, since a pod can contain more than one, but assuming you just want the first, something like .spec.template.containers[0].image? Give a shot and see how it goes.
Command to get the custom columns as per the question:
Kubectl get deployments -o custom-columns=DEPLOYMENT:.metadata.name,CONTAINER_IMAGE:..image,READY_REPLICAS:..replicas,NAMESPACE:..namespace

Get Ready status using kubectl -o=jsonpath

I was trying to get the Ready status of pods by using -o=jsonpath.
To be more clear of what I want, I would like to get the value 1/1 of the following example using -o=jsonpath.
NAME READY STATUS RESTARTS AGE
some_pod 1/1 Running 1 34d
I have managed to get some information such as the pod name or namespace.
kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"\n"}{.metadata.namespace}{"\t"}{.metadata.name}{"\t"}{end}'
And I get somthing like:
some_namespace1 pod_name1
However, I don't know how to get the Ready status. What I would like to have is an aoutput similar to this:
some_namespace1 pod_name1 1/1
I know I can use bash commands like cut:
kubectl get pods --all-namespaces| tail -1 | cut -d' ' -f8
However, I would like to get it by using kubectl
You can get all the pods status using the following command:
kubectl get pods -o jsonpath={.items[*].status.phase}
Similar commands you can use for the name
kubectl get pods -o jsonpath={.items[*].metadata.name}
EDIT:
You need to compare the .status.replicas and .status.readyReplicas to get how many ready replicas are there.
I think this isn't directly reported in the Kubernetes API.
If you kubectl get pod ... -o yaml (or -o json) you'll get back an object matching a List (not included in the API docs) where each item is a Pod in the Kubernetes API, and -o jsonpath values follow that object structure. In particular a PodStatus has a list of ContainerStatus, each of which may or may not be ready, but the API itself doesn't return the counts as first-class fields.
There are a couple of different JSONPath implementations. I think Kubernetes only supports the syntax in the Kubernetes documentation, which doesn't include any sort of "length" function. (The original JavaScript implementation and a ready Googlable Java implementation both seem to, with slightly different syntax.)
The best I can come up with playing with this is to report all of the individual container "ready" statuses
kubectl get pods \
-o $'jsonpath={range .items[*]}{.metadata.name}\t{.status.containerStatuses[*].ready}\n{end}'
($'...' is bash/zsh syntax) but this still requires some post-processing to get back the original counts.

What is the jsonpath for Kubernetes POD status?

I couldn't find the status jsonpath when using kubectl. The pod json has a status field but it is an array.
kubectl get pods --column=Status:.status[*]
There are several elements in the array, how can I select the one for real pod status?
One must enable jsonpath output, via kubectl get pods --output="jsonpath={.status}", not via --column
The results were an array because as written kubectl is returning all Pods that are in the current namespace. To get a singular Pod status, qualify your request with:kubectl get pod $the_pod_name_here --output="jsonpath={.status}"if you really do want the status of all pods, then --output="jsonpath={.items[*].status}" is likely the syntax you were seeking