kubectl
- Pronunciation
- KOOB kun-TROHL
In short
kubectl is the command-line tool for Kubernetes: it sends requests to a cluster's API server to deploy applications, inspect them and change them.
What is kubectl?
kubectl is how most people talk to a Kubernetes cluster. Every command becomes a request to the cluster's API server: kubectl get pods lists the running pods, kubectl describe shows the details and recent events of one object, kubectl logs prints a container's output, and kubectl exec runs a command inside a container. Which cluster it talks to, and as whom, comes from a kubeconfig file, usually ~/.kube/config, which can hold several clusters as named contexts.
There are two ways to change a cluster. Imperative commands such as kubectl create deployment or kubectl scale say what to do right now. The declarative way, preferred for anything that lasts, is to describe the desired state in YAML files and run kubectl apply -f, which compares the files with what is running and makes only the changes needed. Keeping those files in Git turns every change into a reviewed commit, and tools built on the same idea, such as Helm and GitOps controllers, take it further.
kubectl is to a cluster what a remote control is to a television: the work happens elsewhere, and the tool only sends instructions. Because it can change anything the credentials allow, access to production is usually narrowed with role-based access control (RBAC), and kubectl diff or --dry-run=server show what a change would do before it is applied. People say its name in several ways, such as kube control, kube cuttle or kube C-T-L.
Key takeaways
- kubectl is the Kubernetes command-line tool; every command is a call to the API server.
get,describe,logsandexecinspect what is running.kubectl apply -fmakes the cluster match YAML files that describe the desired state.- The kubeconfig file decides which cluster kubectl talks to, and as whom.
Example
kubectl config get-contexts # the clusters you can talk to
kubectl get pods -n shop # pods in the "shop" namespace
kubectl describe pod web-7d9f-x2kq -n shop # details and recent events
kubectl logs -f deploy/web -n shop # follow the logs of the deployment's pod
kubectl diff -f web.yaml # what applying the file would change
kubectl apply -f web.yaml # make the cluster match the file
kubectl rollout undo deploy/web -n shop # go back to the previous versionReaders ask
What is the difference between kubectl apply and kubectl create?
kubectl create makes a new object and fails if it already exists. kubectl apply creates the object or updates it to match the file, so you can run it again and again; that is why it suits configuration kept in files.
Where does kubectl get its credentials?
From a kubeconfig file, ~/.kube/config unless the KUBECONFIG variable points elsewhere. It lists clusters, users and contexts, each context pairing a cluster with a user and a namespace; kubectl config use-context switches between them.
See also
- KubernetesDevOps & Cloud, p. 32Kubernetes is an open-source system that automates deploying, scaling, and managing containerized applications across a cluster of machines.
- PodDevOps & Cloud, p. 41A pod is the smallest deployable unit in Kubernetes: one or more containers that share a network address and storage and are scheduled together on one node.
- HelmDevOps & Cloud, p. 26Helm is the package manager for Kubernetes: it bundles an app's configuration files into a chart you can install, upgrade and roll back with one command.
- YAMLDevOps & Cloud, p. 54YAML is a human-readable data format that uses indentation instead of brackets, widely used for configuration files in DevOps tools and CI/CD pipelines.
- GitOpsDevOps & Cloud, p. 23GitOps is a way of managing infrastructure and deployments where Git holds the desired state of a system and an automated agent keeps the live system in sync.
- ContainerDevOps & Cloud, p. 12A container is a lightweight, isolated package that bundles an application with its dependencies and runs it on the host's shared operating system kernel.
- Command Line InterfaceOperating Systems, p. 2A command line interface (CLI) is a text-based way of using a computer: you type a command with options and arguments, press Enter, and read its output.
Sources
Spotted a mistake or something missing on this page?Suggest an edit