The kubectl command
kubectl is the command-line utility administrators use to control a Kubernetes
cluster. It talks to the kube-APIserver on the
control plane: it turns your command-line
entries into Kubernetes API calls, sends them to the kube-APIserver, and figures
out which part of the control plane to reach. Before it can do any of that, it
has to be pointed at a cluster and given credentials.
How a command reaches the cluster
Say an administrator wants to list the Pods in a cluster. Once kubectl is
connected with valid credentials, they run kubectl get pods:
kubectlconverts the command into an API call and sends it over HTTPS to the kube-APIserver on the cluster's control plane.- The kube-APIserver processes the request by querying etcd for the state.
- It returns the results to
kubectlover HTTPS. kubectlinterprets the response and prints it at the command prompt.
Configuration
kubectl stores its own configuration in $HOME/.kube/config, a hidden file in
your home directory. That file holds the list of clusters and the credentials
attached to each one. The credentials themselves come from GKE, delivered by the
gcloud command.
To connect kubectl to a GKE cluster, first fetch the credentials with
gcloud container clusters get-credentials (see
Working with clusters for the full command set). This works
in any environment where both gcloud and kubectl are installed - both ship by
default in Cloud Shell.
- Config path:
$HOME/.kube/config(hidden.kubefolder in the home directory). - Contents: the list of clusters + the credentials attached to each.
- Credentials source: GKE, delivered through the
gcloudcommand. - View the config: open the file, or run
kubectl config view. get-credentialsonly needs to run once per cluster in Cloud Shell - the.kubedirectory persists in$HOME. Rerunning it for a different cluster updates the file with that cluster's credentials.
kubectl config view shows the configuration of the kubectl command itself
(clusters, contexts, credentials). Every other kubectl command
(get, describe, …) shows the configuration of the cluster and its workloads.
Don't confuse the two.
kubectl manages the internal state of an existing cluster. It cannot
create new clusters or change the shape (node count, machine type) of existing
ones - that is done through the GKE control plane, which gcloud and the Google
Cloud console talk to. After the kubeconfig is written, kubectl references it
automatically and connects to the default cluster without prompting.
Command syntax
A kubectl command has four parts: command, type, name, and optional
flags.
- command - the action to perform:
get,describe,logs,exec, … Some commands show information; others change the cluster's configuration. - TYPE - the kind of object the command acts on:
pods,deployments,nodes, or other objects (including the cluster itself). - NAME - the specific object. Optional for listing commands:
kubectl get podslists every Pod, whilekubectl get pod my-test-appreturns just that one. - flags - optional "special requests" appended to the end.
-o=yaml- print an object's full state as YAML:kubectl get pod my-test-app -o=yaml. Useful for recreating an object elsewhere.-o=wide- wider listing:kubectl get pods -o=widealso shows which node each Pod runs on.--kubeconfig/--context- override which config file or cluster the command targets, when you have not configured a default.