set
These commands help you make changes to existing application resources.
List the environment variables defined on a deployments ‘sample-build’
kubectl set env deployment/sample-build --list
List the environment variables defined on all pods
kubectl set env pods --all --list
Output modified deployment in YAML, and does not alter the object on the server
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
kubectl set env rc --all ENV=prod
Import environment from a secret
kubectl set env --from=secret/mysecret deployment/myapp
Import environment from a config map with a prefix
kubectl set env --from=configmap/myconfigmap --prefix=MYSQL_ deployment/myapp
Import specific keys from a config map
kubectl set env --keys=my-example-key --from=configmap/myconfigmap deployment/myapp
Remove the environment variable ENV from a deployment definition on disk and # update the deployment config on the server
kubectl set env -f deploy.json ENV-
Set some of the local shell environment into a deployment config on the server
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.
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
image
Set a deployment’s nginx container image to ‘nginx:1.9.1’, and its busybox container image to ‘busybox’
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’
kubectl set image deployments,rc nginx=nginx:1.9.1 --all
Update image of all containers of daemonset abc to ‘nginx:1.9.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
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
Name | Shorthand | Default | Usage |
---|---|---|---|
all | false | Select all resources, including uninitialized ones, in the namespace of the specified resource types | |
allow-missing-template-keys | true | If 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-run | none | Must 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-manager | kubectl-set | Name of the manager used to track field ownership. | |
filename | f | [] | Filename, directory, or URL to files identifying the resource to get from a server. |
kustomize | k | Process the kustomization directory. This flag can’t be used together with -f or -R. | |
local | false | If true, set image will NOT contact api-server but run locally. | |
output | o | Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file. | |
record | false | Record 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. | |
recursive | R | false | Process the directory used in -f, —filename recursively. Useful when you want to manage related manifests organized within the same directory. |
selector | l | Selector (label query) to filter on, not including uninitialized ones, supports ‘=’, ‘==’, and ‘!=’.(e.g. -l key1=value1,key2=value2) | |
show-managed-fields | false | If true, keep the managedFields when printing objects in JSON or YAML format. | |
template | Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates []. |
Set a deployments nginx container cpu limits to “200m” and memory to “512Mi”
kubectl set resources deployment nginx -c=nginx --limits=cpu=200m,memory=512Mi
Remove the resource requests for resources on containers in nginx
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
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.
Usage
$ kubectl set resources (-f FILENAME | TYPE NAME) ([--limits=LIMITS & --requests=REQUESTS]
Flags
selector
Set the labels and selector before creating a deployment/service pair
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 -
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]
Name | Shorthand | Default | Usage |
---|---|---|---|
all | false | Select all resources in the namespace of the specified resource types | |
allow-missing-template-keys | true | If 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-run | none | Must 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-manager | kubectl-set | Name of the manager used to track field ownership. | |
filename | f | [] | identifying the resource. |
local | false | If true, annotation will NOT contact api-server but run locally. | |
output | o | Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file. | |
record | false | Record 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. | |
recursive | R | true | Process the directory used in -f, —filename recursively. Useful when you want to manage related manifests organized within the same directory. |
resource-version | If 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-fields | false | If true, keep the managedFields when printing objects in JSON or YAML format. | |
template | Template 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]. |
Set deployment nginx-deployment’s service account to serviceaccount1
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
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
subject
Update a cluster role binding for serviceaccount1
kubectl set subject clusterrolebinding admin --serviceaccount=namespace:serviceaccount1
Update a role binding for user1, user2, and group1
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]
Name | Shorthand | Default | Usage |
---|---|---|---|
all | false | Select all resources, including uninitialized ones, in the namespace of the specified resource types | |
allow-missing-template-keys | true | If 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-run | none | Must 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-manager | kubectl-set | Name of the manager used to track field ownership. | |
filename | f | [] | Filename, directory, or URL to files the resource to update the subjects |
group | [] | Groups to bind to the role | |
kustomize | k | Process the kustomization directory. This flag can’t be used together with -f or -R. | |
local | false | If true, set subject will NOT contact api-server but run locally. | |
output | o | Output format. One of: json|yaml|name|go-template|go-template-file|template|templatefile|jsonpath|jsonpath-as-json|jsonpath-file. | |
recursive | R | false | Process the directory used in -f, —filename recursively. Useful when you want to manage related manifests organized within the same directory. |
selector | l | Selector (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-fields | false | If true, keep the managedFields when printing objects in JSON or YAML format. | |
template | Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates []. |