liveBook · Manning

Kubectl apply

As explained in Kubernetes for Developers

kubectl apply is a kubectl command that creates or updates Kubernetes resources from declarative configuration.

kubectl apply creates or updates Kubernetes resources from YAML configuration. It supports a declarative workflow: edit the configuration, then apply it to the cluster instead of constructing every object imperatively. section 11.4.3

Common deployment uses include:

  • Generate a Secret manifest with kubectl create secret --dry-run=client -o yaml, save it, and later apply it with kubectl apply -f secret.yaml. section 11.4.3
  • Update a Deployment manifest, such as changing its container image, and apply the change to start a rolling update. section 4.2.1
  • Modify a blue/green Deployment or Service manifest and apply each change, including switching the Service selector to the prepared version. section 4.2.3

Applying a production image change

Change the image in a Deployment manifest, then run kubectl apply -f deploy.yaml. With a rolling-update strategy, Kubernetes replaces Pods while continuing to serve requests; with blue/green, apply the manifest changes and switch the Service selector after the new Deployment is ready. section 4.2.1 section 4.2.3

Listing 4.6 Chapter04/4.2.1_RollingUpdate/deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: timeserver
spec:
  replicas: 3
  selector:
    matchLabels:
      pod: timeserver-pod
  strategy:
    type: RollingUpdate    #1
    rollingUpdate:         #2
      maxSurge: 2          #2
      maxUnavailable: 1    #2
  template:
    metadata:
      labels:
        pod: timeserver-pod
    spec:
      containers:
      - name: timeserver-container
        image: docker.io/wdenniss/timeserver:3
#1 Rolling update strategy
#2 Optional configuration

A Deployment manifest shows the kind of versioned configuration that kubectl apply updates, including the image and rolling-update settings. section 4.2.1

How Kubernetes for Developers covers it

How deep it goes
Demonstrates manifest-driven application and rollout sequences, but stops short of detailed command reference or API internals.
Examples
Uses Secret YAML, a timeserver Deployment, rolling-update settings, and blue/green Service-selector changes.
What it assumes
Assumes familiarity with YAML resource manifests and basic Kubernetes Deployments, Pods, Services, and Secrets.
What you can do after
Operate/configure Kubernetes resources by editing YAML and applying changes for Secret creation and controlled application rollouts.
How it connects
Connects declarative configuration with Kubernetes resources, Secrets, Deployments, Services, rolling updates, and blue-green deployment.

Also explained in

Compare how 6 books explain kubectl apply →

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.