Kubernetes and Cloud Native Associate (KCNA)Kubernetes FundamentalsMedium

A new container image has been pushed to a registry, and a developer needs to update an existing Deployment named `frontend-app` to use this new image. The Deployment currently uses `my-app:v1.0`, and the new image is `my-app:v2.0`. Which `kubectl` command would efficiently update the container image without manually editing the YAML manifest?

  1. A`kubectl replace deployment/frontend-app --image=my-app:v2.0`
  2. B`kubectl rollout update deployment/frontend-app --image=my-app:v2.0`
  3. C`kubectl set image deployment/frontend-app my-container=my-app:v2.0`
  4. D`kubectl edit deployment frontend-app --set image my-container=my-app:v2.0`
Show answer & explanation

Correct answer: C. `kubectl set image deployment/frontend-app my-container=my-app:v2.0`

The `kubectl set image` command is specifically designed for updating the image of a container within a resource like a Deployment. It takes the resource type/name and the container=image mapping as arguments, triggering a rolling update.

Why the other options are wrong

  • A. `kubectl replace` requires a full manifest file to replace an existing resource, and it doesn't have an `--image` flag for direct image updates.
  • B. `kubectl rollout` is used to manage the rollout process (e.g., `status`, `history`, `undo`), but `update` is not a valid subcommand for setting an image directly.
  • D. `kubectl edit` opens an editor for manual YAML modification, which is not 'without manually editing'. There is no `--set image` flag for `edit`.

`kubectl set image`

A `kubectl` command used to update the image of a container in a Pod, Deployment, or other workload resource. It initiates a rolling update to apply the change.

  • Used for updating container images.
  • Triggers a rolling update for Deployments.
  • Syntax: `kubectl set image <resource_type>/<name> <container_name>=<new_image>`.

Memory trick: Set Image for Smooth Sailing.

More Kubernetes Fundamentals questions