Sign In

Curriculum 8: Updating Resources

kubectl set

12 min · 15 XP

kubectl set -- Quick Field Updates

kubectl set provides shortcut commands for updating the most commonly changed fields on resources. Instead of writing JSON patches or editing YAML, you update fields with simple, readable commands.

kubectl set image

Update container images on Deployments, DaemonSets, StatefulSets, or Jobs:

# Update the nginx container image in a deployment
kubectl set image deployment/my-app nginx=nginx:1.27

# Update multiple containers at once
kubectl set image deployment/my-app nginx=nginx:1.27 sidecar=fluentd:v2

# Update all containers to the same image
kubectl set image deployment/my-app '*=nginx:1.27'

This triggers a rolling update automatically. You can watch the rollout:

kubectl rollout status deployment/my-app

kubectl set env

Add, update, or remove environment variables:

# Set environment variables
kubectl set env deployment/my-app DATABASE_URL=postgres://db:5432/app LOG_LEVEL=info

# Set an env var from a configmap
kubectl set env deployment/my-app --from=configmap/app-config

# Set an env var from a secret
kubectl set env deployment/my-app --from=secret/db-creds

# Remove an environment variable (trailing dash)
kubectl set env deployment/my-app LOG_LEVEL-

# View current environment variables
kubectl set env deployment/my-app --list

kubectl set resources

Update CPU and memory requests and limits:

# Set both requests and limits
kubectl set resources deployment/my-app \
  --requests=cpu=100m,memory=128Mi \
  --limits=cpu=500m,memory=256Mi

# Set resources for a specific container
kubectl set resources deployment/my-app -c nginx \
  --requests=cpu=200m,memory=256Mi \
  --limits=cpu=1,memory=512Mi

# Remove resource limits
kubectl set resources deployment/my-app --limits=cpu=0,memory=0

kubectl set serviceaccount

Change the service account used by a workload:

kubectl set serviceaccount deployment/my-app app-service-account

kubectl set selector

Update the selector on a Service:

kubectl set selector service/my-service app=my-app-v2

Dry Run with set

All set subcommands support dry run for previewing changes:

# Preview the image change without applying it
kubectl set image deployment/my-app nginx=nginx:1.27 --dry-run=client -o yaml

Key Takeaways

  • kubectl set image is the fastest way to update container images
  • kubectl set env manages environment variables including references to ConfigMaps and Secrets
  • kubectl set resources adjusts CPU and memory requests and limits
  • All set commands trigger rolling updates on Deployments
  • Use --dry-run=client -o yaml to preview changes before applying