# 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**:
    
    ```sh
    kubectl version --client
    ```
    
* **Display Cluster Info**:
    
    ```sh
    kubectl cluster-info
    ```
    
* **List Nodes**:
    
    ```sh
    kubectl get nodes
    ```
    
* **Describe a Node**:
    
    ```sh
    kubectl describe node NODE_NAME
    ```
    

#### Namespaces

* **List Namespaces**:
    
    ```sh
    kubectl get namespaces
    ```
    
* **Create a Namespace**:
    
    ```sh
    kubectl create namespace NAMESPACE_NAME
    ```
    
* **Delete a Namespace**:
    
    ```sh
    kubectl delete namespace NAMESPACE_NAME
    ```
    

#### Pods

* **List Pods**:
    
    ```sh
    kubectl get pods
    ```
    
* **List Pods in All Namespaces**:
    
    ```sh
    kubectl get pods --all-namespaces
    ```
    
* **Describe a Pod**:
    
    ```sh
    kubectl describe pod POD_NAME
    ```
    
* **Get Pod Logs**:
    
    ```sh
    kubectl logs POD_NAME
    ```
    
* **Execute a Command in a Pod**:
    
    ```sh
    kubectl exec -it POD_NAME -- COMMAND
    ```
    
* **Create a Pod from a YAML File**:
    
    ```sh
    kubectl apply -f pod.yaml
    ```
    
* **Delete a Pod**:
    
    ```sh
    kubectl delete pod POD_NAME
    ```
    

#### Deployments

* **List Deployments**:
    
    ```sh
    kubectl get deployments
    ```
    
* **Describe a Deployment**:
    
    ```sh
    kubectl describe deployment DEPLOYMENT_NAME
    ```
    
* **Scale a Deployment**:
    
    ```sh
    kubectl scale deployment DEPLOYMENT_NAME --replicas=NUMBER_OF_REPLICAS
    ```
    
* **Update a Deployment**:
    
    ```sh
    kubectl set image deployment/DEPLOYMENT_NAME CONTAINER_NAME=IMAGE:TAG
    ```
    
* **Roll Back a Deployment**:
    
    ```sh
    kubectl rollout undo deployment/DEPLOYMENT_NAME
    ```
    
* **Get Deployment Status**:
    
    ```sh
    kubectl rollout status deployment/DEPLOYMENT_NAME
    ```
    

#### Services

* **List Services**:
    
    ```sh
    kubectl get services
    ```
    
* **Describe a Service**:
    
    ```sh
    kubectl describe service SERVICE_NAME
    ```
    
* **Expose a Deployment as a Service**:
    
    ```sh
    kubectl expose deployment DEPLOYMENT_NAME --type=LoadBalancer --name=SERVICE_NAME
    ```
    
* **Delete a Service**:
    
    ```sh
    kubectl delete service SERVICE_NAME
    ```
    

#### ConfigMaps and Secrets

* **List ConfigMaps**:
    
    ```sh
    kubectl get configmaps
    ```
    
* **Create a ConfigMap from a File**:
    
    ```sh
    kubectl create configmap CONFIGMAP_NAME --from-file=FILE_PATH
    ```
    
* **Describe a ConfigMap**:
    
    ```sh
    kubectl describe configmap CONFIGMAP_NAME
    ```
    
* **Delete a ConfigMap**:
    
    ```sh
    kubectl delete configmap CONFIGMAP_NAME
    ```
    
* **List Secrets**:
    
    ```sh
    kubectl get secrets
    ```
    
* **Create a Secret from Literal**:
    
    ```sh
    kubectl create secret generic SECRET_NAME --from-literal=key1=value1 --from-literal=key2=value2
    ```
    
* **Describe a Secret**:
    
    ```sh
    kubectl describe secret SECRET_NAME
    ```
    
* **Delete a Secret**:
    
    ```sh
    kubectl delete secret SECRET_NAME
    ```
    

#### Persistent Volumes and Claims

* **List Persistent Volumes**:
    
    ```sh
    kubectl get pv
    ```
    
* **Describe a Persistent Volume**:
    
    ```sh
    kubectl describe pv PV_NAME
    ```
    
* **List Persistent Volume Claims**:
    
    ```sh
    kubectl get pvc
    ```
    
* **Describe a Persistent Volume Claim**:
    
    ```sh
    kubectl describe pvc PVC_NAME
    ```
    

#### Config Management

* **Apply a Configuration**:
    
    ```sh
    kubectl apply -f config.yaml
    ```
    
* **Delete a Resource from a Configuration File**:
    
    ```sh
    kubectl delete -f config.yaml
    ```
    
* **Diff Current State with Configuration File**:
    
    ```sh
    kubectl diff -f config.yaml
    ```
    

#### Resource Status

* **Get Resource Status**:
    
    ```sh
    kubectl get RESOURCE_TYPE RESOURCE_NAME
    ```
    
* **Describe Resource**:
    
    ```sh
    kubectl describe RESOURCE_TYPE RESOURCE_NAME
    ```
    

#### Troubleshooting

* **View Events**:
    
    ```sh
    kubectl get events
    ```
    
* **Debug a Resource**:
    
    ```sh
    kubectl describe RESOURCE_TYPE RESOURCE_NAME
    ```
    
* **Check Cluster Components Status**:
    
    ```sh
    kubectl get componentstatuses
    ```
    
* **Get Logs for a Pod**:
    
    ```sh
    kubectl logs POD_NAME
    ```
    
* **Follow Logs for a Pod**:
    
    ```sh
    kubectl logs -f POD_NAME
    ```
    

#### Cleaning Up

* **Delete a Resource**:
    
    ```sh
    kubectl delete RESOURCE_TYPE RESOURCE_NAME
    ```
    
* **Delete All Pods in a Namespace**:
    
    ```sh
    kubectl delete pods --all -n NAMESPACE_NAME
    ```
    
* **Delete All Resources in a Namespace**:
    
    ```sh
    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](https://kubernetes.io/docs/reference/kubectl/overview/).
