Advanced kubectl Commands: Essential Guide for Kubernetes Management (Part 2)

Ram Kumar

Ram Kumar

November 1, 20245 min read

Advanced kubectl Commands: Essential Guide for Kubernetes Management (Part 2)

In the first part of our kubectl command series, we covered essential commands for basic cluster interaction and resource management. Now, in Part 2, we dive deeper with Commands 25-50, introducing advanced features and powerful commands to enhance your Kubernetes management toolkit. These commands help with resource inspection, configuration, and specific workload management.

26. Display JSON Output for Any Resource

kubectl get <resource-type> <resource-name> -o json

Using -o json provides a complete JSON representation of a specified resource. This format is ideal for scripting, configuration management, and precise resource inspection.

27. Display YAML Output for Any Resource

kubectl get <resource-type> <resource-name> -o yaml

Retrieving a resource in YAML format with -o yaml is particularly useful for inspecting configurations, backing up resource settings, or using as a template for new resources.

28. Get All Resources in a Namespace

kubectl get all -n <namespace>

Displays all resources (pods, services, deployments, etc.) in a namespace, offering a complete view of an environment and allowing you to assess all active resources and dependencies.

29. Patch a Resource

kubectl patch <resource-type> <resource-name> -p '{"spec":{"key":"value"}}' -n <namespace>


The patch command applies partial updates to a resource, useful for quick fixes. It doesn’t require reapplying the entire YAML configuration, saving time for minor changes or test updates.

30. Annotate a Resource

kubectl annotate <resource-type> <resource-name> <key>=<value> -n <namespace>


Annotations allow you to add custom metadata to resources. This can be used by monitoring and automation tools to track and manage resources based on custom information.

31. Label a Resource

kubectl label <resource-type> <resource-name> <key>=<value> -n <namespace>

Labels enable categorization and selection of resources for targeted actions like scaling, monitoring, and clean-up. They’re key for organizing resources and driving deployment strategies.

32. View All Nodes with Labels

kubectl get nodes --show-labels

Displays all nodes with their assigned labels. Node labels help with resource placement strategies and affinity rules, enabling efficient workload distribution.

33. Expose a Deployment as a Service

kubectl expose deployment <deployment-name> --type=<service-type> --port=<port>

This command creates a service for a deployment, exposing it to the cluster or externally depending on the service type (ClusterIP, NodePort, LoadBalancer). It’s critical for application access and networking.

34. Check Rollout Status of a Deployment

kubectl rollout status deployment/<deployment-name> -n <namespace>

Monitors the progress of a deployment’s rollout, confirming if updates are successfully applied or encountering issues. Useful for tracking deployments after applying changes.

35. Rollback a Deployment

kubectl rollout undo deployment/<deployment-name> -n <namespace>

Rolls back a deployment to its previous configuration. This command is essential for quick recovery if a deployment introduces bugs or issues.

36. List Resource Quotas in a Namespace

kubectl get resourcequotas -n <namespace>

Shows any resource quotas within a namespace, such as limits on CPU, memory, and storage usage. Understanding quotas helps you manage resources effectively and avoid over-allocation.

37. Get Events in a Namespace

kubectl rollout undo deployment/<deployment-name> -n <namespace>

Lists recent events in a namespace. Events log resource-related activity, including failures, restarts, and scheduling delays, making them invaluable for troubleshooting.

38. Describe a Service

kubectl describe service <service-name> -n <namespace>

Provides details about a service, including endpoints, port mappings, and selectors. This command is helpful for verifying connections and debugging network-related issues.

39. Set Image for a Deployment

kubectl set image deployment/<deployment-name> <container-name>=<new-image> -n <namespace>

This command allows you to update the image for a container within a deployment, making it a key step in deploying updated application versions.

40. List StatefulSets in a Namespace

kubectl get statefulsets -n <namespace>

Displays all StatefulSets in a namespace, which are crucial for managing stateful applications that require persistent data or ordered deployments, like databases.

41. Delete a Namespace

kubectl delete namespace <namespace-name>

Deletes a namespace along with all resources within it. It’s a powerful command for environment cleanup, though it should be used with caution to avoid unintended data loss.

42. Create a Secret from Literal Values

kubectl create secret generic <secret-name> --from-literal=<key>=<value> -n <namespace>

Generates a Kubernetes Secret from specified key-value pairs, allowing you to securely manage sensitive data like API keys and passwords.

43. List Ingress Resources in a Namespace

kubectl get ingress -n <namespace>

Lists all ingress resources, which control external access to services. This is helpful for tracking ingress configurations for routing and security.

44. Describe an Ingress

kubectl describe ingress <ingress-name> -n <namespace>

Displays details about an ingress resource, including associated services and routing rules. Use this command to troubleshoot routing issues and ensure traffic is directed correctly.

45. Edit Context for the Current Cluster

kubectl config set-context --current --namespace=<namespace>

Sets the default namespace for the current context, so you no longer need to specify -n <namespace> for each command. This saves time and reduces the risk of operating in the wrong namespace.

46. List All Contexts

kubectl config get-contexts

Displays all contexts configured in kubectl, each representing different clusters or environments. This is helpful for switching between clusters and managing access points.

47. Switch Context

kubectl config use-context <context-name>

Switches to the specified context, changing the cluster or environment you’re currently working in. This command is essential when managing multiple clusters.

48. View Persistent Volumes

kubectl get pv

Lists all Persistent Volumes (PVs) in the cluster, which represent physical storage resources available to Kubernetes. Monitoring PVs helps in managing available storage and tracking usage.

49. View Persistent Volume Claims in a Namespace

kubectl get pvc -n <namespace>

Displays Persistent Volume Claims (PVCs), which are storage requests for applications. Viewing PVCs is key for tracking application storage needs and ensuring data persistence.

50. Create a ConfigMap from a File

kubectl create configmap <configmap-name> --from-file=<file-path> -n <namespace>

Generates a ConfigMap from a file, useful for managing configuration data as files within Kubernetes. ConfigMaps decouple configuration from code, making configurations easier to manage and update.

Wrapping Up Part 2

With these advanced commands, you can now delve deeper into Kubernetes management with more control and efficiency. Part 2 focused on commands for configuration management, resource quotas, debugging, and environment control. Mastering these commands allows for a stronger command over Kubernetes resources and is essential for effectively managing production clusters. Stay tuned for Part 3, where we’ll cover even more specialized kubectl commands for scaling, debugging, and optimizing cluster performance.

[@portabletext/react] Unknown block type "image", specify a component for it in the `components.types` prop

Happy managing!

Previous: Building a Micro UI Architecture with React, TypeScript, Tailwind CSS, Webpack Module Federation, and Docker
Next: Advanced kubectl Commands: Deep Dive into Kubernetes Management (Part 3)