Learn Kubernetes (Kubectl) Commands List
Contents
- 1 Basic Kubernetes Commands
- 2 Advanced Kubernetes Commands
- 3 Kuberenetes ( Kubectl ) Troubleshooting Commands
- 4 Kuberenetes ( Kubectl ) Networking Commands
- 5 Kuberenetes ( Kubectl ) Configuration and Management
- 6 Kuberenetes ( Kubectl ) Backup and Restore Commands
- 7 Kuberenetes ( Kubectl ) Miscellaneous Commands
Basic Kubernetes Commands
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- ReplicaSet Details
- Description: Provides detailed information about a specific ReplicaSet.
- Example:
kubectl describe replicaset [replicaset-name]
- Additional Detail: Helps diagnose scaling and replication issues.
- 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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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
andgzip
tools are used for backing up and compressing volume data.
- 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.
- 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.
- 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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.