1
1
---
2
- title : Managing Resources
2
+ title : Managing Workloads
3
3
content_type : concept
4
4
reviewers :
5
5
- janetkuo
@@ -8,14 +8,14 @@ weight: 40
8
8
9
9
<!-- overview -->
10
10
11
- You've deployed your application and exposed it via a service . Now what? Kubernetes provides a
11
+ You've deployed your application and exposed it via a Service . Now what? Kubernetes provides a
12
12
number of tools to help you manage your application deployment, including scaling and updating.
13
13
14
14
<!-- body -->
15
15
16
16
## Organizing resource configurations
17
17
18
- Many applications require multiple resources to be created, such as a Deployment and a Service.
18
+ Many applications require multiple resources to be created, such as a Deployment along with a Service.
19
19
Management of multiple resources can be simplified by grouping them together in the same file
20
20
(separated by ` --- ` in YAML). For example:
21
21
@@ -32,9 +32,9 @@ service/my-nginx-svc created
32
32
deployment.apps/my-nginx created
33
33
```
34
34
35
- The resources will be created in the order they appear in the file . Therefore, it's best to
36
- specify the service first, since that will ensure the scheduler can spread the pods associated
37
- with the service as they are created by the controller(s), such as Deployment.
35
+ The resources will be created in the order they appear in the manifest . Therefore, it's best to
36
+ specify the Service first, since that will ensure the scheduler can spread the pods associated
37
+ with the Service as they are created by the controller(s), such as Deployment.
38
38
39
39
` kubectl apply ` also accepts multiple ` -f ` arguments:
40
40
@@ -50,7 +50,7 @@ directory. If the tiers of your application bind to each other using DNS, you ca
50
50
the components of your stack together.
51
51
52
52
A URL can also be specified as a configuration source, which is handy for deploying directly from
53
- configuration files checked into GitHub :
53
+ manifests in your source control system :
54
54
55
55
``` shell
56
56
kubectl apply -f https://k8s.io/examples/application/nginx/nginx-deployment.yaml
@@ -60,6 +60,27 @@ kubectl apply -f https://k8s.io/examples/application/nginx/nginx-deployment.yaml
60
60
deployment.apps/my-nginx created
61
61
```
62
62
63
+ If you need to define more manifests, such as adding a ConfigMap, you can do that too.
64
+
65
+ ### External tools
66
+
67
+ This section lists only the most common tools used for managing workloads on Kubernetes. To see a larger list, view
68
+ [ Application definition and image build] ( https://landscape.cncf.io/guide#app-definition-and-development--application-definition-image-build )
69
+ in the {{< glossary_tooltip text="CNCF" term_id="cncf" >}} Landscape.
70
+
71
+ #### Helm {#external-tool-helm}
72
+
73
+ {{% thirdparty-content single="true" %}}
74
+
75
+ [ Helm] ( https://helm.sh/ ) is a tool for managing packages of pre-configured
76
+ Kubernetes resources. These packages are known as _ Helm charts_ .
77
+
78
+ #### Kustomize {#external-tool-kustomize}
79
+
80
+ [ Kustomize] ( https://kustomize.io/ ) traverses a Kubernetes manifest to add, remove or update configuration options.
81
+ It is available both as a standalone binary and as a [ native feature] ( /docs/tasks/manage-kubernetes-objects/kustomization/ )
82
+ of kubectl.
83
+
63
84
## Bulk operations in kubectl
64
85
65
86
Resource creation isn't the only operation that ` kubectl ` can perform in bulk. It can also extract
@@ -94,26 +115,32 @@ deployment.apps "my-nginx" deleted
94
115
service "my-nginx-svc" deleted
95
116
```
96
117
118
+ ### Chaining and filtering
119
+
97
120
Because ` kubectl ` outputs resource names in the same syntax it accepts, you can chain operations
98
121
using ` $() ` or ` xargs ` :
99
122
100
123
``` shell
101
- kubectl get $( kubectl create -f docs/concepts/cluster-administration/nginx/ -o name | grep service)
102
- kubectl create -f docs/concepts/cluster-administration/nginx/ -o name | grep service | xargs -i kubectl get {}
124
+ kubectl get $( kubectl create -f docs/concepts/cluster-administration/nginx/ -o name | grep service/ )
125
+ kubectl create -f docs/concepts/cluster-administration/nginx/ -o name | grep service/ | xargs -i kubectl get ' {} '
103
126
```
104
127
128
+ The output might be similar to:
129
+
105
130
``` none
106
131
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
107
132
my-nginx-svc LoadBalancer 10.0.0.208 <pending> 80/TCP 0s
108
133
```
109
134
110
- With the above commands, we first create resources under ` examples/application/nginx/ ` and print
135
+ With the above commands, first you create resources under ` examples/application/nginx/ ` and print
111
136
the resources created with ` -o name ` output format (print each resource as resource/name).
112
- Then we ` grep ` only the "service", and then print it with ` kubectl get ` .
137
+ Then you ` grep ` only the Service, and then print it with [ ` kubectl get ` ] ( /docs/reference/kubectl/generated/kubectl_get/ ) .
138
+
139
+ ### Recursive operations on local files
113
140
114
141
If you happen to organize your resources across several subdirectories within a particular
115
142
directory, you can recursively perform the operations on the subdirectories also, by specifying
116
- ` --recursive ` or ` -R ` alongside the ` --filename, -f ` flag .
143
+ ` --recursive ` or ` -R ` alongside the ` --filename ` / ` -f ` argument .
117
144
118
145
For instance, assume there is a directory ` project/k8s/development ` that holds all of the
119
146
{{< glossary_tooltip text="manifests" term_id="manifest" >}} needed for the development environment,
@@ -130,7 +157,7 @@ project/k8s/development
130
157
```
131
158
132
159
By default, performing a bulk operation on ` project/k8s/development ` will stop at the first level
133
- of the directory, not processing any subdirectories. If we had tried to create the resources in
160
+ of the directory, not processing any subdirectories. If you had tried to create the resources in
134
161
this directory using the following command, we would have encountered an error:
135
162
136
163
``` shell
@@ -141,7 +168,7 @@ kubectl apply -f project/k8s/development
141
168
error: you must provide one or more resources by argument or filename (.json|.yaml|.yml|stdin)
142
169
```
143
170
144
- Instead, specify the ` --recursive ` or ` -R ` flag with the ` --filename, -f ` flag as such :
171
+ Instead, specify the ` --recursive ` or ` -R ` command line argument along with the ` --filename ` / ` -f ` argument :
145
172
146
173
``` shell
147
174
kubectl apply -f project/k8s/development --recursive
@@ -153,10 +180,10 @@ deployment.apps/my-deployment created
153
180
persistentvolumeclaim/my-pvc created
154
181
```
155
182
156
- The ` --recursive ` flag works with any operation that accepts the ` --filename, -f ` flag such as:
157
- ` kubectl { create, get, delete, describe,rollout} ` etc .
183
+ The ` --recursive ` argument works with any operation that accepts the ` --filename ` / ` -f ` argument such as:
184
+ ` kubectl create ` , ` kubectl get` , ` kubectl delete` , ` kubectl describe` , or even ` kubectl rollout ` .
158
185
159
- The ` --recursive ` flag also works when multiple ` -f ` arguments are provided:
186
+ The ` --recursive ` argument also works when multiple ` -f ` arguments are provided:
160
187
161
188
``` shell
162
189
kubectl apply -f project/k8s/namespaces -f project/k8s/development --recursive
@@ -173,6 +200,90 @@ persistentvolumeclaim/my-pvc created
173
200
If you're interested in learning more about ` kubectl ` , go ahead and read
174
201
[ Command line tool (kubectl)] ( /docs/reference/kubectl/ ) .
175
202
203
+ ## Updating your application without an outage
204
+
205
+ At some point, you'll eventually need to update your deployed application, typically by specifying
206
+ a new image or image tag. ` kubectl ` supports several update operations, each of which is applicable
207
+ to different scenarios.
208
+
209
+ You can run multiple copies of your app, and use a _ rollout_ to gradually shift the traffic to
210
+ new healthy Pods. Eventually, all the running Pods would have the new software.
211
+
212
+ This section of the page guides you through how to create and update applications with Deployments.
213
+
214
+ Let's say you were running version 1.14.2 of nginx:
215
+
216
+ ``` shell
217
+ kubectl create deployment my-nginx --image=nginx:1.14.2
218
+ ```
219
+
220
+ ``` none
221
+ deployment.apps/my-nginx created
222
+ ```
223
+
224
+ Ensure that there is 1 replica:
225
+
226
+ ``` shell
227
+ kubectl scale --replicas 1 deployments/my-nginx --subresource=' scale' --type=' merge' -p ' {"spec":{"replicas": 1}}'
228
+ ```
229
+
230
+ ``` none
231
+ deployment.apps/my-nginx scaled
232
+ ```
233
+
234
+ and allow Kubernetes to add more temporary replicas during a rollout, by setting a _ surge maximum_ of
235
+ 100%:
236
+
237
+ ``` shell
238
+ kubectl patch --type=' merge' -p ' {"spec":{"strategy":{"rollingUpdate":{"maxSurge": "100%" }}}}'
239
+ ```
240
+
241
+ ``` none
242
+ deployment.apps/my-nginx patched
243
+ ```
244
+
245
+ To update to version 1.16.1, change ` .spec.template.spec.containers[0].image ` from ` nginx:1.14.2 `
246
+ to ` nginx:1.16.1 ` using ` kubectl edit ` :
247
+
248
+ ``` shell
249
+ kubectl edit deployment/my-nginx
250
+ # Change the manifest to use the newer container image, then save your changes
251
+ ```
252
+
253
+ That's it! The Deployment will declaratively update the deployed nginx application progressively
254
+ behind the scene. It ensures that only a certain number of old replicas may be down while they are
255
+ being updated, and only a certain number of new replicas may be created above the desired number
256
+ of pods. To learn more details about how this happens,
257
+ visit [ Deployment] ( /docs/concepts/workloads/controllers/deployment/ ) .
258
+
259
+ You can use rollouts with DaemonSets, Deployments, or StatefulSets.
260
+
261
+ ### Managing rollouts
262
+
263
+ You can use [ ` kubectl rollout ` ] ( /docs/reference/kubectl/generated/kubectl_rollout/ ) to manage a
264
+ progressive update of an existing application.
265
+
266
+ For example:
267
+
268
+ ``` shell
269
+ kubectl apply -f my-deployment.yaml
270
+
271
+ # wait for rollout to finish
272
+ kubectl rollout status deployment/my-deployment --timeout 10m # 10 minute timeout
273
+ ```
274
+
275
+ or
276
+
277
+ ``` shell
278
+ kubectl apply -f backing-stateful-component.yaml
279
+
280
+ # don't wait for rollout to finish, just check the status
281
+ kubectl rollout status statefulsets/backing-stateful-component --watch=false
282
+ ```
283
+
284
+ You can also pause, resume or cancel a rollout.
285
+ Visit [ ` kubectl rollout ` ] ( /docs/reference/kubectl/generated/kubectl_rollout/ ) to learn more.
286
+
176
287
## Canary deployments
177
288
178
289
<!-- TODO: make a task out of this for canary deployment, ref #42786-->
@@ -229,13 +340,10 @@ each release that will receive live production traffic (in this case, 3:1).
229
340
Once you're confident, you can update the stable track to the new application release and remove
230
341
the canary one.
231
342
232
- For a more concrete example, check the
233
- [tutorial of deploying Ghost](https://github.com/kelseyhightower/talks/tree/master/kubecon-eu-2016/demo#deploy-a-canary).
234
-
235
343
## Updating annotations
236
344
237
345
Sometimes you would want to attach annotations to resources. Annotations are arbitrary
238
- non-identifying metadata for retrieval by API clients such as tools, libraries, etc .
346
+ non-identifying metadata for retrieval by API clients such as tools or libraries .
239
347
This can be done with ` kubectl annotate`. For example:
240
348
241
349
` ` ` shell
@@ -253,7 +361,7 @@ metadata:
253
361
` ` `
254
362
255
363
For more information, see [annotations](/docs/concepts/overview/working-with-objects/annotations/)
256
- and [kubectl annotate](/docs/reference/generated/ kubectl/kubectl-commands/#annotate) document .
364
+ and [kubectl annotate](/docs/reference/kubectl/generated/kubectl_annotate/) .
257
365
258
366
# # Scaling your application
259
367
@@ -283,6 +391,7 @@ To have the system automatically choose the number of nginx replicas as needed,
283
391
ranging from 1 to 3, do :
284
392
285
393
` ` ` shell
394
+ # This requires an existing source of container and Pod metrics
286
395
kubectl autoscale deployment/my-nginx --min=1 --max=3
287
396
` ` `
288
397
@@ -292,8 +401,8 @@ horizontalpodautoscaler.autoscaling/my-nginx autoscaled
292
401
293
402
Now your nginx replicas will be scaled up and down as needed, automatically.
294
403
295
- For more information, please see [kubectl scale](/docs/reference/generated/ kubectl/kubectl-commands/#scale ),
296
- [kubectl autoscale](/docs/reference/generated/ kubectl/kubectl-commands/#autoscale ) and
404
+ For more information, please see [kubectl scale](/docs/reference/kubectl/generated/kubectl_scale/ ),
405
+ [kubectl autoscale](/docs/reference/kubectl/generated/kubectl_autoscale/ ) and
297
406
[horizontal pod autoscaler](/docs/tasks/run-application/horizontal-pod-autoscale/) document.
298
407
299
408
# # In-place updates of resources
@@ -305,7 +414,7 @@ Sometimes it's necessary to make narrow, non-disruptive updates to resources you
305
414
It is suggested to maintain a set of configuration files in source control
306
415
(see [configuration as code](https://martinfowler.com/bliki/InfrastructureAsCode.html)),
307
416
so that they can be maintained and versioned along with the code for the resources they configure.
308
- Then, you can use [`kubectl apply`](/docs/reference/generated/ kubectl/kubectl-commands/#apply )
417
+ Then, you can use [`kubectl apply`](/docs/reference/kubectl/generated/kubectl_apply/ )
309
418
to push your configuration changes to the cluster.
310
419
311
420
This command will compare the version of the configuration that you're pushing with the previous
@@ -320,23 +429,11 @@ kubectl apply -f https://k8s.io/examples/application/nginx/nginx-deployment.yaml
320
429
deployment.apps/my-nginx configured
321
430
` ` `
322
431
323
- Note that `kubectl apply` attaches an annotation to the resource in order to determine the changes
324
- to the configuration since the previous invocation. When it's invoked, `kubectl apply` does a
325
- three-way diff between the previous configuration, the provided input and the current
326
- configuration of the resource, in order to determine how to modify the resource.
327
-
328
- Currently, resources are created without this annotation, so the first invocation of `kubectl
329
- apply` will fall back to a two-way diff between the provided input and the current configuration
330
- of the resource. During this first invocation, it cannot detect the deletion of properties set
331
- when the resource was created. For this reason, it will not remove them.
332
-
333
- All subsequent calls to `kubectl apply`, and other commands that modify the configuration, such as
334
- ` kubectl replace` and `kubectl edit`, will update the annotation, allowing subsequent calls to
335
- ` kubectl apply` to detect and perform deletions using a three-way diff.
432
+ To learn more about the underlying mechanism, read [server-side apply](/docs/reference/using-api/server-side-apply/).
336
433
337
434
# ## kubectl edit
338
435
339
- Alternatively, you may also update resources with `kubectl edit` :
436
+ Alternatively, you may also update resources with [ `kubectl edit`](/docs/reference/kubectl/generated/kubectl_edit/) :
340
437
341
438
` ` ` shell
342
439
kubectl edit deployment/my-nginx
@@ -359,15 +456,17 @@ rm /tmp/nginx.yaml
359
456
This allows you to do more significant changes more easily. Note that you can specify the editor
360
457
with your `EDITOR` or `KUBE_EDITOR` environment variables.
361
458
362
- For more information, please see [kubectl edit](/docs/reference/generated/ kubectl/kubectl-commands/#edit) document .
459
+ For more information, please see [kubectl edit](/docs/reference/kubectl/generated/kubectl_edit/) .
363
460
364
461
# ## kubectl patch
365
462
366
- You can use `kubectl patch` to update API objects in place. This command supports JSON patch,
367
- JSON merge patch, and strategic merge patch. See
463
+ You can use [`kubectl patch`](/docs/reference/kubectl/generated/kubectl_patch/) to update API objects in place.
464
+ This subcommand supports JSON patch,
465
+ JSON merge patch, and strategic merge patch.
466
+
467
+ See
368
468
[Update API Objects in Place Using kubectl patch](/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/)
369
- and
370
- [kubectl patch](/docs/reference/generated/kubectl/kubectl-commands/#patch).
469
+ for more details.
371
470
372
471
# # Disruptive updates
373
472
@@ -385,48 +484,7 @@ deployment.apps/my-nginx deleted
385
484
deployment.apps/my-nginx replaced
386
485
` ` `
387
486
388
- # # Updating your application without a service outage
389
-
390
- At some point, you'll eventually need to update your deployed application, typically by specifying
391
- a new image or image tag, as in the canary deployment scenario above. `kubectl` supports several
392
- update operations, each of which is applicable to different scenarios.
393
-
394
- We'll guide you through how to create and update applications with Deployments.
395
-
396
- Let's say you were running version 1.14.2 of nginx :
397
-
398
- ` ` ` shell
399
- kubectl create deployment my-nginx --image=nginx:1.14.2
400
- ` ` `
401
-
402
- ` ` ` none
403
- deployment.apps/my-nginx created
404
- ` ` `
405
-
406
- with 3 replicas (so the old and new revisions can coexist) :
407
-
408
- ` ` ` shell
409
- kubectl scale deployment my-nginx --current-replicas=1 --replicas=3
410
- ` ` `
411
-
412
- ` ` ` none
413
- deployment.apps/my-nginx scaled
414
- ` ` `
415
-
416
- To update to version 1.16.1, change `.spec.template.spec.containers[0].image` from `nginx:1.14.2`
417
- to `nginx:1.16.1` using the previous kubectl commands.
418
-
419
- ` ` ` shell
420
- kubectl edit deployment/my-nginx
421
- ` ` `
422
-
423
- That's it! The Deployment will declaratively update the deployed nginx application progressively
424
- behind the scene. It ensures that only a certain number of old replicas may be down while they are
425
- being updated, and only a certain number of new replicas may be created above the desired number
426
- of pods. To learn more details about it, visit [Deployment page](/docs/concepts/workloads/controllers/deployment/).
427
487
428
488
# # {{% heading "whatsnext" %}}
429
489
430
490
- Learn about [how to use `kubectl` for application introspection and debugging](/docs/tasks/debug/debug-application/debug-running-pod/).
431
- - See [Configuration Best Practices and Tips](/docs/concepts/configuration/overview/).
432
-
0 commit comments