# K8s Commands

### Basic Commands

1. **Get cluster information:**
    
    ```bash
    kubectl cluster-info
    ```
    
2. **Display available commands:**
    
    ```bash
    kubectl help
    ```
    
3. **Get current context:**
    
    ```bash
    kubectl config current-context
    ```
    
4. **Set current context:**
    
    ```bash
    kubectl config use-context <context-name>
    ```
    

### Create and Apply Resources

1. **Create a resource from a file or stdin:**
    
    ```bash
    kubectl create -f <filename>
    ```
    
2. **Apply a configuration to a resource by filename or stdin:**
    
    ```bash
    kubectl apply -f <filename>
    ```
    

### View Resources

1. **Get all resources:**
    
    ```bash
    kubectl get all
    ```
    
2. **Get all resources in a specific namespace:**
    
    ```bash
    kubectl get all -n <namespace>
    ```
    
3. **Get specific resource types:**
    
    ```bash
    kubectl get <resource-type>
    ```
    
4. **Get a specific resource:**
    
    ```bash
    kubectl get <resource-type> <resource-name>
    ```
    
5. **Get detailed information about a resource:**
    
    ```bash
    kubectl describe <resource-type> <resource-name>
    ```
    

### Edit and Update Resources

1. **Edit a resource:**
    
    ```bash
    kubectl edit <resource-type> <resource-name>
    ```
    
2. **Patch a resource:**
    
    ```bash
    kubectl patch <resource-type> <resource-name> --patch '{"spec": {"replicas": 2}}'
    ```
    
3. **Scale a resource:**
    
    ```bash
    # kubectl scale
    kubectl scale <resource-type> <resource-name> --replicas=<number>
    Eg.,
    kubectl scale deployment my-app --replicas=3
    
    # kubectl set
    kubectl set image <resource>/<resource-name> <container-name>=<image>:<tag>
    Eg.,
    kubectl set image deployment/my-deployment my-container=nginx:1.19
    ```
    
    * `kubectl scale` is used to **change the number of replicas** for resources that support it (e.g., Deployments).
        
    * `kubectl set` allows you to **modify fields** (e.g., image, environment variables) in a resource's specification.
        

### Delete Resources

1. **Delete a resource:**
    
    ```bash
    kubectl delete <resource-type> <resource-name>
    ```
    
2. **Delete a resource by filename:**
    
    ```bash
    kubectl delete -f <filename>
    ```
    
3. **Delete all resources of a specific type:**
    
    ```bash
    kubectl delete <resource-type> --all
    ```
    

### Namespace Management

1. **Get all namespaces:**
    
    ```bash
    kubectl get namespaces
    ```
    
2. **Create a namespace:**
    
    ```bash
    kubectl create namespace <namespace-name>
    ```
    
3. **Delete a namespace:**
    
    ```bash
    kubectl delete namespace <namespace-name>
    ```
    

### Logs and Events

1. **View logs of a specific pod:**
    
    ```bash
    kubectl logs <pod-name>
    ```
    
2. **View logs of a specific container in a pod:**
    
    ```bash
    kubectl logs <pod-name> -c <container-name>
    ```
    
3. **Stream logs of a specific pod:**
    
    ```bash
    kubectl logs -f <pod-name>
    ```
    
4. **View cluster events:**
    
    ```bash
    kubectl get events
    ```
    

### Exec and Port Forward

1. **Execute a command in a container:**
    
    ```bash
    kubectl exec <pod-name> -- <command>
    ```
    
2. **Start a bash session in a container:**
    
    ```bash
    kubectl exec -it <pod-name> -- /bin/bash
    ```
    
3. **Forward a local port to a port on a pod:**
    
    ```bash
    kubectl port-forward <pod-name> <local-port>:<pod-port>
    ```
    
    or
    
    ```bash
    kubectl port-forward svc/<svc-name> <local-port>:<pod:port>
    ```
    

### Label the Nodes

Create a namespace so that we can dedicate a node to the namespace.

```bash
kubectl create namespace high5
```

Label the nodes you want to schedule the namespace's pods on. For example, let's say you have two nodes named `node1` and `node2`.

```bash
kubectl label nodes <node1-name> dedicated=high5
kubectl label nodes <node2-name> dedicated=high5
```

### Resource Types

1. **Pods:**
    
    ```bash
    kubectl get pods
    kubectl describe pod <pod-name>
    kubectl delete pod <pod-name>
    ```
    
2. **Deployments:**
    
    ```bash
    kubectl get deployments
    kubectl describe deployment <deployment-name>
    kubectl delete deployment <deployment-name>
    ```
    
3. **Replicaset:**
    
    ```bash
    kubectl get replicaset
    kubectl describe replicaset <replicaset-name>
    kubectl delete replicaset <replicaset-name>
    ```
    
4. **Services:**
    
    ```bash
    kubectl get services
    kubectl describe service <service-name>
    kubectl delete service <service-name>
    ```
    
5. **ConfigMaps:**
    
    ```bash
    kubectl get configmaps
    kubectl describe configmap <configmap-name>
    kubectl delete configmap <configmap-name>
    ```
    
6. **Secrets:**
    
    ```bash
    kubectl get secrets
    kubectl describe secret <secret-name>
    kubectl delete secret <secret-name>
    ```
    
7. **Ingresses:**
    
    ```bash
    kubectl get ingresses
    kubectl describe ingress <ingress-name>
    kubectl delete ingress <ingress-name>
    ```
    
8. **Ingress controller:**
    
    ```bash
    kubectl get pods -n ingress-nginx //To check ingress-controller is running or not
    ```
    
9. **Webhook configurations:**
    
    ```bash
    kubectl get validatingwebhookconfigurations
    ```
    
10. **PersistentVolumeClaims (PVCs):**
    
    ```bash
    kubectl get pvc
    kubectl describe pvc <pvc-name>
    kubectl delete pvc <pvc-name>
    ```
    
11. **PersistentVolumes (PVs):**
    
    ```bash
    kubectl get pv
    kubectl describe pv <pv-name>
    kubectl delete pv <pv-name>
    ```
    
12. **StatefulSets:**
    
    ```bash
    kubectl get statefulsets
    kubectl describe statefulset <statefulset-name>
    kubectl delete statefulset <statefulset-name>
    ```
    
13. **DaemonSets:**
    
    ```bash
    kubectl get daemonsets
    kubectl describe daemonset <daemonset-name>
    kubectl delete daemonset <daemonset-name>
    ```
    
14. **Jobs:**
    
    ```bash
    kubectl get jobs
    kubectl describe job <job-name>
    kubectl delete job <job-name>
    ```
    
15. **CronJobs:**
    
    ```bash
    kubectl get cronjobs
    kubectl describe cronjob <cronjob-name>
    kubectl delete cronjob <cronjob-name>
    ```
    

### Other Useful Commands

1. **Apply a resource configuration from a file:**
    
    ```bash
    kubectl apply -f <filename>
    ```
    
2. **Create a secret from literals:**
    
    ```bash
    kubectl create secret generic <secret-name> --from-literal=<key>=<value>
    ```
    
3. **Expose a deployment as a service:**
    
    ```bash
    kubectl expose deployment <deployment-name> --type=LoadBalancer --name=<service-name>
    ```
    
4. **Roll out a new version of a deployment:**
    
    ```bash
    kubectl rollout restart deployment <deployment-name>
    ```
    
5. **Roll back to a previous version of a deployment:**
    
    ```bash
    kubectl rollout undo deployment <deployment-name>
    ```
    
6. **Check the rollout status of a deployment:**
    
    ```bash
    kubectl rollout status deployment <deployment-name>
    ```
    

### Context and Configuration Management

1. **View current contexts:**
    
    ```bash
    kubectl config get-contexts
    ```
    
2. **Use a specific context:**
    
    ```bash
    kubectl config use-context <context-name>
    ```
    
3. **Set a context's namespace:**
    
    ```bash
    kubectl config set-context --current --namespace=<namespace-name>
    ```
    

### Few other commands to Delete the pods

To delete pods using `kubectl`, you can use the `kubectl delete` command. Here are some examples of how to delete pods in different scenarios:

1. **Delete a specific pod**:
    
    ```bash
    kubectl delete pod <pod-name>
    ```
    
2. **Delete all pods in a namespace**:
    
    ```bash
    kubectl delete pods --all -n <namespace>
    ```
    
3. **Delete pods by label**:
    
    ```bash
    kubectl delete pods -l <label-key>=<label-value>
    ```
    
    `label-key:` key which we write under metadata in deployment.yaml file.
    
    `label-value:` Value of the key which we write under metadata in deployment.yaml file.
    
4. **Delete pods in a specific namespace by label**:
    
    ```bash
    kubectl delete pods -l <label-key>=<label-value> -n <namespace>
    ```
    
    `label-key:` key which we write under metadata in deployment.yaml file.
    
    `label-value:` Value of the key which we write under metadata in deployment.yaml file.
    
5. **Force delete a pod** (useful if a pod is stuck in a terminating state):
    
    ```bash
    kubectl delete pod <pod-name> --grace-period=0 --force
    ```
    

Here are some specific examples:

1. **Delete a pod named**`nginx-pod`:
    
    ```bash
    kubectl delete pod nginx-pod
    ```
    
2. **Delete all pods in the**`default` namespace:
    
    ```bash
    kubectl delete pods --all -n default
    ```
    
3. **Delete pods with the label**`app=myapp`:
    
    ```bash
    kubectl delete pods -l app=myapp
    ```
    
4. **Delete pods with the label**`app=myapp` in the `development` namespace:
    
    ```bash
    kubectl delete pods -l app=myapp -n development
    ```
    
5. **Force delete a pod named**`stuck-pod`:
    
    ```bash
    kubectl delete pod stuck-pod --grace-period=0 --force
    ```
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719250531414/10e976bf-77d5-43f0-ba87-43f2cbe3c3ff.webp align="center")
