liveBook · Manning

Imperative programming

As explained in Kubernetes for Developers

An imperative command directly instructs a system to perform an operational action rather than declaring the desired resulting state.

Imperative programming instructs Kubernetes to perform one operational action at a time. A command creates, exposes, updates, or scales an object instead of describing the complete desired state in configuration. section 3.3

Imperative commands can deploy an application quickly, but configuration-based declarative operation is generally preferred for workplace use. Reproducing an imperative deployment requires remembering or storing the command sequence, while configuration can be reapplied across environments. section 3.3

Typical imperative operations include:

  • Create a Deployment from a container image with kubectl create deployment. section 3.3
  • Expose the Deployment through a LoadBalancer Service with kubectl expose. section 3.3
  • Change a running image with kubectl set image or scale replicas with kubectl scale. section 3.3 section 11.1.2
  • Create a Secret directly, or generate YAML without creating it by adding --dry-run=client -o yaml. section 11.4.3

Deploying and updating a timeserver

Run kubectl create deployment timeserver --image=docker.io/wdenniss/timeserver:1, expose it with a LoadBalancer Service, then update the image using kubectl set image deployment timeserver timeserver=wdenniss/timeserver:2. Each command changes the cluster directly. section 3.3

$ kubectl create deployment timeserver \
   --image=docker.io/wdenniss/timeserver:1
deployment.apps/timeserver created

Inspect the concrete kubectl command that creates a Deployment imperatively; related listings show exposure, status, and image updates. section 3.3

How Kubernetes for Developers covers it

How deep it goes
Explains command examples and operational trade-offs, but stops short of detailing Kubernetes controller internals.
Examples
Demonstrates timeserver deployment, LoadBalancer exposure, image replacement, replica scaling, and Secret generation.
What it assumes
Assumes a Kubernetes cluster and kubectl context capable of creating and modifying resources.
What you can do after
Operate/configure Kubernetes resources directly with kubectl commands and export resulting cluster configuration as YAML.
How it connects
Contrasts with declarative configuration and connects to Kubernetes Deployments, Services, Secrets, and configuration as code.

Also explained in

Compare how 14 books explain imperative programming →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes for Developers. The section links point to the parts of the book it draws on.