Kubernetes command-line tool "kubectl" commands

kubectl is Kubernetes command-line tool that allows you to run commands against Kubernetes clusters. 

The kubectl command line tool lets you control Kubernetes clusters. For configuration, kubectl looks for a file named config in the $HOME/.kube directory. You can specify other kubeconfig files by setting the KUBECONFIG environment variable or by setting the --kubeconfig flag.

example:

    kubectl command to pass kubeconfig  as commandline argument & fetch client & server version => kubectl --kubeconfig=<kubeconfig_file_path> version


Cluster Management:
     

    Display the Kubernetes version running on both the client and server => kubectl version

    Display endpoint information about the master and services in the cluster => kubectl cluster-info 

    Get the configuration of the cluster => kubectl config view 

    List the API resources that are available => kubectl api-resources 

    List all everything(running resources in all namespaces) => kubectl get all -A 


Nodes(no):
     

    Update the taints on one or more nodes => kubectl taint node <node_name> 

    List one or more nodes => kubectl get node 

    Describe one or more nodes => kubectl get node

    Show node labels =>  kubectl get nodes --show-labels

    Add or update the labels of one or more nodes => kubectl label node <node-name> <key>=<value>

    Display Resource usage (CPU/Memory/Storage) for nodes => kubectl top node

    Delete a node or multiple nodes => kubectl delete node <node_name>

    Resource allocation per node => kubectl describe nodes | grep Allocated -A 5 

    GPU Resource available/used per node =>
kubectl describe nodes  |  tr -d '\000' | sed -n -e '/^Name/,/Roles/p' -e '/^Capacity/,/Allocatable/p' -e '/^Allocated resources/,/Events/p'  | grep -e Name  -e  nvidia.com  | perl -pe 's/\n//'  |  perl -pe 's/Name:/\n/g' | sed 's/nvidia.com\/gpu:\?//g'  | sed '1s/^/Node Available(GPUs)  Used(GPUs)/' | sed 's/$/ 0 0 0/'  | awk '{print $1, $2, $3}'  | column -t
 

    Pods running on a node => kubectl get pods -o wide | grep <node_name> 

    Annotate a node => kubectl annotate node <node_name> 

    Mark a node as unschedulable => kubectl cordon node <node_name> 

    Mark node as schedulable => kubectl uncordon node <node_name>


Pods(po):


    List one or more pods => kubectl get pod

    List one or more pods in all namespaces => kubectl get pod -A

    List one or more pods in wide format => kubectl get pod -o wide

    List one or more pods yaml spec => kubectl get pod -o yaml

    List one or more pods of a specific namespace => kubectl get pod -n <namespace_name> 

    Delete a pod => kubectl delete pod <pod_name> 

    Display the detailed state of a pods => kubectl describe pod <pod_name> 

    Create a pod => kubectl create pod <pod_name> 

    Execute a command against a container in a pod => kubectl exec <pod_name> -c <container_name> <command> 

    Get interactive shell on a a single-container pod => kubectl exec -it <pod_name> /bin/sh 

    Display Resource usage (CPU/Memory/Storage) for pods => kubectl top pod 

    Add or update the annotations of a pod => kubectl annotate pod <pod_name> <annotation> 

    Add or update the label of a pod => kubectl label pod <pod_name>


Services(svc): 

    List one or more services => kubectl get services

    List one or more services in all namespaces => kubectl get svc -A

    Display the detailed state of a service => kubectl describe services 

    Expose a replication controller, service, deployment or pod as a new Kubernetes service => kubectl expose deployment <deployment_name> 

    Edit and update the definition of one or more services => kubectl edit services
 
 

Watch:

    To monitor progress, use the kubectl get service command with the --watch argument.
    example:
        kubectl get service azure-vote-front --watch

 

Secrets: 

    Create a secret => kubectl create secret

    List secrets => kubectl get secrets 

    List details about secrets => kubectl describe secrets 

    Delete a secret => kubectl delete secret <secret_name>
 

Deployments(deploy):
 

    List one or more deployments in default namespace => kubectl get deployment

    List one or more deployments in all namespaces => kubectl get deployment -A 

    Display the detailed state of one or more deployments => kubectl describe deployment <deployment_name> 

    Edit and update the definition of one or more deployment on the server => kubectl edit deployment <deployment_name> 

    Create one a new deployment => kubectl create deployment <deployment_name> 

    Delete deployments => kubectl delete deployment <deployment_name> 


Logs:

    Print the logs for a pod => kubectl logs <pod_name>

    Print the logs for a pod and follow new logs => kubectl logs -f <pod_name>

    Print the logs for a container in a pod => kubectl logs -c <container_name> <pod_name>

    Output the logs for a pod into a file named ‘pod.log’ => kubectl logs <pod_name> pod.log 

    View the logs for a previously failed pod => kubectl logs --previous <pod_name>

    Print the logs for the last hour for a pod => kubectl logs --since=1h <pod_name> 

    Get the most recent 20 lines of logs => kubectl logs --tail=20 <pod_name> 

    Get logs from a service and optionally select which container => kubectl logs -f <service_name> [-c <$container>]

 

Events(ev):

    List recent events for all resources in the system => kubectl get events 

    List Warnings only => kubectl get events --field-selector type=Warning 

    List events but exclude Pod events => kubectl get events --field-selector involvedObject.kind!=Pod
 

Manifest Files:
 

    Apply a configuration to an object by filename or stdin. Overrides the existing configuration => kubectl apply -f manifest_file.yaml 

    Create objects => kubectl create -f manifest_file.yaml 

    Create objects in all manifest files in a directory => kubectl create -f ./dir 

    Create objects from a URL => kubectl create -f ‘url’

    Delete an object => kubectl delete -f manifest_file.yaml

 


ref:

Kubectl overview - https://kubernetes.io/docs/reference/kubectl/overview/

Install kubectl - https://kubernetes.io/docs/tasks/tools/install-kubectl/

kubectl cheat sheet - 

    https://kubernetes.io/docs/reference/kubectl/cheatsheet/

    https://www.bluematador.com/learn/kubectl-cheatsheet

    https://unofficial-kubernetes.readthedocs.io/en/latest/user-guide/kubectl-cheatsheet/

    https://opensource.com/article/20/5/kubectl-cheat-sheet