Skip to content

Commit ec4d60a

Browse files
authored
[zh] Sync change from English (Issues 25247) (#25322)
* sync kubectl/jsonpath.md for [#25247] * sync kubectl/jsonpath.md for [#25247] * sync kubectl/cheatsheet.md * update * update * update ... * update ... * update ...
1 parent 4f9b919 commit ec4d60a

File tree

2 files changed

+123
-51
lines changed

2 files changed

+123
-51
lines changed

content/zh/docs/reference/kubectl/cheatsheet.md

Lines changed: 82 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,18 @@ card:
2020
<!-- overview -->
2121

2222
<!--
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.
2624
-->
27-
另见: [Kubectl 概述](/zh/docs/reference/kubectl/overview/)[JsonPath 指南](/zh/docs/reference/kubectl/jsonpath)
28-
29-
本页面是 `kubectl` 命令的概述。
3025

26+
本页列举了常用的 “kubectl” 命令和标志
3127

3228
<!-- body -->
3329

3430
<!--
35-
# kubectl - Cheat Sheet
31+
## Kubectl autocomplete
3632
37-
## Kubectl Autocomplete
33+
### BASH
3834
-->
39-
# kubectl - 备忘单
4035

4136
## Kubectl 自动补全
4237

@@ -67,12 +62,12 @@ complete -F __start_kubectl k
6762
<!--
6863
```bash
6964
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
7166
```
7267
-->
7368
```bash
7469
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 中永久的添加自动补全
7671
```
7772

7873
<!--
@@ -84,12 +79,12 @@ detailed config file information.
8479
-->
8580
## Kubectl 上下文和配置
8681

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/)
8984
文档获取配置文件详细信息。
9085

9186
<!--
92-
``bash
87+
```bash
9388
kubectl config view # Show Merged kubeconfig settings.
9489
9590
# 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}'
10398
kubectl config view -o jsonpath='{.users[].name}' # display the first user
10499
kubectl config view -o jsonpath='{.users[*].name}' # get a list of users
105100
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
107102
kubectl config use-context my-cluster-name # set the default context to my-cluster-name
108103
109-
# add a new cluster to your kubeconf that supports basic auth
104+
# add a new user to your kubeconf that supports basic auth
110105
kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword
111106
112107
# permanently save the namespace for all subsequent kubectl commands in that context.
@@ -134,7 +129,7 @@ kubectl config get-contexts # 显示上下文列表
134129
kubectl config current-context # 展示当前所处的上下文
135130
kubectl config use-context my-cluster-name # 设置默认的上下文为 my-cluster-name
136131

137-
# 添加新的集群配置到 kubeconf 中,使用 basic auth 进行身份认证
132+
# 添加新的用户配置到 kubeconf 中,使用 basic auth 进行身份认证
138133
kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword
139134

140135
# 在指定上下文中持久性地保存名字空间,供所有后续 kubectl 命令使用
@@ -148,12 +143,12 @@ kubectl config unset users.foo # 删除用户 foo
148143
```
149144

150145
<!--
151-
## Apply
146+
## Kubectl apply
152147
`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).
153148
-->
154-
## Apply
155-
`apply` 通过定义 Kubernetes 资源的文件来管理应用。它通过运行
156-
`kubectl apply` 在集群中创建和更新资源。
149+
## Kubectl apply
150+
`apply` 通过定义 Kubernetes 资源的文件来管理应用。
151+
它通过运行 `kubectl apply` 在集群中创建和更新资源。
157152
这是在生产中管理 Kubernetes 应用的推荐方法。
158153
参见 [Kubectl 文档](https://kubectl.docs.kubernetes.io)
159154

@@ -170,12 +165,19 @@ Kubernetes 配置可以用 YAML 或 JSON 定义。可以使用的文件扩展名
170165

171166
<!--
172167
```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
179181
180182
# Create multiple YAML objects from stdin
181183
cat <<EOF | kubectl apply -f -
@@ -223,7 +225,14 @@ kubectl apply -f ./my1.yaml -f ./my2.yaml # 使用多个文件创建
223225
kubectl apply -f ./dir # 基于目录下的所有清单文件创建资源
224226
kubectl apply -f https://git.io/vPieo # 从 URL 中创建资源
225227
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 清单的文档说明
227236

228237
# 从标准输入创建多个 YAML 对象
229238
cat <<EOF | kubectl apply -f -
@@ -266,7 +275,7 @@ EOF
266275
```
267276

268277
<!--
269-
## Viewing, Finding Resources
278+
## Viewing, finding resources
270279
-->
271280
## 查看和查找资源
272281

@@ -275,7 +284,7 @@ EOF
275284
# Get commands with basic output
276285
kubectl get services # List all services in the namespace
277286
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
279288
kubectl get deployment my-dep # List a particular deployment
280289
kubectl get pods # List all pods in the namespace
281290
kubectl get pod my-pod -o yaml # Get a pod's YAML
@@ -297,6 +306,10 @@ kubectl get pv --sort-by=.spec.capacity.storage
297306
kubectl get pods --selector=app=cassandra -o \
298307
jsonpath='{.items[*].metadata.labels.version}'
299308
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+
300313
# Get all worker nodes (use a selector to exclude results that have a label
301314
# named 'node-role.kubernetes.io/master')
302315
kubectl get node --selector='!node-role.kubernetes.io/master'
@@ -331,6 +344,14 @@ kubectl get events --sort-by=.metadata.creationTimestamp
331344
332345
# Compares the current state of the cluster against the state that the cluster would be in if the manifest was applied.
333346
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+
334355
```
335356
-->
336357
```bash
@@ -359,6 +380,10 @@ kubectl get pv --sort-by=.spec.capacity.storage
359380
kubectl get pods --selector=app=cassandra -o \
360381
jsonpath='{.items[*].metadata.labels.version}'
361382

383+
# 检索带有 “.” 键值,例: 'ca.crt'
384+
kubectl get configmap myconfig \
385+
-o jsonpath='{.data.ca\.crt}'
386+
362387
# 获取所有工作节点(使用选择器以排除标签名称为 'node-role.kubernetes.io/master' 的结果)
363388
kubectl get node --selector='!node-role.kubernetes.io/master'
364389

@@ -392,10 +417,18 @@ kubectl get events --sort-by=.metadata.creationTimestamp
392417

393418
# 比较当前的集群状态和假定某清单被应用之后的集群状态
394419
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+
395428
```
396429

397430
<!--
398-
## Updating Resources
431+
## Updating resources
399432
-->
400433
## 更新资源
401434

@@ -448,7 +481,7 @@ kubectl annotate pods my-pod icon-url=http://goo.gl/XXBTWq # 添加注解
448481
kubectl autoscale deployment foo --min=2 --max=10 # 对 "foo" Deployment 自动伸缩容
449482
```
450483

451-
<!-- ## Patching Resources -->
484+
<!-- ## Patching resources -->
452485
## 部分更新资源
453486

454487
<!--
@@ -487,9 +520,9 @@ kubectl patch sa default --type='json' -p='[{"op": "add", "path": "/secrets/1",
487520
```
488521

489522
<!--
490-
## Editing Resources
523+
## Editing resources
491524
492-
The edit any API resource in an editor.
525+
Edit any API resource in your preferred editor.
493526
-->
494527
## 编辑资源
495528

@@ -507,7 +540,7 @@ KUBE_EDITOR="nano" kubectl edit svc/docker-registry # 使用其他编辑器
507540
```
508541

509542
<!--
510-
## Scaling Resources
543+
## Scaling resources
511544
-->
512545
## 对资源进行伸缩
513546

@@ -526,7 +559,7 @@ kubectl scale --replicas=5 rc/foo rc/bar rc/baz # 伸缩多个
526559
```
527560

528561
<!--
529-
## Deleting Resources
562+
## Deleting resources
530563
-->
531564
## 删除资源
532565

@@ -535,7 +568,7 @@ kubectl delete -f ./pod.json # Dele
535568
kubectl delete pod,service baz foo # Delete pods and services with same names "baz" and "foo"
536569
kubectl delete pods,services -l name=myLabel # Delete pods and services with label name=myLabel
537570
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,
539572
# Delete all pods matching the awk pattern1 or pattern2
540573
kubectl get pods -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{print $1}' | xargs kubectl delete -n mynamespace pod
541574
```
@@ -544,8 +577,7 @@ kubectl get pods -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{pr
544577
kubectl delete -f ./pod.json # 删除在 pod.json 中指定的类型和名称的 Pod
545578
kubectl delete pod,service baz foo # 删除名称为 "baz" 和 "foo" 的 Pod 和服务
546579
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 和服务
549581
# 删除所有与 pattern1 或 pattern2 awk 模式匹配的 Pods
550582
kubectl get pods -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{print $1}' | xargs kubectl delete -n mynamespace pod
551583
```
@@ -674,14 +706,14 @@ kubectl api-resources --api-group=extensions # "extensions" API 组中的所有
674706
<!--
675707
### Formatting output
676708
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.
678710
-->
679711
### 格式化输出
680712

681-
要以特定格式将详细信息输出到终端窗口,可以将 `-o``--output` 参数添加到支持的 `kubectl` 命令
713+
要以特定格式将详细信息输出到终端窗口, `-o`(或者 `--output`参数添加到支持的 `kubectl` 命令中
682714

683715
<!--O
684-
utput format | Description
716+
Output format | Description
685717
--------------| -----------
686718
`-o=custom-columns=<spec>` | Print a table using a comma separated list of custom columns
687719
`-o=custom-columns-file=<filename>` | Print a table using the custom columns template in the `<filename>` file
@@ -773,12 +805,16 @@ Verbosity | Description
773805
## {{% heading "whatsnext" %}}
774806

775807
<!--
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+
777811
* 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+
779815
* See more community [kubectl cheatsheets](https://github.com/dennyzhang/cheatsheet-kubernetes-A4).
780816
-->
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/) 选项
783819
* 参阅 [kubectl 使用约定](/zh/docs/reference/kubectl/conventions/)来理解如何在可复用的脚本中使用它。
784820
* 查看社区中其他的 [kubectl 备忘单](https://github.com/dennyzhang/cheatsheet-kubernetes-A4)

0 commit comments

Comments
 (0)