Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubectl command to check if namespace is ready

Tags:

kubernetes

I would like to know if there is a command in kubernetes that returns true if all resources in a namespace have the ready status and false otherwise.

Something similar to this (ficticious) command:

kubectl get namespace <namespace-name> readiness

If there is not one such command, any help guiding me in the direction of how to retrieve this information (if all resources are ready in a given namespace) is appreciated.

like image 469
user2074945 Avatar asked Feb 18 '19 10:02

user2074945


People also ask

How can I check my namespace in Kubernetes?

You may have to use kubectl config view --minify | grep namespace: to get current namespace.

How do I check my kubectl status?

Using kubectl describe pods to check kube-system If the output from a specific pod is desired, run the command kubectl describe pod pod_name --namespace kube-system . The Status field should be "Running" - any other status will indicate issues with the environment.

How do I list all namespaces in kubectl?

To list the existing namespaces in a cluster 'kubectl get namespace' command is used. After executing the command, the following output will be generated: Observe that the Kubernetes object starts with four initial namespaces: Default, kube-node-lease, kube-public, and kube-system.

How do I find my pod namespace?

We can list all of the pods, services, stateful sets, and other resources in a namespace by using the kubectl get all command. As a result, you may use this command to see the pods, services, and stateful sets in a specific namespace.


2 Answers

There is no such command. try the below command to check all running pods

kubectl get po -n <namespace> | grep 'Running\|Completed'

below command to check the pods that are failed,terminated, error etc.

kubectl get po -n <namespace> | grep -v Running |grep -v Completed
like image 192
P Ekambaram Avatar answered Oct 10 '22 01:10

P Ekambaram


With the following sh scirpt, it is possible to check if all pods in a given namespace are running:

allRunning() {
    podStatus=$(kubectl get pods -n <namespace> -o=jsonpath='{range .items[*]}{.status.conditions[?(@.type=="ContainersReady")].status}{"\n"}{end}')
    for elem in $podStatus
    do
        echo $elem
        if [ $elem != "Running" ]
        then
            return 0
        fi
    done
    return 1
}
allRunning
allAreRunning=$

if [ $allAreRunning == 1 ] 
then
    echo "all are running"
else
    echo "not ready"
fi

EDIT 1: As suggested in the comments, pods do not seem to be the correct resource type to check readiness against. Thus, I suggest the following command for querying readiness which is based on deployment availability:

kubectl get deployments -o=jsonpath='{range .items[*]}{.status.conditions[?(@.type=="Available")].status}{"\n"}{end}'
like image 40
user2074945 Avatar answered Oct 10 '22 01:10

user2074945