Table of contents


Introduction to kubectl

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.

The common format of a kubectl command is:

kubectl <action> <resource> <name-resource>

This performs the specified action (like create, describe) on the specified resource (like node, container) with its name.

To know more about resource types, we can following the link.


Basic commands in Kubectl

Belows are some basic kubectl commands:

  1. Check version of kubernetes clusters

    If you want to check client and server’s version of Kubernetes cluster, use this command.

     kubectl version
    

    For example:

  2. Create a resource for cluster

    Create a resource from a file or from stdin.

    For example, create a Kubernetes deployment from the file.

     kubectl create <resource-name>
    

    For example:

    • let’s deploy our app on kubernetes with the deployment name and app image location

        kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
      

      This performed a few things for you:

      • searched for a suitable node where an instance of the application could be run.
      • scheduled the application to run on that Node.
      • configured the cluster to reschedule the instance on a new Node when needed.
  3. View the resources in the cluster

     kubectl get <resource-name>
    

    For example:

    • View the nodes in the cluster

        kubectl get nodes
      
    • View our deployments

        kubectl get deployments
      

      Then, we have:

    • Get a list of running pods or the YAML output of a pod.

  4. Run a particular image on the cluster

     kubectl run 
    
  5. Set specific features on on objects

     kubectl set
    

    For example:

    • set environment variables
    • update a Docker image in a pod template
  6. Take a service, deployment, or pod and expose it as a new Kubernetes Service

     kubectl expose
    
  7. Get the documentation of resources

     kubectl explain
    

    For example:

    • the documentation on deployments
  8. Edit a resource

     kubectl edit
    

    For example:

    • edit a deployment
  9. Delete resources by filenames, stdin, resources, and names, or by resources and label selectors

     kubectl delete
    


Deploy commands in Kubectl

  1. Manage the rollout of a resource

     kubectl rollout
    
  2. Set a new size for a deployment, ReplicaSet, or StatefulSet

     kubectl scale
    
  3. Auto-scale a deployment, ReplicaSet, or StatefulSet

     kubectl autoscale
    


Cluster management commands

  1. Modify certificate resources

     kubectl certificate
    
  2. Display cluster information

     kubectl cluster-info
    
  3. Display resource (CPU/memory/storage) usage

     kubectl top
    
  4. Mark a node as unschedulable

     kubectl cordon
    
  5. Mark a node as schedulable

     kubectl uncordon
    
  6. Drain a node in preparation for maintenance

     kubectl drain
    
  7. Update the taints on one or more nodes

     kubectl taint
    


Troubleshooting and debugging commands

  1. Show the details of a specific resource or group of resources

     kubectl describe
    
  2. Print the logs for a container in a pod

     kubectl logs
    
  3. Attach to a running container

     kubectl attach
    
  4. Execute a command in a container

     kubectl exec
    
  5. Forward one or more local ports to a pod

     kubectl port-forward
    
  6. Run a proxy to the Kubernetes API server

     kubectl proxy
    
  7. Copy files and directories to and from containers

     kubectl cp
    
  8. Inspect authorization

     kubectl auth
    


Advanced commands

  1. Show difference of live version against a would-be applied version

     kubectl diff
    
  2. Apply a configuration to a resource by filename or stdin

     kubectl apply
    
  3. Update the fields of a resource using a strategic merge patch

     kubectl patch
    
  4. Replace a resource by filename or stdin

     kubectl replace
    
  5. Wait for a specific condition on one or many resources

     kubectl wait
    
  6. Convert config files between different API versions

     kubectl convert
    
  7. Build a customization target from a directory or a remote URL

     kubectl kustomize
    


Wrapping up


Refer:

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