kubectl
Manage Kubernetes clusters
TLDR
List information about a resource with more details
Update specified pod with the label 'unhealthy' and the value 'true'
List all resources with different types
Display resource (CPU/Memory/Storage) usage of nodes or pods
Print the address of the master and cluster services
Display an explanation of a specific field
Print the logs for a container in a pod or specified resource
Run command in an existing pod
SYNOPSIS
kubectl [command] [TYPE] [NAME | -f FILENAME] [flags]
Common commands include:
get: Display one or many resources.
describe: Show details of a specific resource.
apply: Apply a configuration to a resource by filename or stdin.
delete: Delete resources by filename, stdin, resources and names, or by resources and label selector.
logs: Print the logs for a container in a pod.
exec: Execute a command in a container.
create: Create a resource from a file or stdin.
TYPE is a resource type (e.g., pod, deployment, service).
NAME is the name of a specific resource.
FILENAME is the path to a resource definition file (e.g., YAML or JSON).
PARAMETERS
--kubeconfig=
Path to the kubeconfig file to use for CLI requests.
--context=
The name of the kubeconfig context to use.
--namespace, -n
If present, the namespace scope for the CLI request.
--server=
The address and port of the Kubernetes API server.
--cluster=
The name of the kubeconfig cluster to use.
--user=
The name of the kubeconfig user to use.
--output, -o
Output format. One of: json|yaml|wide|name|go-template|jsonpath|jsonpath-as-json|jsonpath-file|template|templatefile.
--dry-run=
Must be "client", "server", or "none". If client, only print the object that would be sent, without sending it. If server, apply the configuration to the server, but don't persist it.
--validate=
If true, use a schema to validate the input before sending it. 'strict' or 'warn'.
--help
Show help for a command.
DESCRIPTION
kubectl is the primary command-line tool for interacting with and managing Kubernetes clusters. It allows users to run commands against Kubernetes clusters, which communicate with the cluster's API server. With kubectl, users can deploy applications, inspect and manage cluster resources, view logs, and execute commands inside running containers. It's an indispensable tool for both cluster administrators and application developers working with Kubernetes. kubectl supports defining Kubernetes resources using YAML or JSON files, enabling declarative management of applications and infrastructure. It provides a comprehensive set of verbs (e.g., create, get, describe, apply, delete) and resources (e.g., pods, deployments, services) to cover the entire lifecycle of applications on Kubernetes. It seamlessly integrates with various authentication mechanisms to securely access the cluster.
CAVEATS
kubectl client version compatibility is crucial; it's generally recommended that the client version be within one minor version of the Kubernetes cluster's API server.
Operating on the correct Kubernetes context and namespace is vital to prevent unintended changes to the wrong cluster or environment. Users must be mindful of their current context, especially when dealing with multiple clusters or namespaces.
While kubectl offers direct editing capabilities (e.g., kubectl edit), using declarative configuration files with kubectl apply -f is generally preferred for consistency, version control, and easier rollback.
All actions performed by kubectl are subject to the Role-Based Access Control (RBAC) policies configured on the Kubernetes cluster. Users require appropriate permissions to execute specific commands and manage resources.
KUBECONFIG MANAGEMENT
kubectl relies on a kubeconfig file (default location: ~/.kube/config) to find the information necessary to choose a cluster, a user, and a namespace. A kubeconfig file contains configurations for contexts, which are combinations of clusters, users, and namespaces. Users can switch between different clusters and environments using kubectl config use-context <context-name>, simplifying multi-cluster management.
PLUGINS
kubectl provides a flexible plugin mechanism, allowing users to extend its capabilities by creating executable files prefixed with kubectl- in their system's PATH. When a command like kubectl my-plugin is executed, kubectl looks for kubectl-my-plugin and runs it. This allows for custom commands, integrations with other tools, and specialized cluster operations, enhancing the tool's versatility beyond its core functionality.
HISTORY
kubectl emerged alongside the Kubernetes project, initially developed by Google and subsequently released as open-source. As Kubernetes gained widespread adoption, kubectl quickly established itself as the indispensable command-line interface for managing and interacting with Kubernetes clusters. Its design principles mirror Kubernetes' declarative approach, allowing users to define the desired state of their applications and infrastructure.
The tool has undergone continuous development, with new features and improvements being added in alignment with Kubernetes release cycles. Its consistent interface across various Kubernetes versions and distributions has cemented its role as the universal client for the ecosystem, empowering developers and operations teams worldwide.