Kubectl Commands: Know your Kubernetes Cluster using kubectl commands(Viewing, finding Kubernetes resources)

What is kubectl? 

kubectl is a CLI(Command Line Interface) client which can be used to interact with Kubernetes. Kubectl command line tool can be used in single node or multi-node cluster.

Installing the Kubernetes client (kubectl): Download and install appropriate format of kubectl client, all you need to do is download it and put it on your path. 

Lets start with Kubectl commands to know about cluster and its building blocks. Generic Syntax to run kubectl commands from your terminal window:

kubectl [command] [TYPE] [NAME] [flags]


Please note
:
1. Result of commands executed below has been masked(**********) intentionally, Please focus on command and its details.  

2. clust_namespace1(is a pseudo namespace)
in my cluster where I am running all commands.

  1. To know about kubernetes cluster components and config: Use "get cluster-info" commnd.
    n0r0082@m-c02z31rnlvdt ~ % kubectl cluster-info     
    Kubernetes control plane is running at https://master.********.k8s.us.walmart.net
    KubeDNS is running at 
    https://master.********.k8s.us.walmart.net/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy KubeDNSUpstream is running at
    https://master.*********.k8s.us.walmart.net/api/v1/namespaces/*****kube-dns-upstream:dns/proxy kubernetes-dashboard is running at
    https://master.*********.k8s.us.walmart.net/api/v1/namespaces/*****/kubernetes-dashboard:http/proxy monitoring-grafana is running at
    https://master.*********.k8s.us.walmart.net/api/v1/namespaces/kube-system/*****/proxy node-local-dns is running at
    https://master.*********.k8s.us.walmart.net/api/v1/namespaces/kube-system/***/proxy

  2. Find all namespace in cluster: Use "get namespace" or "get ns" command
    n0r0082@m-c02z31rnlvdt ~ % kubectl get namespace          
    NAME                         STATUS   AGE
    allspark-****                Active   570d
    ampplus-*****                Active   390d
    **********-service           Active   143d
    ap-***-ims-***-na            Active   401d
    ap-****oom                   Active   578d
    
    
    or
    n0r0082@m-c02z31rnlvdt ~ % kubectl get ns

  3. Find all nodes in cluster: Use "get node" command to find running master and workers nodes in cluster. 
    n0r0082@m-c02z31rnlvdt ~ % kubectl get node
    NAME                               STATUS   ROLES                  AGE     VERSION
    cp2-********-vmss000000            Ready    control-plane,master   538d    v1.20.13
    cp2-********-vmss000001            Ready    control-plane,master   538d    v1.20.13
    istio2-****-*****-vmss00012x       Ready    istio,node             16d     v1.20.13
    worker2-****-*****-vmss000fdm      Ready    node                   16d     v1.20.13
    worker2-****-****-vmss000fdt       Ready    node                   16d     v1.20.13
    

  4. Find all pods in a cluster/namespace:  Use "get pods" or "get po" command to fetch all pods in given name space.
    Please note "-n <namespace_name>" is optional. In production env clusters are logically separated using namespace and resources are allocated accordingly.
    n0r0082@m-c02z31rnlvdt ~ % kubectl get pods -n clust_namespace1
    NAME                                              READY   STATUS      RESTARTS   AGE
    prometheus-prometheus-0                           3/3     Running     1          3d19h
    prometheus-prometheus-1                           3/3     Running     1          16d
    ***-configuration-***-67696bb6c4-9z2rv            1/1     Running     0          5h58m
    ***-configuration-***-67696bb6c4-qr67q            1/1     Running     0          16d
    scale-up-***-configuration-***-1645963200-424xg   0/1     Completed   0          5h59m
    scale-up-***-configuration-***-1645963200-cjttf   0/1     Completed   0          5h59m
    

    • The kubectl get pods command doesn’t list any labels by default, but you can see them by using the --show-labels switch. 
    • List all pods in all namespaces using switch "--all-namespaces"
      n0r0082@m-c02z31rnlvdt ~ % kubectl get pods --all-namespaces
      

  5. Find all pods in a cluster/namespace with labels: Resources in Kubernetes are attached with Labels. To list all pods with Labels use "--show-labels" flag. Labels are list of [key=value] paris separated by comma(",").
    n0r0082@m-c02z31rnlvdt ~ % kubectl get po --show-labels -n clust_namespace1
    NAME                                      READY   STATUS      RESTARTS   AGE     LABELS
    ****-configuration-***-67696bb6c4-9z2rv   1/1     Running     0          6h6m    
    app.kubernetes.io/managed-by=kConfig,[KEY=VALUE],version=v1 ****-app-***-***-6d899d45cc-qrvkm 1/1 Running 0 4d10h
    app.kubernetes.io/managed-by=kConfig,[KEY=VALUE],version=v1

  6. Find all pods in a cluster/namespace with specif Labels: Instead of listing all labels, if you’re only interested in certain labels, you can specify them with the -L switch and have each displayed in its own column.
      
    n0r0082@m-c02z31rnlvdt ~ % kubectl get po -L app.kubernetes.io/name,env -n clust_namespace1
    NAME                              READY  STATUS     RESTARTS  AGE   NAME          ENV
    ******-******-5697c54f4b-fjm9p    1/1    Running    0         6h25m  app1-config  stage
    ******-******-5697c54f4b-z9mpb    1/1    Running    0         12d    app2-config  prod
    

  7. Find pod details with custom columns: To display custom columns we can use "-o custom-columns".
    Lets find IP Address of pod, IP Address of Host and application version# deployed. Host and Pod IP are not exposed with regular kubectl get commands. We can retrieve it from metadata.
     
    n0r0082@m-c02z31rnlvdt ~ % kubectl get pods -l app=my-app-namne 
        -o custom-columns=NAME:.metadata.name,Pod-IP:.status.podIP,
    Host-IP:.status.hostIP,Version:.metadata.labels.'app\.kubernetes\.io/version'

    -n clust_namespace1 NAME Pod-IP Host-IP Version ************-54fb7b67fd-gczfq 11.16.2**.9* 10.33.18.** 1.0.2052

  8. Get pod details in verbose mode: Use "describe" command to fetch pod details in verbose mode.
    n0r0082@m-c02z31rnlvdt ~ % kubectl describe pods my-pod-name -n clust_namespace1
    Name:                 ******-****-*****-7fbd49bc47-ssdqx
    Namespace:            clust_namespace1
    Priority:             0
    Priority Class Name:  default
    Node:                 worker2-****-****-a2-vmss000i84/10.33.**.1**
    Start Time:           Fri, 25 Feb 2022 20:05:39 -0800
    Labels:               app=***-*****-*****-sync
    ......
    .......

  9. Get all services in cluster/namespace: Use "get services" or "get svc" command to fetch all running services.
    n0r0082@m-c02z31rnlvdt ~ % kubectl get svc -n clust_namespace1
    NAME                       TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)             AGE
    app-*****-stg**            ClusterIP   10.50.2**.2**           8080/TCP,7766/TCP   416d
    app-*****-prod**           ClusterIP   10.50.1**.1**           8080/TCP,7766/TCP   212d
    

  10. Get all endpoints (IP:Port) details for running application:
    n0r0082@m-c02z31rnlvdt ~ % kubectl get endpoints my_application_name -n clust_namespace1 
    NAME                    ENDPOINTS                           AGE
    my_application_name     11.17.43.54:7766,11.17.43.54:8080   479d
    
  11. To fetch all replicaset in cluster/namespace: Use "get rs" or "get replicaset" command.
    n0r0082@m-c02z31rnlvdt ~ % kubectl get rs -n clust_namespace1                                         
    NAME                                    DESIRED   CURRENT   READY   AGE
    app-*****-*****-55bd787975              2         2         2       38d
    

  12.  Get all pods in cluster using selection condition: Using "--field-selector" we can fetch only running pods in cluster
    n0r0082@m-c02z31rnlvdt ~ % kubectl get pods --field-selector=status.phase=Running 
    -n clust_namespace1

  13. Get all nodes (workers/master{control-plane}) in cluster: Using "--selector" we can include and exclude nodes.

    To fetch only control-plane nodes, use label "node-role.kubernetes.io/master"
    n0r0082@m-c02z31rnlvdt ~ % kubectl get node --selector='node-role.kubernetes.io/master' 
    -n clust_namespace1 NAME STATUS ROLES AGE VERSION cp2-****-**-a2-vmss000000 Ready control-plane,master 538d v1.20.13 cp2-****-**-a2-vmss000001 Ready control-plane,master 538d v1.20.13

    To fetch only workers nodes, use label "node-role.kubernetes.io/master"with negation(!)
    n0r0082@m-c02z31rnlvdt ~ % kubectl get node --selector='!node-role.kubernetes.io/master' 
    -n clust_namespace1 NAME STATUS ROLES AGE VERSION ....
    ......

Post a Comment

Previous Post Next Post