Learn Kubernetes (Kubectl) Commands List

Basic Kubernetes Commands

  1. Cluster Information
    • Description: Retrieves essential details about the Kubernetes cluster, including the master node and API server endpoints.
    • Example: kubectl cluster-info
    • Additional Detail: Useful for verifying cluster health and connectivity.
  2. List All Nodes
    • Description: Displays a list of all nodes within the Kubernetes cluster, including their statuses.
    • Example: kubectl get nodes
    • Additional Detail: Helps monitor node availability and status.
  3. Pod Information
    • Description: Fetches information about a specific pod, including its current state and resource usage.
    • Example: kubectl get pods [pod-name]
    • Additional Detail: kubectl describe pod [pod-name] provides more detailed information, including events and logs.
  4. Namespace Overview
    • Description: Lists all namespaces within the cluster, which are used to segregate resources.
    • Example: kubectl get namespaces
    • Additional Detail: Useful for managing and organizing resources in a multi-tenant environment.
  5. Pods in a Namespace
    • Description: Lists all pods running within a specific namespace, useful for isolating applications.
    • Example: kubectl get pods -n [namespace]
    • Additional Detail: Helps in managing applications deployed within different namespaces.
  6. Create Resources
    • Description: Creates a new Kubernetes resource based on a configuration file provided in YAML or JSON format.
    • Example: kubectl create -f [file.yaml]
    • Additional Detail: Supports creation of various resources like deployments, services, and ConfigMaps.
  7. Delete Resources
    • Description: Deletes a specified resource from the cluster, such as a pod or service.
    • Example: kubectl delete pod [pod-name]
    • Additional Detail: kubectl delete -f [file.yaml] deletes resources defined in a configuration file.
  8. Scale Deployments
    • Description: Adjusts the number of replicas for a deployment to manage load or redundancy.
    • Example: kubectl scale deployment [deployment-name] --replicas=[number]
    • Additional Detail: Useful for handling changes in application load.
  9. Update Deployment
    • Description: Updates the container image or other configuration aspects of a deployment.
    • Example: kubectl set image deployment/[deployment-name] [container-name]=[image:tag]
    • Additional Detail: Facilitates rolling updates to applications without downtime.
  10. Retrieve Pod Logs
    • Description: Obtains the log output from a specific pod, which is useful for debugging.
    • Example: kubectl logs [pod-name]
    • Additional Detail: kubectl logs [pod-name] -c [container-name] retrieves logs from a specific container within the pod.

Advanced Kubernetes Commands

  1. Resource Usage Metrics
    • Description: Shows real-time metrics on CPU and memory usage for pods or nodes.
    • Example: kubectl top pods
    • Additional Detail: kubectl top nodes provides similar metrics for nodes.
  2. Node Details
    • Description: Provides comprehensive information about a node, including its capacity and current conditions.
    • Example: kubectl describe node [node-name]
    • Additional Detail: Useful for troubleshooting node-specific issues.
  3. Temporary Pod Execution
    • Description: Runs a temporary pod to execute commands or perform debugging tasks.
    • Example: kubectl run [pod-name] --image=[image] --restart=Never -- [command]
    • Additional Detail: The --restart=Never flag ensures the pod is not managed by any controllers.
  4. Access Pod Shell
    • Description: Opens an interactive shell session within a pod for on-the-fly troubleshooting.
    • Example: kubectl exec -it [pod-name] -- /bin/bash
    • Additional Detail: Use /bin/sh for lighter images.
  5. Port Forwarding
    • Description: Forwards a local port to a port on a pod, enabling local access to services running in the pod.
    • Example: kubectl port-forward pod/[pod-name] [local-port]:[pod-port]
    • Additional Detail: Useful for accessing application interfaces directly from your local machine.
  6. Apply Configuration
    • Description: Applies changes from a configuration file to the cluster, such as creating or updating resources.
    • Example: kubectl apply -f [file.yaml]
    • Additional Detail: Supports both creating new resources and updating existing ones.
  7. Resource Quotas
    • Description: Displays quotas for resource usage in a namespace, helping manage resource allocation.
    • Example: kubectl get resourcequotas -n [namespace]
    • Additional Detail: Quotas can limit CPU, memory, and other resource usage.
  8. List Services
    • Description: Lists all services in the cluster or within a specific namespace.
    • Example: kubectl get services
    • Additional Detail: kubectl get svc -n [namespace] for namespace-specific services.
  9. Service Details
    • Description: Provides detailed information about a specific service, including endpoints and selectors.
    • Example: kubectl describe service [service-name]
    • Additional Detail: Useful for understanding how services are configured and connected.
  10. ConfigMaps Information
    • Description: Lists and provides details about ConfigMaps used to store configuration data.
    • Example: kubectl get configmaps
    • Additional Detail: kubectl describe configmap [configmap-name] offers more detailed insights.

Kuberenetes ( Kubectl ) Troubleshooting Commands

  1. Pod Status Check
    • Description: Retrieves the current status of a pod to determine if there are any issues.
    • Example: kubectl get pods [pod-name]
    • Additional Detail: Can be used to verify if a pod is running, pending, or has failed.
  2. Detailed Pod Description
    • Description: Provides in-depth information about a pod, including its events and conditions.
    • Example: kubectl describe pod [pod-name]
    • Additional Detail: Essential for troubleshooting pod failures and other issues.
  3. Event List
    • Description: Shows a list of recent events in a namespace, which can help diagnose problems.
    • Example: kubectl get events -n [namespace]
    • Additional Detail: Events include information about resource changes and errors.
  4. StatefulSets List
    • Description: Lists StatefulSets, which are used for managing stateful applications.
    • Example: kubectl get statefulsets
    • Additional Detail: Useful for managing and scaling stateful applications.
  5. StatefulSet Details
    • Description: Provides detailed information about a specific StatefulSet.
    • Example: kubectl describe statefulset [statefulset-name]
    • Additional Detail: Helps in diagnosing issues related to stateful applications.
  6. Persistent Volumes
    • Description: Lists persistent volumes in the cluster, which are used for storing data.
    • Example: kubectl get pv
    • Additional Detail: Useful for managing and verifying storage resources.
  7. Persistent Volume Claims
    • Description: Lists claims for persistent volumes, showing their status and details.
    • Example: kubectl get pvc
    • Additional Detail: Ensures that volume claims are correctly bound to persistent volumes.
  8. ReplicaSets Overview
    • Description: Lists ReplicaSets that maintain a specified number of pod replicas.
    • Example: kubectl get replicasets
    • Additional Detail: Useful for managing and scaling applications.
  9. ReplicaSet Details
    • Description: Provides detailed information about a specific ReplicaSet.
    • Example: kubectl describe replicaset [replicaset-name]
    • Additional Detail: Helps diagnose scaling and replication issues.
  10. Horizontal Pod Autoscalers
    • Description: Lists Horizontal Pod Autoscalers, which automatically adjust the number of pod replicas based on CPU utilization.
    • Example: kubectl get hpa
    • Additional Detail: Useful for managing dynamic scaling of applications.

Kuberenetes ( Kubectl ) Networking Commands

  1. Ingress Resources
    • Description: Lists Ingress resources that manage external access to services.
    • Example: kubectl get ingress
    • Additional Detail: Helps manage and configure external traffic routing.
  2. Service Endpoints
    • Description: Shows endpoints associated with services, which map services to pod IP addresses.
    • Example: kubectl get endpoints
    • Additional Detail: Useful for understanding how services are exposed.
  3. Network Policy List
    • Description: Lists network policies that control traffic between pods.
    • Example: kubectl get networkpolicies
    • Additional Detail: Helps manage network security and traffic flow.
  4. Network Policy Details
    • Description: Provides details about a specific network policy, including its rules.
    • Example: kubectl describe networkpolicy [policy-name]
    • Additional Detail: Useful for ensuring correct network isolation.
  5. Secrets List
    • Description: Lists secrets, which store sensitive data like passwords or tokens.
    • Example: kubectl get secrets
    • Additional Detail: Manage and verify sensitive data in the cluster.
  6. Secret Details
    • Description: Provides detailed information about a specific secret.
    • Example: kubectl describe secret [secret-name]
    • Additional Detail: Useful for checking secret configurations and values.

Kuberenetes ( Kubectl ) Configuration and Management

  1. Update ConfigMap
    • Description: Applies updates to an existing ConfigMap, which contains configuration data.
    • Example: kubectl apply -f [configmap.yaml]
    • Additional Detail: Enables updating configuration data used by applications.
  2. Deployment Overview
    • Description: Lists all deployments in a namespace or the entire cluster.
    • Example: kubectl get deployments
    • Additional Detail: Essential for managing and scaling application deployments.
  3. Deployment Details
    • Description: Provides detailed information about a specific deployment, including its strategy and rollout status.
    • Example: kubectl describe deployment [deployment-name]
    • Additional Detail: Helps diagnose deployment issues and manage updates.
  4. Rollback Deployment
    • Description: Reverts a deployment to a previous version in case of issues with the current version.
    • Example: kubectl rollout undo deployment/[deployment-name]
    • Additional Detail: Useful for quickly recovering from failed updates.
  5. Deployment Rollout Status
    • Description: Shows the status of a deployment rollout to monitor progress and completion.
    • Example: kubectl rollout status deployment/[deployment-name]
    • Additional Detail: Helps track the deployment progress and resolve issues.
  6. Create Namespace
    • Description: Creates a new namespace to logically separate resources within the cluster.
    • Example: kubectl create namespace [namespace-name]
    • Additional Detail: Useful for organizing resources and managing access.
  7. Delete Namespace
    • Description: Removes a namespace and all associated resources, which can be useful for cleanup.
    • Example: kubectl delete namespace [namespace-name]
    • Additional Detail: Ensure you want to remove all resources within the namespace before deletion.
  8. Resource Usage Statistics
    • Description: Provides current usage statistics for pods and nodes, helping monitor resource consumption.
    • Example: kubectl top pods
    • Additional Detail: kubectl top nodes provides similar information for nodes.
  9. Manage Contexts
    • Description: Lists and switches between Kubernetes contexts to manage different clusters or environments.
    • Example: kubectl config get-contexts
    • Additional Detail: kubectl config use-context [context-name] allows switching to a different context.

Kuberenetes ( Kubectl ) Backup and Restore Commands

  1. Cluster Configuration Backup
    • Description: Creates a backup of the entire cluster configuration, capturing all resources and their states.
    • Example: kubectl get all --all-namespaces -o yaml > backup.yaml
    • Additional Detail: Useful for disaster recovery and migrating cluster configurations.
  2. Cluster Configuration Restore
    • Description: Restores cluster configuration from a backup file, reapplying all previously backed-up resources.
    • Example: kubectl apply -f backup.yaml
    • Additional Detail: Ensure the backup file is up-to-date and compatible with the current cluster.
  3. Namespace Backup
    • Description: Backs up all resources within a specific namespace, facilitating namespace-level disaster recovery.
    • Example: kubectl get all -n [namespace] -o yaml > namespace-backup.yaml
    • Additional Detail: Enables selective restoration of namespaces.
  4. Namespace Restore
    • Description: Restores resources in a namespace from a backup file, reapplying all backed-up resources.
    • Example: kubectl apply -f namespace-backup.yaml
    • Additional Detail: Ensure compatibility with current cluster configurations.
  5. Pod Data Backup
    • Description: Creates a backup of data stored in a specific pod, useful for preserving application state.
    • Example: kubectl exec [pod-name] -- tar czf /backup/data.tar.gz /data
    • Additional Detail: Use tar or similar tools for compressing and backing up data.
  6. Pod Data Restore
    • Description: Restores data to a specific pod from a backup file, facilitating data recovery.
    • Example: kubectl cp /backup/data.tar.gz [pod-name]:/data
    • Additional Detail: Use kubectl cp to copy files from the local machine to the pod.
  7. Persistent Volumes Backup
    • Description: Creates a backup of data from persistent volumes, which store critical data for applications.
    • Example: kubectl exec [pod-name] -- dd if=/dev/sda1 bs=1M | gzip > /backup/pv-backup.gz
    • Additional Detail: dd and gzip tools are used for backing up and compressing volume data.
  8. Persistent Volumes Restore
    • Description: Restores data to persistent volumes from a backup file, enabling data recovery.
    • Example: kubectl cp /backup/pv-backup.gz [pod-name]:/data
    • Additional Detail: Ensure the backup data is correctly formatted and compatible.
  9. Backup All Configurations
    • Description: Creates a comprehensive backup of all resource configurations in the cluster.
    • Example: kubectl get all -o yaml > all-configs-backup.yaml
    • Additional Detail: Useful for full-cluster recovery or migration.
  10. Restore All Configurations
    • Description: Restores all resource configurations from a backup file, reapplying all backed-up resources.
    • Example: kubectl apply -f all-configs-backup.yaml
    • Additional Detail: Ensure the backup file is complete and up-to-date.

Kuberenetes ( Kubectl ) Miscellaneous Commands

  1. DaemonSets Overview
    • Description: Lists all DaemonSets in the cluster, which ensure that a copy of a pod runs on all or selected nodes.
    • Example: kubectl get daemonsets
    • Additional Detail: Useful for managing background processes running on all nodes.
  2. DaemonSet Details
    • Description: Provides detailed information about a specific DaemonSet, including its pods and node assignments.
    • Example: kubectl describe daemonset [daemonset-name]
    • Additional Detail: Helps diagnose issues related to DaemonSets.
  3. Job List
    • Description: Lists all jobs and their statuses, which are used for batch processing.
    • Example: kubectl get jobs
    • Additional Detail: Useful for monitoring batch job completion and status.
  4. Job Details
    • Description: Displays detailed information about a specific job, including its pods and status.
    • Example: kubectl describe job [job-name]
    • Additional Detail: Helps in troubleshooting job failures and successes.
  5. CronJobs List
    • Description: Lists all CronJobs, which schedule periodic tasks to run at specified intervals.
    • Example: kubectl get cronjobs
    • Additional Detail: Useful for managing scheduled tasks within the cluster.
  6. CronJob Details
    • Description: Provides details about a specific CronJob, including its schedule and associated jobs.
    • Example: kubectl describe cronjob [cronjob-name]
    • Additional Detail: Helps diagnose scheduling and execution issues.
  7. Resource Quotas
    • Description: Lists all resource quotas in a namespace, which limit resource usage like CPU and memory.
    • Example: kubectl get resourcequotas
    • Additional Detail: Ensures resources are used efficiently and prevents over-allocation.
  8. Resource Quota Details
    • Description: Provides detailed information about a specific resource quota, including its limits and usage.
    • Example: kubectl describe resourcequota [quota-name]
    • Additional Detail: Helps manage resource allocation and enforce quotas.
  9. Available API Resources
    • Description: Lists all API resources available in the cluster, including custom resources.
    • Example: kubectl api-resources
    • Additional Detail: Useful for understanding the types of resources that can be managed.
  10. API Versions
    • Description: Displays all available API versions in the cluster, which indicate the versioning of Kubernetes APIs.
    • Example: kubectl api-versions
    • Additional Detail: Helps in determining compatibility and available features.
  11. Check Cluster Health
    • Description: Monitors the health and status of the cluster components.
    • Example: kubectl get componentstatuses
    • Additional Detail: Ensures that cluster components like etcd and the API server are functioning correctly.
  12. List All Resources
    • Description: Lists all resources in the cluster, providing a comprehensive overview of the current state.
    • Example: kubectl get all --all-namespaces
    • Additional Detail: Useful for a complete snapshot of the cluster’s resource status.
  13. Configuration Check
    • Description: Validates configuration files before applying them, ensuring they are error-free.
    • Example: kubectl apply --dry-run -f [file.yaml]
    • Additional Detail: Helps catch errors before actual deployment.
  14. View Current Context
    • Description: Displays the current Kubernetes context, including cluster, user, and namespace information.
    • Example: kubectl config current-context
    • Additional Detail: Useful for verifying the active context and managing multiple clusters.
  15. Switch Context
    • Description: Changes the active Kubernetes context to manage different clusters or environments.
    • Example: kubectl config use-context [context-name]
    • Additional Detail: Facilitates working with multiple Kubernetes clusters or namespaces.