K8s Commands

K8s Commands

Basic Commands

  1. Get cluster information:

     kubectl cluster-info
    
  2. Display available commands:

     kubectl help
    
  3. Get current context:

     kubectl config current-context
    
  4. Set current context:

     kubectl config use-context <context-name>
    

Create and Apply Resources

  1. Create a resource from a file or stdin:

     kubectl create -f <filename>
    
  2. Apply a configuration to a resource by filename or stdin:

     kubectl apply -f <filename>
    

View Resources

  1. Get all resources:

     kubectl get all
    
  2. Get all resources in a specific namespace:

     kubectl get all -n <namespace>
    
  3. Get specific resource types:

     kubectl get <resource-type>
    
  4. Get a specific resource:

     kubectl get <resource-type> <resource-name>
    
  5. Get detailed information about a resource:

     kubectl describe <resource-type> <resource-name>
    

Edit and Update Resources

  1. Edit a resource:

     kubectl edit <resource-type> <resource-name>
    
  2. Patch a resource:

     kubectl patch <resource-type> <resource-name> --patch '{"spec": {"replicas": 2}}'
    
  3. Scale a resource:

     # 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:

     kubectl delete <resource-type> <resource-name>
    
  2. Delete a resource by filename:

     kubectl delete -f <filename>
    
  3. Delete all resources of a specific type:

     kubectl delete <resource-type> --all
    

Namespace Management

  1. Get all namespaces:

     kubectl get namespaces
    
  2. Create a namespace:

     kubectl create namespace <namespace-name>
    
  3. Delete a namespace:

     kubectl delete namespace <namespace-name>
    

Logs and Events

  1. View logs of a specific pod:

     kubectl logs <pod-name>
    
  2. View logs of a specific container in a pod:

     kubectl logs <pod-name> -c <container-name>
    
  3. Stream logs of a specific pod:

     kubectl logs -f <pod-name>
    
  4. View cluster events:

     kubectl get events
    

Exec and Port Forward

  1. Execute a command in a container:

     kubectl exec <pod-name> -- <command>
    
  2. Start a bash session in a container:

     kubectl exec -it <pod-name> -- /bin/bash
    
  3. Forward a local port to a port on a pod:

     kubectl port-forward <pod-name> <local-port>:<pod-port>
    

    or

     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.

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.

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

Resource Types

  1. Pods:

     kubectl get pods
     kubectl describe pod <pod-name>
     kubectl delete pod <pod-name>
    
  2. Deployments:

     kubectl get deployments
     kubectl describe deployment <deployment-name>
     kubectl delete deployment <deployment-name>
    
  3. Replicaset:

     kubectl get replicaset
     kubectl describe replicaset <replicaset-name>
     kubectl delete replicaset <replicaset-name>
    
  4. Services:

     kubectl get services
     kubectl describe service <service-name>
     kubectl delete service <service-name>
    
  5. ConfigMaps:

     kubectl get configmaps
     kubectl describe configmap <configmap-name>
     kubectl delete configmap <configmap-name>
    
  6. Secrets:

     kubectl get secrets
     kubectl describe secret <secret-name>
     kubectl delete secret <secret-name>
    
  7. Ingresses:

     kubectl get ingresses
     kubectl describe ingress <ingress-name>
     kubectl delete ingress <ingress-name>
    
  8. Ingress controller:

     kubectl get pods -n ingress-nginx //To check ingress-controller is running or not
    
  9. Webhook configurations:

     kubectl get validatingwebhookconfigurations
    
  10. PersistentVolumeClaims (PVCs):

    kubectl get pvc
    kubectl describe pvc <pvc-name>
    kubectl delete pvc <pvc-name>
    
  11. PersistentVolumes (PVs):

    kubectl get pv
    kubectl describe pv <pv-name>
    kubectl delete pv <pv-name>
    
  12. StatefulSets:

    kubectl get statefulsets
    kubectl describe statefulset <statefulset-name>
    kubectl delete statefulset <statefulset-name>
    
  13. DaemonSets:

    kubectl get daemonsets
    kubectl describe daemonset <daemonset-name>
    kubectl delete daemonset <daemonset-name>
    
  14. Jobs:

    kubectl get jobs
    kubectl describe job <job-name>
    kubectl delete job <job-name>
    
  15. CronJobs:

    kubectl get cronjobs
    kubectl describe cronjob <cronjob-name>
    kubectl delete cronjob <cronjob-name>
    

Other Useful Commands

  1. Apply a resource configuration from a file:

     kubectl apply -f <filename>
    
  2. Create a secret from literals:

     kubectl create secret generic <secret-name> --from-literal=<key>=<value>
    
  3. Expose a deployment as a service:

     kubectl expose deployment <deployment-name> --type=LoadBalancer --name=<service-name>
    
  4. Roll out a new version of a deployment:

     kubectl rollout restart deployment <deployment-name>
    
  5. Roll back to a previous version of a deployment:

     kubectl rollout undo deployment <deployment-name>
    
  6. Check the rollout status of a deployment:

     kubectl rollout status deployment <deployment-name>
    

Context and Configuration Management

  1. View current contexts:

     kubectl config get-contexts
    
  2. Use a specific context:

     kubectl config use-context <context-name>
    
  3. Set a context's namespace:

     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:

     kubectl delete pod <pod-name>
    
  2. Delete all pods in a namespace:

     kubectl delete pods --all -n <namespace>
    
  3. Delete pods by label:

     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:

     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):

     kubectl delete pod <pod-name> --grace-period=0 --force
    

Here are some specific examples:

  1. Delete a pod namednginx-pod:

     kubectl delete pod nginx-pod
    
  2. Delete all pods in thedefault namespace:

     kubectl delete pods --all -n default
    
  3. Delete pods with the labelapp=myapp:

     kubectl delete pods -l app=myapp
    
  4. Delete pods with the labelapp=myapp in the development namespace:

     kubectl delete pods -l app=myapp -n development
    
  5. Force delete a pod namedstuck-pod:

     kubectl delete pod stuck-pod --grace-period=0 --force