liveBook · Manning

Kubectl

As explained in Learn Docker in a Month of Lunches, Second Edition

kubectl is the command-line tool for querying and managing Kubernetes clusters through the Kubernetes API.

Once Kubernetes groups machines into a cluster, you work with the cluster rather than choosing a machine for each container. kubectl is the command-line tool for sending queries and commands to that cluster. To deploy an application, describe its resources in YAML and send the model with kubectl; Kubernetes then works to make the running state match it. section 15.1

Start with a cluster you can reach, then use a few repeatable commands:

  • On Docker Desktop, run `kubectl config use-context docker-desktop`, then `kubectl get nodes`. A node listing is a useful first check that the cluster is running. section 15.1
  • Run `kubectl apply -f ch15/exercises/pi/pod.yaml` to send a Pod specification to the cluster. Use `kubectl get pods` to see its status, `kubectl logs pi` to read its output, and `kubectl get pods --watch` to follow changes; exit the watch with Ctrl-C or Cmd-C. section 15.2
  • For more detail, use `kubectl describe pod numbers-api`. To reach a Pod’s HTTP server locally, run `kubectl port-forward pod/numbers-api 8015:80`, then open `http://localhost:8015/rng`; stop forwarding with Ctrl-C or Cmd-C. section 15.2

A Pod whose container finishes and exits may show repeated restarts rather than a successful, finished deployment: its current state does not match the desired running state. Repeated failures can lead to `CrashLoopBackOff`. Check the Pod status and logs before assuming the YAML describes a suitable long-running component. section 15.2

Check configuration inside a container

After `kubectl apply -f ch15/exercises/sleep-configured`, inspect the ConfigMap with `kubectl describe configmap sleep-config-env`. Then run `kubectl exec deploy/sleep -- printenv` to check that its values reached the container as environment variables. `exec` can also run other commands inside a container. section 15.4

# deploy the Pod YAML:
kubectl apply -f ch15/exercises/pi/pod.yaml
# show the Pod table:
kubectl get pods
# print the logs from the pi Pod:
kubectl logs pi
# show Pods again with the watch flag which prints any changes:
kubectl get pods --watch
# Ctrl-C or Cmd-C to exit the watch

Use this command sequence as a compact first exercise: deploy a Pod, inspect it, read its logs, and watch its status change. section 15.2

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Works through context selection, deployment, inspection, logs, port forwarding and container commands without explaining kubectl internals.
Examples
Deploys a Pod, watches its restarts, forwards traffic to an HTTP Pod and checks ConfigMap values inside a container.
What it assumes
Introduces clusters and the YAML application model before asking readers to use a reachable Kubernetes context.
What you can do after
Apply it via kubectl to deploy YAML, verify resources, inspect failures and run checks inside containers.
How it connects
Connects kubectl to Kubernetes clusters, the Kubernetes API, YAML, Pods, Services and ConfigMaps.

Also explained in

Compare how 11 books explain kubectl →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.