Kubectl Commands - Kubernetes Management Tool

Master kubectl commands for Kubernetes management. Explore essential commands for listing resources, explaining configurations, managing contexts, and enabling shell autocompletion.

Kubectl Commands

Kubectl is the command-line interface for interacting with Kubernetes clusters. Mastering its commands is crucial for managing your containerized applications effectively. This guide provides essential kubectl commands to help you navigate and control your Kubernetes environment.

Kubectl Command Reference

Below is a collection of frequently used kubectl commands, categorized for clarity. These commands cover common tasks such as displaying help, listing resources, explaining configurations, and managing cluster contexts.

Getting Help and Information

# To display list of all available commands:
kubectl -h

# To display an explanation of a specific command:
kubectl command_name -h

# To display complete list of supported resources:
kubectl api-resources

# To display an explanation of a specific resource:
kubectl explain resource_name

# To display an explanation of a specific field of resource:
kubectl explain resource_name.field_name

# To display list of global command-line options:
kubectl options

Managing Resources and Contexts

# To set up `kubectl` autocomplete in bash (press Tab to use):
source <(kubectl completion bash)

# To display all resources in all namespaces:
kubectl get all -A

# To order events by `creationTimestamp`:
kubectl get events --sort-by='.metadata.creationTimestamp'

# To switch context of a specific cluster:
kubectl config use-context CONTEXT_NAME [options]

Advanced Kubectl Usage

Beyond basic commands, kubectl offers powerful features for deeper inspection and management of your Kubernetes deployments. Understanding these can significantly enhance your operational efficiency.

Kubernetes API Resources

The kubectl api-resources command is invaluable for understanding the types of objects you can manage within your Kubernetes cluster. It lists all supported resources and their short names.

Command Autocompletion

Setting up shell autocompletion for kubectl can drastically speed up your workflow. The command source <(kubectl completion bash) enables tab completion for commands and resource names.

Context Management

For users managing multiple Kubernetes clusters, kubectl config use-context allows seamless switching between different cluster configurations, ensuring you are always interacting with the correct environment.

Further Resources

For more in-depth information on kubectl and Kubernetes, refer to the official Kubernetes documentation: