LinuxCommandLibrary

kubectl-set

Update specific fields of Kubernetes resources

TLDR

Update the image of a container in a deployment

$ kubectl set image [[deploy|deployment]]/[deployment_name] [container_name]=[image]
copy

Update the image of all containers in a deployment
$ kubectl set image [[deploy|deployment]]/[deployment_name] *=[image]
copy

Set resource limits (CPU and memory) on a deployment
$ kubectl set resources [[deploy|deployment]]/[deployment_name] --limits cpu=[cpu_limit],memory=[memory_limit]
copy

Set an environment variable on a deployment
$ kubectl set env [[deploy|deployment]]/[deployment_name] [variable_name]=[value]
copy

Remove an environment variable from a deployment
$ kubectl set env [[deploy|deployment]]/[deployment_name] [variable_name]-
copy

Import environment variables from a secret or ConfigMap
$ kubectl set env --from [secret|configmap]/[resource_name] [[deploy|deployment]]/[deployment_name]
copy

Update the service account of a deployment
$ kubectl set [[sa|serviceaccount]] [[deploy|deployment]]/[deployment_name] [service_account_name]
copy

SYNOPSIS

kubectl set SUBCOMMAND [NAME] resources [--container=<name>] [--dry-run=<strategy>] [--local] [--record] [--resource-version=<version>] [--timeout=<duration>]

PARAMETERS

env
    Update environment variables on a pod or pod template

image
    Update container image(s) for a pod template or container

resources
    Update resource requests/limits on pod template containers

serviceaccount
    Update serviceAccount on a resource

subject
    Update subjects in RBAC RoleBinding/ClusterRoleBinding

selector
    Set selector on a resource

trigger
    Update triggers on ReplicationController

--container
    Container name; if omitted, selects all

--dry-run
    "client|server|none"; preview without applying

--local
    Client-side update; output to stdout

--record
    Record current kubectl command in resource annotations

--resource-version
    Precondition resource version

--timeout
    Timeout for server-side operations

DESCRIPTION

kubectl set is a powerful subcommand of the Kubernetes CLI tool kubectl designed to update specific fields on resources like deployments, pods, and replica sets without a full replacement or restart. It supports targeted modifications via subcommands such as image, env, resources, enabling quick changes like updating container images, environment variables, or CPU/memory limits.

For instance, to bump a deployment's container image: kubectl set image deployment/myapp nginx=nginx:1.16. This triggers a rolling update with minimal disruption. It leverages strategic merge patches, preserving unspecified fields, and integrates with selectors for bulk operations.

Ideal for CI/CD pipelines and production tweaks, it requires appropriate RBAC permissions. Changes can be previewed with --dry-run, recorded for rollbacks via --record, and scoped to namespaces. Unlike kubectl patch, it's declarative for common fields but limited to supported subcommands.

Enhances declarative configs by bridging imperious updates, reducing kubectl apply cycles in dynamic clusters.

CAVEATS

Limited to specific fields/subcommands; uses strategic merge patch which may not support custom resources. Requires write permissions; not for complex JSON patches (use kubectl patch). Rolling updates may cause brief unavailability.

EXAMPLES

kubectl set image deployment/nginx nginx=nginx:1.16.1 --record
kubectl set env deployment/myapp DB_HOST=prod-db --containers=app
kubectl set resources deployment/myapp -c=app --requests=cpu=100m,memory=128Mi --limits=cpu=200m

SELECTORS

Use -l app=myapp for label-based resource selection, e.g., kubectl set image deployments -l app=myapp nginx=latest

HISTORY

Introduced in Kubernetes v1.11 (2017) as a convenience wrapper over patches. Subcommands expanded through v1.20+; now stable for core workloads. Developed by Kubernetes SIG-CLI for streamlined resource management.

SEE ALSO

kubectl patch(1), kubectl apply(1), kubectl edit(1), kubectl rollout(1)

Copied to clipboard