set

Configure application resources.

These commands help you make changes to existing application resources.

Usage

$ kubectl set SUBCOMMAND


env

Update deployment ‘registry’ with a new environment variable

  1. kubectl set env deployment/registry STORAGE_DIR=/local

List the environment variables defined on a deployments ‘sample-build’

  1. kubectl set env deployment/sample-build --list

List the environment variables defined on all pods

  1. kubectl set env pods --all --list

Output modified deployment in YAML, and does not alter the object on the server

  1. kubectl set env deployment/sample-build STORAGE_DIR=/data -o yaml

Update all containers in all replication controllers in the project to have ENV=prod

  1. kubectl set env rc --all ENV=prod

Import environment from a secret

  1. kubectl set env --from=secret/mysecret deployment/myapp

Import environment from a config map with a prefix

  1. kubectl set env --from=configmap/myconfigmap --prefix=MYSQL_ deployment/myapp

Import specific keys from a config map

  1. kubectl set env --keys=my-example-key --from=configmap/myconfigmap deployment/myapp

Remove the environment variable ENV from container ‘c1’ in all deployment configs

  1. kubectl set env deployments --all --containers="c1" ENV-

Remove the environment variable ENV from a deployment definition on disk and # update the deployment config on the server

  1. kubectl set env -f deploy.json ENV-

Set some of the local shell environment into a deployment config on the server

  1. env | grep RAILS_ | kubectl set env -e - deployment/registry

Update environment variables on a pod template.

List environment variable definitions in one or more pods, pod templates. Add, update, or remove container environment variable definitions in one or more pod templates (within replication controllers or deployment configurations). View or modify the environment variable definitions on all containers in the specified pods or pod templates, or just those that match a wildcard.

If “—env -“ is passed, environment variables can be read from STDIN using the standard env syntax.

Possible resources include (case insensitive):

pod (po), replicationcontroller (rc), deployment (deploy), daemonset (ds), statefulset (sts), cronjob (cj), replicaset (rs)

Usage

$ kubectl set env RESOURCE/NAME KEY_1=VAL_1 ... KEY_N=VAL_N

Flags

NameShorthandDefaultUsage
allfalseIf true, select all resources in the namespace of the specified resource types
allow-missing-template-keystrueIf true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
containersc*The names of containers in the selected pod templates to change - may use wildcards
dry-runnoneMust be “none”, “server”, or “client”. If client strategy, only print the object that would be sent, without sending it. If server strategy, submit server-side request without persisting the resource.
enve[]Specify a key-value pair for an environment variable to set into each container.
field-managerkubectl-setName of the manager used to track field ownership.
filenamef[]Filename, directory, or URL to files the resource to update the env
fromThe name of a resource from which to inject environment variables
keys[]Comma-separated list of keys to import from specified resource
kustomizekProcess the kustomization directory. This flag can’t be used together with -f or -R.
listfalseIf true, display the environment and any changes in the standard format. this flag will removed when we have kubectl view env.
localfalseIf true, set env will NOT contact api-server but run locally.
outputoOutput format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
overwritetrueIf true, allow environment to be overwritten, otherwise reject updates that overwrite existing environment.
prefixPrefix to append to variable names
recursiveRfalseProcess the directory used in -f, —filename recursively. Useful when you want to manage related manifests organized within the same directory.
resolvefalseIf true, show secret or configmap references when listing variables
selectorlSelector (label query) to filter on
show-managed-fieldsfalseIf true, keep the managedFields when printing objects in JSON or YAML format.
templateTemplate string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].

image

Set a deployment’s nginx container image to ‘nginx:1.9.1’, and its busybox container image to ‘busybox’

  1. kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1

Update all deployments’ and rc’s nginx container’s image to ‘nginx:1.9.1’

  1. kubectl set image deployments,rc nginx=nginx:1.9.1 --all

Update image of all containers of daemonset abc to ‘nginx:1.9.1’

  1. kubectl set image daemonset abc *=nginx:1.9.1

Print result (in yaml format) of updating nginx container image from local file, without hitting the server

  1. kubectl set image -f path/to/file.yaml nginx=nginx:1.9.1 --local -o yaml

Update existing container image(s) of resources.

Possible resources include (case insensitive):

pod (po), replicationcontroller (rc), deployment (deploy), daemonset (ds), statefulset (sts), cronjob (cj), replicaset (rs)

Usage

$ kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1 ... CONTAINER_NAME_N=CONTAINER_IMAGE_N

Flags

NameShorthandDefaultUsage
allfalseSelect all resources, including uninitialized ones, in the namespace of the specified resource types
allow-missing-template-keystrueIf true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
dry-runnoneMust be “none”, “server”, or “client”. If client strategy, only print the object that would be sent, without sending it. If server strategy, submit server-side request without persisting the resource.
field-managerkubectl-setName of the manager used to track field ownership.
filenamef[]Filename, directory, or URL to files identifying the resource to get from a server.
kustomizekProcess the kustomization directory. This flag can’t be used together with -f or -R.
localfalseIf true, set image will NOT contact api-server but run locally.
outputoOutput format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
recordfalseRecord current kubectl command in the resource annotation. If set to false, do not record the command. If set to true, record the command. If not set, default to updating the existing annotation value only if one already exists.
recursiveRfalseProcess the directory used in -f, —filename recursively. Useful when you want to manage related manifests organized within the same directory.
selectorlSelector (label query) to filter on, not including uninitialized ones, supports ‘=’, ‘==’, and ‘!=’.(e.g. -l key1=value1,key2=value2)
show-managed-fieldsfalseIf true, keep the managedFields when printing objects in JSON or YAML format.
templateTemplate string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].

resources

Set a deployments nginx container cpu limits to “200m” and memory to “512Mi”

  1. kubectl set resources deployment nginx -c=nginx --limits=cpu=200m,memory=512Mi

Set the resource request and limits for all containers in nginx

  1. kubectl set resources deployment nginx --limits=cpu=200m,memory=512Mi --requests=cpu=100m,memory=256Mi

Remove the resource requests for resources on containers in nginx

  1. kubectl set resources deployment nginx --limits=cpu=0,memory=0 --requests=cpu=0,memory=0

Print the result (in yaml format) of updating nginx container limits from a local, without hitting the server

  1. kubectl set resources -f path/to/file.yaml --limits=cpu=200m,memory=512Mi --local -o yaml

Specify compute resource requirements (CPU, memory) for any resource that defines a pod template. If a pod is successfully scheduled, it is guaranteed the amount of resource requested, but may burst up to its specified limits.

For each compute resource, if a limit is specified and a request is omitted, the request will default to the limit.

Possible resources include (case insensitive): Use “kubectl api-resources” for a complete list of supported resources..

Usage

$ kubectl set resources (-f FILENAME | TYPE NAME) ([--limits=LIMITS & --requests=REQUESTS]

Flags

NameShorthandDefaultUsage
allfalseSelect all resources, including uninitialized ones, in the namespace of the specified resource types
allow-missing-template-keystrueIf true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
containersc*The names of containers in the selected pod templates to change, all containers are selected by default - may use wildcards
dry-runnoneMust be “none”, “server”, or “client”. If client strategy, only print the object that would be sent, without sending it. If server strategy, submit server-side request without persisting the resource.
field-managerkubectl-setName of the manager used to track field ownership.
filenamef[]Filename, directory, or URL to files identifying the resource to get from a server.
kustomizekProcess the kustomization directory. This flag can’t be used together with -f or -R.
limitsThe resource requirement requests for this container. For example, ‘cpu=100m,memory=256Mi’. Note that server side components may assign requests depending on the server configuration, such as limit ranges.
localfalseIf true, set resources will NOT contact api-server but run locally.
outputoOutput format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
recordfalseRecord current kubectl command in the resource annotation. If set to false, do not record the command. If set to true, record the command. If not set, default to updating the existing annotation value only if one already exists.
recursiveRfalseProcess the directory used in -f, —filename recursively. Useful when you want to manage related manifests organized within the same directory.
requestsThe resource requirement requests for this container. For example, ‘cpu=100m,memory=256Mi’. Note that server side components may assign requests depending on the server configuration, such as limit ranges.
selectorlSelector (label query) to filter on, not including uninitialized ones,supports ‘=’, ‘==’, and ‘!=’.(e.g. -l key1=value1,key2=value2)
show-managed-fieldsfalseIf true, keep the managedFields when printing objects in JSON or YAML format.
templateTemplate string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].

selector

Set the labels and selector before creating a deployment/service pair

  1. kubectl create service clusterip my-svc --clusterip="None" -o yaml --dry-run=client | kubectl set selector --local -f - 'environment=qa' -o yaml | kubectl create -f -
  2. kubectl create deployment my-dep -o yaml --dry-run=client | kubectl label --local -f - environment=qa -o yaml | kubectl create -f -

Set the selector on a resource. Note that the new selector will overwrite the old selector if the resource had one prior to the invocation of ‘set selector’.

A selector must begin with a letter or number, and may contain letters, numbers, hyphens, dots, and underscores, up to 63 characters. If —resource-version is specified, then updates will use this resource version, otherwise the existing resource-version will be used. Note: currently selectors can only be set on Service objects.

Usage

$ kubectl set selector (-f FILENAME | TYPE NAME) EXPRESSIONS [--resource-version=version]

Flags

NameShorthandDefaultUsage
allfalseSelect all resources in the namespace of the specified resource types
allow-missing-template-keystrueIf true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
dry-runnoneMust be “none”, “server”, or “client”. If client strategy, only print the object that would be sent, without sending it. If server strategy, submit server-side request without persisting the resource.
field-managerkubectl-setName of the manager used to track field ownership.
filenamef[]identifying the resource.
localfalseIf true, annotation will NOT contact api-server but run locally.
outputoOutput format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
recordfalseRecord current kubectl command in the resource annotation. If set to false, do not record the command. If set to true, record the command. If not set, default to updating the existing annotation value only if one already exists.
recursiveRtrueProcess the directory used in -f, —filename recursively. Useful when you want to manage related manifests organized within the same directory.
resource-versionIf non-empty, the selectors update will only succeed if this is the current resource-version for the object. Only valid when specifying a single resource.
show-managed-fieldsfalseIf true, keep the managedFields when printing objects in JSON or YAML format.
templateTemplate string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].

serviceaccount

Set deployment nginx-deployment’s service account to serviceaccount1

  1. kubectl set serviceaccount deployment nginx-deployment serviceaccount1

Print the result (in YAML format) of updated nginx deployment with the service account from local file, without hitting the API server

  1. kubectl set sa -f nginx-deployment.yaml serviceaccount1 --local --dry-run=client -o yaml

Update the service account of pod template resources.

Possible resources (case insensitive) can be:

replicationcontroller (rc), deployment (deploy), daemonset (ds), job, replicaset (rs), statefulset

Usage

$ kubectl set serviceaccount (-f FILENAME | TYPE NAME) SERVICE_ACCOUNT

Flags

NameShorthandDefaultUsage
allfalseSelect all resources, including uninitialized ones, in the namespace of the specified resource types
allow-missing-template-keystrueIf true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
dry-runnoneMust be “none”, “server”, or “client”. If client strategy, only print the object that would be sent, without sending it. If server strategy, submit server-side request without persisting the resource.
field-managerkubectl-setName of the manager used to track field ownership.
filenamef[]Filename, directory, or URL to files identifying the resource to get from a server.
kustomizekProcess the kustomization directory. This flag can’t be used together with -f or -R.
localfalseIf true, set serviceaccount will NOT contact api-server but run locally.
outputoOutput format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
recordfalseRecord current kubectl command in the resource annotation. If set to false, do not record the command. If set to true, record the command. If not set, default to updating the existing annotation value only if one already exists.
recursiveRfalseProcess the directory used in -f, —filename recursively. Useful when you want to manage related manifests organized within the same directory.
show-managed-fieldsfalseIf true, keep the managedFields when printing objects in JSON or YAML format.
templateTemplate string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].

subject

Update a cluster role binding for serviceaccount1

  1. kubectl set subject clusterrolebinding admin --serviceaccount=namespace:serviceaccount1

Update a role binding for user1, user2, and group1

  1. kubectl set subject rolebinding admin --user=user1 --user=user2 --group=group1

Print the result (in YAML format) of updating rolebinding subjects from a local, without hitting the server

  1. kubectl create rolebinding admin --role=admin --user=admin -o yaml --dry-run=client | kubectl set subject --local -f - --user=foo -o yaml

Update the user, group, or service account in a role binding or cluster role binding.

Usage

$ kubectl set subject (-f FILENAME | TYPE NAME) [--user=username] [--group=groupname] [--serviceaccount=namespace:serviceaccountname] [--dry-run=server|client|none]

Flags

NameShorthandDefaultUsage
allfalseSelect all resources, including uninitialized ones, in the namespace of the specified resource types
allow-missing-template-keystrueIf true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
dry-runnoneMust be “none”, “server”, or “client”. If client strategy, only print the object that would be sent, without sending it. If server strategy, submit server-side request without persisting the resource.
field-managerkubectl-setName of the manager used to track field ownership.
filenamef[]Filename, directory, or URL to files the resource to update the subjects
group[]Groups to bind to the role
kustomizekProcess the kustomization directory. This flag can’t be used together with -f or -R.
localfalseIf true, set subject will NOT contact api-server but run locally.
outputoOutput format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file.
recursiveRfalseProcess the directory used in -f, —filename recursively. Useful when you want to manage related manifests organized within the same directory.
selectorlSelector (label query) to filter on, not including uninitialized ones, supports ‘=’, ‘==’, and ‘!=’.(e.g. -l key1=value1,key2=value2)
serviceaccount[]Service accounts to bind to the role
show-managed-fieldsfalseIf true, keep the managedFields when printing objects in JSON or YAML format.
templateTemplate string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].