Skip to main content

Kubernetes General Commands

Cluster Information​

  • Check cluster info:
    kubectl cluster-info
  • View all nodes in the cluster:
    kubectl get nodes
  • Describe a specific node:
    kubectl describe node <node-name>
  • Get the current context:
    kubectl config current-context
  • List all contexts:
    kubectl config get-contexts
  • Switch to a different context:
    kubectl config use-context <context-name>

Pod Management​

  • List all pods in the default namespace:
    kubectl get pods
  • List pods in a specific namespace:
    kubectl get pods -n <namespace>
  • Get detailed information about a pod:
    kubectl describe pod <pod-name>
  • Get logs from a pod:
    kubectl logs <pod-name>
  • Get logs from a specific container in a pod:
    kubectl logs <pod-name> -c <container-name>
  • Execute a command inside a running pod:
    kubectl exec -it <pod-name> -- /bin/sh

Deployment Management​

  • List all deployments:
    kubectl get deployments
  • Describe a deployment:
    kubectl describe deployment <deployment-name>
  • Scale a deployment:
    kubectl scale deployment <deployment-name> --replicas=<num>
  • Rollout status of a deployment:
    kubectl rollout status deployment <deployment-name>
  • Rollback a deployment to a previous version:
    kubectl rollout undo deployment <deployment-name>

Service Management​

  • List all 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 --port=80
  • Port-forward a service to local machine:
    kubectl port-forward svc/<service-name> <local-port>:<service-port>

Namespace Management​

  • List all namespaces:
    kubectl get namespaces
  • Create a namespace:
    kubectl create namespace <namespace-name>
  • Delete a namespace:
    kubectl delete namespace <namespace-name>

ConfigMap & Secret Management​

  • List all ConfigMaps:
    kubectl get configmaps
  • List all Secrets:
    kubectl get secrets
  • Create a Secret from a file:
    kubectl create secret generic <secret-name> --from-file=<path-to-file>

Persistent Volume Management​

  • List all Persistent Volumes (PV):
    kubectl get pv
  • List all Persistent Volume Claims (PVC):
    kubectl get pvc

Resource Cleanup​

  • Delete a pod:
    kubectl delete pod <pod-name>
  • Delete a deployment:
    kubectl delete deployment <deployment-name>
  • Delete a service:
    kubectl delete service <service-name>
  • Delete all resources in a namespace:
    kubectl delete all --all -n <namespace>

Debugging & Troubleshooting​

  • Get events in a namespace:
    kubectl get events -n <namespace>
  • Debug a failing pod:
    kubectl debug pod/<pod-name> --image=busybox -it
  • Show the live resource changes:
    kubectl get pods --watch