Cheat Sheet #day16 - kubectl Commands

Cheat Sheet #day16 - kubectl Commands

kubectl Commands Cheat Sheet

kubectl is the command-line tool for interacting with Kubernetes clusters. This cheat sheet covers the most commonly used kubectl commands to help manage and troubleshoot Kubernetes clusters.

Cluster Information

  • Check kubectl Version:

      kubectl version --client
    
  • Display Cluster Info:

      kubectl cluster-info
    
  • List Nodes:

      kubectl get nodes
    
  • Describe a Node:

      kubectl describe node NODE_NAME
    

Namespaces

  • List Namespaces:

      kubectl get namespaces
    
  • Create a Namespace:

      kubectl create namespace NAMESPACE_NAME
    
  • Delete a Namespace:

      kubectl delete namespace NAMESPACE_NAME
    

Pods

  • List Pods:

      kubectl get pods
    
  • List Pods in All Namespaces:

      kubectl get pods --all-namespaces
    
  • Describe a Pod:

      kubectl describe pod POD_NAME
    
  • Get Pod Logs:

      kubectl logs POD_NAME
    
  • Execute a Command in a Pod:

      kubectl exec -it POD_NAME -- COMMAND
    
  • Create a Pod from a YAML File:

      kubectl apply -f pod.yaml
    
  • Delete a Pod:

      kubectl delete pod POD_NAME
    

Deployments

  • List Deployments:

      kubectl get deployments
    
  • Describe a Deployment:

      kubectl describe deployment DEPLOYMENT_NAME
    
  • Scale a Deployment:

      kubectl scale deployment DEPLOYMENT_NAME --replicas=NUMBER_OF_REPLICAS
    
  • Update a Deployment:

      kubectl set image deployment/DEPLOYMENT_NAME CONTAINER_NAME=IMAGE:TAG
    
  • Roll Back a Deployment:

      kubectl rollout undo deployment/DEPLOYMENT_NAME
    
  • Get Deployment Status:

      kubectl rollout status deployment/DEPLOYMENT_NAME
    

Services

  • List Services:

      kubectl get services
    
  • Describe a Service:

      kubectl describe service SERVICE_NAME
    
  • Expose a Deployment as a Service:

      kubectl expose deployment DEPLOYMENT_NAME --type=LoadBalancer --name=SERVICE_NAME
    
  • Delete a Service:

      kubectl delete service SERVICE_NAME
    

ConfigMaps and Secrets

  • List ConfigMaps:

      kubectl get configmaps
    
  • Create a ConfigMap from a File:

      kubectl create configmap CONFIGMAP_NAME --from-file=FILE_PATH
    
  • Describe a ConfigMap:

      kubectl describe configmap CONFIGMAP_NAME
    
  • Delete a ConfigMap:

      kubectl delete configmap CONFIGMAP_NAME
    
  • List Secrets:

      kubectl get secrets
    
  • Create a Secret from Literal:

      kubectl create secret generic SECRET_NAME --from-literal=key1=value1 --from-literal=key2=value2
    
  • Describe a Secret:

      kubectl describe secret SECRET_NAME
    
  • Delete a Secret:

      kubectl delete secret SECRET_NAME
    

Persistent Volumes and Claims

  • List Persistent Volumes:

      kubectl get pv
    
  • Describe a Persistent Volume:

      kubectl describe pv PV_NAME
    
  • List Persistent Volume Claims:

      kubectl get pvc
    
  • Describe a Persistent Volume Claim:

      kubectl describe pvc PVC_NAME
    

Config Management

  • Apply a Configuration:

      kubectl apply -f config.yaml
    
  • Delete a Resource from a Configuration File:

      kubectl delete -f config.yaml
    
  • Diff Current State with Configuration File:

      kubectl diff -f config.yaml
    

Resource Status

  • Get Resource Status:

      kubectl get RESOURCE_TYPE RESOURCE_NAME
    
  • Describe Resource:

      kubectl describe RESOURCE_TYPE RESOURCE_NAME
    

Troubleshooting

  • View Events:

      kubectl get events
    
  • Debug a Resource:

      kubectl describe RESOURCE_TYPE RESOURCE_NAME
    
  • Check Cluster Components Status:

      kubectl get componentstatuses
    
  • Get Logs for a Pod:

      kubectl logs POD_NAME
    
  • Follow Logs for a Pod:

      kubectl logs -f POD_NAME
    

Cleaning Up

  • Delete a Resource:

      kubectl delete RESOURCE_TYPE RESOURCE_NAME
    
  • Delete All Pods in a Namespace:

      kubectl delete pods --all -n NAMESPACE_NAME
    
  • Delete All Resources in a Namespace:

      kubectl delete all --all -n NAMESPACE_NAME
    

Conclusion

This cheat sheet provides a quick reference to the most commonly used kubectl commands. For more detailed information and advanced usage, refer to the official Kubernetes documentation.

Did you find this article valuable?

Support Cloud Tuned by becoming a sponsor. Any amount is appreciated!