@@ -20,23 +20,18 @@ card:
20
20
<!-- overview -->
21
21
22
22
<!--
23
- See also: [Kubectl Overview](/docs/reference/kubectl/overview/) and [JsonPath Guide](/docs/reference/kubectl/jsonpath).
24
-
25
- This page is an overview of the `kubectl` command.
23
+ This page contains a list of commonly used `kubectl` commands and flags.
26
24
-->
27
- 另见: [ Kubectl 概述] ( /zh/docs/reference/kubectl/overview/ ) 和 [ JsonPath 指南] ( /zh/docs/reference/kubectl/jsonpath ) 。
28
-
29
- 本页面是 ` kubectl ` 命令的概述。
30
25
26
+ 本页列举了常用的 “kubectl” 命令和标志
31
27
32
28
<!-- body -->
33
29
34
30
<!--
35
- # kubectl - Cheat Sheet
31
+ ## Kubectl autocomplete
36
32
37
- ## Kubectl Autocomplete
33
+ ### BASH
38
34
-->
39
- # kubectl - 备忘单
40
35
41
36
## Kubectl 自动补全
42
37
@@ -67,12 +62,12 @@ complete -F __start_kubectl k
67
62
<!--
68
63
```bash
69
64
source <(kubectl completion zsh) # setup autocomplete in zsh into the current shell
70
- echo "if [ $commands[kubectl] ]; then source <(kubectl completion zsh); fi " >> ~/.zshrc # add autocomplete permanently to your zsh shell
65
+ echo "[[ $commands[kubectl] ]] && source <(kubectl completion zsh)" >> ~/.zshrc # add autocomplete permanently to your zsh shell
71
66
```
72
67
-->
73
68
``` bash
74
69
source <( kubectl completion zsh) # 在 zsh 中设置当前 shell 的自动补全
75
- echo " if [ $commands [kubectl] ]; then source <(kubectl completion zsh); fi " >> ~ /.zshrc # 在您的 zsh shell 中永久的添加自动补全
70
+ echo " [[ $commands [kubectl] ]] && source <(kubectl completion zsh)" >> ~ /.zshrc # 在您的 zsh shell 中永久的添加自动补全
76
71
```
77
72
78
73
<!--
@@ -84,12 +79,12 @@ detailed config file information.
84
79
-->
85
80
## Kubectl 上下文和配置
86
81
87
- 设置 ` kubectl ` 与哪个 Kubernetes 集群进行通信并修改配置信息。查看
88
- [ 使用 kubeconfig 跨集群授权访问] ( /zh/docs/tasks/access-application-cluster/configure-access-multiple-clusters/ )
82
+ 设置 ` kubectl ` 与哪个 Kubernetes 集群进行通信并修改配置信息。
83
+ 查看 [ 使用 kubeconfig 跨集群授权访问] ( /zh/docs/tasks/access-application-cluster/configure-access-multiple-clusters/ )
89
84
文档获取配置文件详细信息。
90
85
91
86
<!--
92
- ``bash
87
+ ``` bash
93
88
kubectl config view # Show Merged kubeconfig settings.
94
89
95
90
# use multiple kubeconfig files at the same time and view merged config
@@ -103,10 +98,10 @@ kubectl config view -o jsonpath='{.users[?(@.name == "e2e")].user.password}'
103
98
kubectl config view -o jsonpath='{.users[].name}' # display the first user
104
99
kubectl config view -o jsonpath='{.users[*].name}' # get a list of users
105
100
kubectl config get-contexts # display list of contexts
106
- kubectl config current-context # display the current-context
101
+ kubectl config current-context # display the current-context
107
102
kubectl config use-context my-cluster-name # set the default context to my-cluster-name
108
103
109
- # add a new cluster to your kubeconf that supports basic auth
104
+ # add a new user to your kubeconf that supports basic auth
110
105
kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword
111
106
112
107
# permanently save the namespace for all subsequent kubectl commands in that context.
@@ -134,7 +129,7 @@ kubectl config get-contexts # 显示上下文列表
134
129
kubectl config current-context # 展示当前所处的上下文
135
130
kubectl config use-context my-cluster-name # 设置默认的上下文为 my-cluster-name
136
131
137
- # 添加新的集群配置到 kubeconf 中,使用 basic auth 进行身份认证
132
+ # 添加新的用户配置到 kubeconf 中,使用 basic auth 进行身份认证
138
133
kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword
139
134
140
135
# 在指定上下文中持久性地保存名字空间,供所有后续 kubectl 命令使用
@@ -148,12 +143,12 @@ kubectl config unset users.foo # 删除用户 foo
148
143
```
149
144
150
145
<!--
151
- ## Apply
146
+ ## Kubectl apply
152
147
`apply` manages applications through files defining Kubernetes resources. It creates and updates resources in a cluster through running `kubectl apply`. This is the recommended way of managing Kubernetes applications on production. See [Kubectl Book](https://kubectl.docs.kubernetes.io).
153
148
-->
154
- ## Apply
155
- ` apply ` 通过定义 Kubernetes 资源的文件来管理应用。它通过运行
156
- ` kubectl apply ` 在集群中创建和更新资源。
149
+ ## Kubectl apply
150
+ ` apply ` 通过定义 Kubernetes 资源的文件来管理应用。
151
+ 它通过运行 ` kubectl apply ` 在集群中创建和更新资源。
157
152
这是在生产中管理 Kubernetes 应用的推荐方法。
158
153
参见 [ Kubectl 文档] ( https://kubectl.docs.kubernetes.io ) 。
159
154
@@ -170,12 +165,19 @@ Kubernetes 配置可以用 YAML 或 JSON 定义。可以使用的文件扩展名
170
165
171
166
<!--
172
167
```bash
173
- kubectl apply -f ./my-manifest.yaml # create resource(s)
174
- kubectl apply -f ./my1.yaml -f ./my2.yaml # create from multiple files
175
- kubectl apply -f ./dir # create resource(s) in all manifest files in dir
176
- kubectl apply -f https://git.io/vPieo # create resource(s) from url
177
- kubectl create deployment nginx --image=nginx # start a single instance of nginx
178
- kubectl explain pods # get the documentation for pod and svc manifests
168
+ kubectl apply -f ./my-manifest.yaml # create resource(s)
169
+ kubectl apply -f ./my1.yaml -f ./my2.yaml # create from multiple files
170
+ kubectl apply -f ./dir # create resource(s) in all manifest files in dir
171
+ kubectl apply -f https://git.io/vPieo # create resource(s) from url
172
+ kubectl create deployment nginx --image=nginx # start a single instance of nginx
173
+
174
+ # create a Job which prints "Hello World"
175
+ kubectl create job hello --image=busybox -- echo "Hello World"
176
+
177
+ # create a CronJob that prints "Hello World" every minute
178
+ kubectl create cronjob hello --image=busybox --schedule="*/1 * * * *" -- echo "Hello World"
179
+
180
+ kubectl explain pods # get the documentation for pod manifests
179
181
180
182
# Create multiple YAML objects from stdin
181
183
cat <<EOF | kubectl apply -f -
@@ -223,7 +225,14 @@ kubectl apply -f ./my1.yaml -f ./my2.yaml # 使用多个文件创建
223
225
kubectl apply -f ./dir # 基于目录下的所有清单文件创建资源
224
226
kubectl apply -f https://git.io/vPieo # 从 URL 中创建资源
225
227
kubectl create deployment nginx --image=nginx # 启动单实例 nginx
226
- kubectl explain pods,svc # 获取 pod 清单的文档说明
228
+
229
+ # 创建一个打印 “Hello World” 的 Job
230
+ kubectl create job hello --image=busybox -- echo " Hello World"
231
+
232
+ # 创建一个打印 “Hello World” 间隔1分钟的 CronJob
233
+ kubectl create cronjob hello --image=busybox --schedule=" */1 * * * *" -- echo " Hello World"
234
+
235
+ kubectl explain pods # 获取 pod 清单的文档说明
227
236
228
237
# 从标准输入创建多个 YAML 对象
229
238
cat << EOF | kubectl apply -f -
266
275
```
267
276
268
277
<!--
269
- ## Viewing, Finding Resources
278
+ ## Viewing, finding resources
270
279
-->
271
280
## 查看和查找资源
272
281
275
284
# Get commands with basic output
276
285
kubectl get services # List all services in the namespace
277
286
kubectl get pods --all-namespaces # List all pods in all namespaces
278
- kubectl get pods -o wide # List all pods in the namespace, with more details
287
+ kubectl get pods -o wide # List all pods in the current namespace, with more details
279
288
kubectl get deployment my-dep # List a particular deployment
280
289
kubectl get pods # List all pods in the namespace
281
290
kubectl get pod my-pod -o yaml # Get a pod's YAML
@@ -297,6 +306,10 @@ kubectl get pv --sort-by=.spec.capacity.storage
297
306
kubectl get pods --selector=app=cassandra -o \
298
307
jsonpath='{.items[*].metadata.labels.version}'
299
308
309
+ # Retrieve the value of a key with dots, e.g. 'ca.crt'
310
+ kubectl get configmap myconfig \
311
+ -o jsonpath='{.data.ca\.crt}'
312
+
300
313
# Get all worker nodes (use a selector to exclude results that have a label
301
314
# named 'node-role.kubernetes.io/master')
302
315
kubectl get node --selector='!node-role.kubernetes.io/master'
@@ -331,6 +344,14 @@ kubectl get events --sort-by=.metadata.creationTimestamp
331
344
332
345
# Compares the current state of the cluster against the state that the cluster would be in if the manifest was applied.
333
346
kubectl diff -f ./my-manifest.yaml
347
+
348
+ # Produce a period-delimited tree of all keys returned for nodes
349
+ # Helpful when locating a key within a complex nested JSON structure
350
+ kubectl get nodes -o json | jq -c 'path(..)|[.[]|tostring]|join(".")'
351
+
352
+ # Produce a period-delimited tree of all keys returned for pods, etc
353
+ kubectl get pods -o json | jq -c 'path(..)|[.[]|tostring]|join(".")'
354
+
334
355
```
335
356
-->
336
357
``` bash
@@ -359,6 +380,10 @@ kubectl get pv --sort-by=.spec.capacity.storage
359
380
kubectl get pods --selector=app=cassandra -o \
360
381
jsonpath=' {.items[*].metadata.labels.version}'
361
382
383
+ # 检索带有 “.” 键值,例: 'ca.crt'
384
+ kubectl get configmap myconfig \
385
+ -o jsonpath=' {.data.ca\.crt}'
386
+
362
387
# 获取所有工作节点(使用选择器以排除标签名称为 'node-role.kubernetes.io/master' 的结果)
363
388
kubectl get node --selector=' !node-role.kubernetes.io/master'
364
389
@@ -392,10 +417,18 @@ kubectl get events --sort-by=.metadata.creationTimestamp
392
417
393
418
# 比较当前的集群状态和假定某清单被应用之后的集群状态
394
419
kubectl diff -f ./my-manifest.yaml
420
+
421
+ # 生成一个句点分隔的树,其中包含为节点返回的所有键
422
+ # 在复杂的嵌套JSON结构中定位键时非常有用
423
+ kubectl get nodes -o json | jq -c ' path(..)|[.[]|tostring]|join(".")'
424
+
425
+ # 生成一个句点分隔的树,其中包含为pod等返回的所有键
426
+ kubectl get pods -o json | jq -c ' path(..)|[.[]|tostring]|join(".")'
427
+
395
428
```
396
429
397
430
<!--
398
- ## Updating Resources
431
+ ## Updating resources
399
432
-->
400
433
## 更新资源
401
434
@@ -448,7 +481,7 @@ kubectl annotate pods my-pod icon-url=http://goo.gl/XXBTWq # 添加注解
448
481
kubectl autoscale deployment foo --min=2 --max=10 # 对 "foo" Deployment 自动伸缩容
449
482
```
450
483
451
- <!-- ## Patching Resources -->
484
+ <!-- ## Patching resources -->
452
485
## 部分更新资源
453
486
454
487
<!--
@@ -487,9 +520,9 @@ kubectl patch sa default --type='json' -p='[{"op": "add", "path": "/secrets/1",
487
520
```
488
521
489
522
<!--
490
- ## Editing Resources
523
+ ## Editing resources
491
524
492
- The edit any API resource in an editor.
525
+ Edit any API resource in your preferred editor.
493
526
-->
494
527
## 编辑资源
495
528
@@ -507,7 +540,7 @@ KUBE_EDITOR="nano" kubectl edit svc/docker-registry # 使用其他编辑器
507
540
```
508
541
509
542
<!--
510
- ## Scaling Resources
543
+ ## Scaling resources
511
544
-->
512
545
## 对资源进行伸缩
513
546
@@ -526,7 +559,7 @@ kubectl scale --replicas=5 rc/foo rc/bar rc/baz # 伸缩多个
526
559
```
527
560
528
561
<!--
529
- ## Deleting Resources
562
+ ## Deleting resources
530
563
-->
531
564
## 删除资源
532
565
@@ -535,7 +568,7 @@ kubectl delete -f ./pod.json # Dele
535
568
kubectl delete pod,service baz foo # Delete pods and services with same names "baz" and "foo"
536
569
kubectl delete pods,services -l name=myLabel # Delete pods and services with label name=myLabel
537
570
kubectl delete pods,services -l name=myLabel # Delete pods and services with label name=myLabel
538
- kubectl -n my-ns delete po ,svc --all # Delete all pods and services in namespace my-ns,
571
+ kubectl -n my-ns delete pod ,svc --all # Delete all pods and services in namespace my-ns,
539
572
# Delete all pods matching the awk pattern1 or pattern2
540
573
kubectl get pods -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{print $1}' | xargs kubectl delete -n mynamespace pod
541
574
```
@@ -544,8 +577,7 @@ kubectl get pods -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{pr
544
577
kubectl delete -f ./pod.json # 删除在 pod.json 中指定的类型和名称的 Pod
545
578
kubectl delete pod,service baz foo # 删除名称为 "baz" 和 "foo" 的 Pod 和服务
546
579
kubectl delete pods,services -l name=myLabel # 删除包含 name=myLabel 标签的 pods 和服务
547
- kubectl delete pods,services -l name=myLabel --include-uninitialized # 删除包含 label name=myLabel 标签的 Pods 和服务
548
- kubectl -n my-ns delete po,svc --all # 删除在 my-ns 名字空间中全部的 Pods 和服务
580
+ kubectl -n my-ns delete pod,svc --all # 删除在 my-ns 名字空间中全部的 Pods 和服务
549
581
# 删除所有与 pattern1 或 pattern2 awk 模式匹配的 Pods
550
582
kubectl get pods -n mynamespace --no-headers=true | awk ' /pattern1|pattern2/{print $1}' | xargs kubectl delete -n mynamespace pod
551
583
```
@@ -674,14 +706,14 @@ kubectl api-resources --api-group=extensions # "extensions" API 组中的所有
674
706
<!--
675
707
### Formatting output
676
708
677
- To output details to your terminal window in a specific format, you can add either the `-o` or `--output` flags to a supported `kubectl` command.
709
+ To output details to your terminal window in a specific format, add the `-o` ( or `--output`) flag to a supported `kubectl` command.
678
710
-->
679
711
### 格式化输出
680
712
681
- 要以特定格式将详细信息输出到终端窗口,可以将 ` -o ` 或 ` --output ` 参数添加到支持的 ` kubectl ` 命令 。
713
+ 要以特定格式将详细信息输出到终端窗口,将 ` -o ` (或者 ` --output ` ) 参数添加到支持的 ` kubectl ` 命令中 。
682
714
683
715
<!-- O
684
- utput format | Description
716
+ Output format | Description
685
717
--------------| -----------
686
718
`-o=custom-columns=<spec>` | Print a table using a comma separated list of custom columns
687
719
`-o=custom-columns-file=<filename>` | Print a table using the custom columns template in the `<filename>` file
@@ -773,12 +805,16 @@ Verbosity | Description
773
805
## {{% heading "whatsnext" %}}
774
806
775
807
<!--
776
- * Learn more about [Overview of kubectl](/docs/reference/kubectl/overview/).
808
+
809
+ * Read the [kubectl overview](/docs/reference/kubectl/overview/) and learn about [JsonPath](/docs/reference/kubectl/jsonpath).
810
+
777
811
* See [kubectl](/docs/reference/kubectl/kubectl/) options.
778
- * Also [kubectl Usage Conventions](/docs/reference/kubectl/conventions/) to understand how to use it in reusable scripts.
812
+
813
+ * Also read [kubectl Usage Conventions](/docs/reference/kubectl/conventions/) to understand how to use kubectl in reusable scripts.
814
+
779
815
* See more community [kubectl cheatsheets](https://github.com/dennyzhang/cheatsheet-kubernetes-A4).
780
816
-->
781
- * 进一步了解 [ kubectl 概述] ( /zh/docs/reference/kubectl/overview/ ) 。
782
- * 参阅 [ kubectl] ( /zh/docs/reference/kubectl/kubectl/ ) 选项.
817
+ * 参阅 [ kubectl 概述] ( /zh/docs/reference/kubectl/overview/ ) ,进一步了解 [ JsonPath ] ( /zh/docs/reference/kubectl/jsonpath ) 。
818
+ * 参阅 [ kubectl] ( /zh/docs/reference/kubectl/kubectl/ ) 选项。
783
819
* 参阅 [ kubectl 使用约定] ( /zh/docs/reference/kubectl/conventions/ ) 来理解如何在可复用的脚本中使用它。
784
820
* 查看社区中其他的 [ kubectl 备忘单] ( https://github.com/dennyzhang/cheatsheet-kubernetes-A4 ) 。
0 commit comments