Welcome to Part 3 of our kubectl series! In this installment, we continue with Commands 51-75, covering advanced debugging techniques, resource scaling, storage management, and environment clean-up. These commands will help you gain full control over your Kubernetes clusters, making daily operations smoother and more efficient.
51. Copy a File from a Pod
kubectl cp <namespace>/<pod-name>:/<source-path> <local-destination-path>
Copies files from a pod to your local machine. This command is useful for retrieving logs or configuration files for local analysis, especially when troubleshooting.
52. Drain a Node
kubectl drain <node-name> --ignore-daemonsets --force
Prepares a node for maintenance by evicting all pods, except those managed by DaemonSets. This is essential for updating or replacing nodes without disrupting workloads.
53. Cordon a Node
kubectl cordon <node-name>
Marks a node as unschedulable, preventing new pods from being scheduled on it. Use this command for nodes undergoing maintenance while keeping existing pods running.
54. Uncordon a Node
kubectl uncordon <node-name>
Reverses a cordon operation, making the node schedulable again. It’s useful for bringing a node back online after maintenance.
55. Delete a Pod with a Grace Period
kubectl delete pod <pod-name> --grace-period=<seconds> -n <namespace>
Specifies a grace period in seconds for pod deletion, allowing applications time to gracefully shut down and clean up. This is especially useful for stateful workloads.
56. Apply All YAML Files in a Directory
kubectl apply -f <directory-path>/
Applies all YAML files in a directory, streamlining batch deployments of multiple resources. This is helpful for orchestrating more complex applications with various resource files.
57. Force a Pod to Restart
kubectl rollout restart deployment <deployment-name> -n <namespace>
Forces a deployment to recreate its pods, which can refresh configurations or apply updates without changing the image version. This is useful when pods require a restart but no code changes.
58. Get API Resources
kubectl api-resources
Lists all resource types available in the Kubernetes API. This command is handy when exploring new resource types and ensuring compatibility across different Kubernetes versions.
59. Get API Versions
kubectl api-versions
Displays supported API versions, helping you verify compatibility with YAML specifications and ensuring you’re using the correct versions for each resource type.
60. Delete Completed Jobs
kubectl delete job -l <label-selector> --field-selector=status.successful=1 -n <namespace>
Deletes jobs that have successfully completed, which helps save resources and maintain a clean environment by removing old, completed batch processes.
61. View Horizontal Pod Autoscalers (HPA)
kubectl get hpa -n <namespace>
Lists all Horizontal Pod Autoscalers, which dynamically adjust the number of pod replicas based on CPU or memory usage, allowing for resource-efficient scaling.
62. Create a Namespace with a Resource Quota
kubectl create namespace <namespace-name> && kubectl create quota <quota-name> --hard=cpu=1,memory=1Gi -n <namespace-name>
Creates a namespace with specific CPU and memory limits, helping manage resource allocation in environments with limited capacity.
63. View Custom Metrics
kubectl top pods --containers -n <namespace>
Provides detailed CPU and memory metrics at the container level within a pod, useful for fine-grained resource monitoring and optimizing usage.
64. Delete All Resources by Label
kubectl delete all -l <label-key>=<label-value> -n <namespace>
Deletes all resources in a namespace that match a specific label, which is useful for targeted clean-up, especially after test runs or temporary deployments.
65. Rollout Pause a Deployment
kubectl rollout pause deployment <deployment-name> -n <namespace>
Pauses a deployment, preventing it from rolling out new replicas or updates. This command is helpful for troubleshooting deployment issues in real-time.
66. Rollout Resume a Deployment
kubectl rollout resume deployment <deployment-name> -n <namespace>
Resumes a paused deployment, allowing it to continue its rollout. This command is useful after troubleshooting or testing fixes on deployment issues.
67. Delete Pods by Label Selector
kubectl delete pods -l <label-key>=<label-value> -n <namespace>
Removes pods based on a label selector. Use this to quickly terminate a specific set of pods, particularly during testing or maintenance.
68. Scale a StatefulSet
kubectl scale statefulset <statefulset-name> --replicas=<number> -n <namespace>
Changes the replica count for a StatefulSet, used to manage applications with stable identities, like databases or stateful workloads.
69. List DaemonSets in a Namespace
kubectl get daemonsets -n <namespace>
Lists all DaemonSets, which ensure that each node runs a copy of the specified pod. DaemonSets are useful for node-level applications like monitoring agents.
70. Get Pods Sorted by Age
kubectl get pods --sort-by=.metadata.creationTimestamp -n <namespace>
Lists pods in a namespace sorted by age, which helps you identify the oldest or most recent resources for inspection.
71. Apply a Label to All Pods in a Namespace
kubectl label pods --all <label-key>=<label-value> -n <namespace>
Adds a label to all pods in a namespace. Labels help organize resources and make it easier to apply bulk actions across multiple pods.
72. Debug a Node
kubectl debug node/<node-name> -it --image=busybox
Launches a debug pod on a specified node, useful for inspecting networking, storage, or system configurations at the node level.
73. Suspend a CronJob
kubectl patch cronjob <cronjob-name> -p '{"spec":{"suspend":true}}' -n <namespace>
Temporarily suspends a CronJob, preventing it from creating new jobs. This is useful when you need to pause scheduled jobs without deleting the CronJob configuration.
74. Get Configurations for All Running Pods
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{.spec.containers[*].image}{"\n\n"}{end}' -n <namespace>
Lists all running pods and their container images. This command provides a quick overview of deployed versions, useful for version tracking and auditing.
75. View Endpoints for Services
kubectl get endpoints -n <namespace>
Lists endpoints associated with services in a namespace, showing which pods are receiving traffic. This is essential for verifying service availability and routing.
Wrapping Up Part 3
With these advanced commands, you now have a robust toolkit for handling Kubernetes management tasks, from monitoring and scaling to debugging and organizing resources. Part 3 has provided in-depth, specialized kubectl commands, enabling you to manage Kubernetes environments more effectively and efficiently.
Mastering these commands will make you proficient in handling even the most complex Kubernetes clusters. Stay tuned for more advanced Kubernetes topics to further strengthen your management skills!

