From ec4d60aa8056ffb8ff12ed4d4bb1cb1dd5677ef0 Mon Sep 17 00:00:00 2001 From: Xu Yuandong <786018072@qq.com> Date: Tue, 1 Dec 2020 17:10:50 +0800 Subject: [PATCH] [zh] Sync change from English (Issues 25247) (#25322) * sync kubectl/jsonpath.md for [kubernetes/website#25247] * sync kubectl/jsonpath.md for [kubernetes/website#25247] * sync kubectl/cheatsheet.md * update * update * update ... * update ... * update ... --- .../zh/docs/reference/kubectl/cheatsheet.md | 128 +++++++++++------- content/zh/docs/reference/kubectl/jsonpath.md | 46 ++++++- 2 files changed, 123 insertions(+), 51 deletions(-) diff --git a/content/zh/docs/reference/kubectl/cheatsheet.md b/content/zh/docs/reference/kubectl/cheatsheet.md index 1c351a7eab..013af57460 100644 --- a/content/zh/docs/reference/kubectl/cheatsheet.md +++ b/content/zh/docs/reference/kubectl/cheatsheet.md @@ -20,23 +20,18 @@ card: -另见: [Kubectl 概述](/zh/docs/reference/kubectl/overview/) 和 [JsonPath 指南](/zh/docs/reference/kubectl/jsonpath)。 - -本页面是 `kubectl` 命令的概述。 +本页列举了常用的 “kubectl” 命令和标志 -# kubectl - 备忘单 ## Kubectl 自动补全 @@ -67,12 +62,12 @@ complete -F __start_kubectl k ```bash source <(kubectl completion zsh) # 在 zsh 中设置当前 shell 的自动补全 -echo "if [ $commands[kubectl] ]; then source <(kubectl completion zsh); fi" >> ~/.zshrc # 在您的 zsh shell 中永久的添加自动补全 +echo "[[ $commands[kubectl] ]] && source <(kubectl completion zsh)" >> ~/.zshrc # 在您的 zsh shell 中永久的添加自动补全 ``` ## Kubectl 上下文和配置 -设置 `kubectl` 与哪个 Kubernetes 集群进行通信并修改配置信息。查看 -[使用 kubeconfig 跨集群授权访问](/zh/docs/tasks/access-application-cluster/configure-access-multiple-clusters/) +设置 `kubectl` 与哪个 Kubernetes 集群进行通信并修改配置信息。 +查看[使用 kubeconfig 跨集群授权访问](/zh/docs/tasks/access-application-cluster/configure-access-multiple-clusters/) 文档获取配置文件详细信息。 -## Apply -`apply` 通过定义 Kubernetes 资源的文件来管理应用。它通过运行 -`kubectl apply` 在集群中创建和更新资源。 +## Kubectl apply +`apply` 通过定义 Kubernetes 资源的文件来管理应用。 +它通过运行 `kubectl apply` 在集群中创建和更新资源。 这是在生产中管理 Kubernetes 应用的推荐方法。 参见 [Kubectl 文档](https://kubectl.docs.kubernetes.io)。 @@ -170,12 +165,19 @@ Kubernetes 配置可以用 YAML 或 JSON 定义。可以使用的文件扩展名 ## 查看和查找资源 @@ -275,7 +284,7 @@ EOF # Get commands with basic output kubectl get services # List all services in the namespace kubectl get pods --all-namespaces # List all pods in all namespaces -kubectl get pods -o wide # List all pods in the namespace, with more details +kubectl get pods -o wide # List all pods in the current namespace, with more details kubectl get deployment my-dep # List a particular deployment kubectl get pods # List all pods in the namespace kubectl get pod my-pod -o yaml # Get a pod's YAML @@ -297,6 +306,10 @@ kubectl get pv --sort-by=.spec.capacity.storage kubectl get pods --selector=app=cassandra -o \ jsonpath='{.items[*].metadata.labels.version}' +# Retrieve the value of a key with dots, e.g. 'ca.crt' +kubectl get configmap myconfig \ + -o jsonpath='{.data.ca\.crt}' + # Get all worker nodes (use a selector to exclude results that have a label # named 'node-role.kubernetes.io/master') kubectl get node --selector='!node-role.kubernetes.io/master' @@ -331,6 +344,14 @@ kubectl get events --sort-by=.metadata.creationTimestamp # Compares the current state of the cluster against the state that the cluster would be in if the manifest was applied. kubectl diff -f ./my-manifest.yaml + +# Produce a period-delimited tree of all keys returned for nodes +# Helpful when locating a key within a complex nested JSON structure +kubectl get nodes -o json | jq -c 'path(..)|[.[]|tostring]|join(".")' + +# Produce a period-delimited tree of all keys returned for pods, etc +kubectl get pods -o json | jq -c 'path(..)|[.[]|tostring]|join(".")' + ``` --> ```bash @@ -359,6 +380,10 @@ kubectl get pv --sort-by=.spec.capacity.storage kubectl get pods --selector=app=cassandra -o \ jsonpath='{.items[*].metadata.labels.version}' +# 检索带有 “.” 键值,例: 'ca.crt' +kubectl get configmap myconfig \ + -o jsonpath='{.data.ca\.crt}' + # 获取所有工作节点(使用选择器以排除标签名称为 'node-role.kubernetes.io/master' 的结果) kubectl get node --selector='!node-role.kubernetes.io/master' @@ -392,10 +417,18 @@ kubectl get events --sort-by=.metadata.creationTimestamp # 比较当前的集群状态和假定某清单被应用之后的集群状态 kubectl diff -f ./my-manifest.yaml + +# 生成一个句点分隔的树,其中包含为节点返回的所有键 +# 在复杂的嵌套JSON结构中定位键时非常有用 +kubectl get nodes -o json | jq -c 'path(..)|[.[]|tostring]|join(".")' + +# 生成一个句点分隔的树,其中包含为pod等返回的所有键 +kubectl get pods -o json | jq -c 'path(..)|[.[]|tostring]|join(".")' + ``` ## 更新资源 @@ -448,7 +481,7 @@ kubectl annotate pods my-pod icon-url=http://goo.gl/XXBTWq # 添加注解 kubectl autoscale deployment foo --min=2 --max=10 # 对 "foo" Deployment 自动伸缩容 ``` - + ## 部分更新资源 ## 编辑资源 @@ -507,7 +540,7 @@ KUBE_EDITOR="nano" kubectl edit svc/docker-registry # 使用其他编辑器 ``` ## 对资源进行伸缩 @@ -526,7 +559,7 @@ kubectl scale --replicas=5 rc/foo rc/bar rc/baz # 伸缩多个 ``` ## 删除资源 @@ -535,7 +568,7 @@ kubectl delete -f ./pod.json # Dele kubectl delete pod,service baz foo # Delete pods and services with same names "baz" and "foo" kubectl delete pods,services -l name=myLabel # Delete pods and services with label name=myLabel kubectl delete pods,services -l name=myLabel # Delete pods and services with label name=myLabel -kubectl -n my-ns delete po,svc --all # Delete all pods and services in namespace my-ns, +kubectl -n my-ns delete pod,svc --all # Delete all pods and services in namespace my-ns, # Delete all pods matching the awk pattern1 or pattern2 kubectl get pods -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{print $1}' | xargs kubectl delete -n mynamespace pod ``` @@ -544,8 +577,7 @@ kubectl get pods -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{pr kubectl delete -f ./pod.json # 删除在 pod.json 中指定的类型和名称的 Pod kubectl delete pod,service baz foo # 删除名称为 "baz" 和 "foo" 的 Pod 和服务 kubectl delete pods,services -l name=myLabel # 删除包含 name=myLabel 标签的 pods 和服务 -kubectl delete pods,services -l name=myLabel --include-uninitialized # 删除包含 label name=myLabel 标签的 Pods 和服务 -kubectl -n my-ns delete po,svc --all # 删除在 my-ns 名字空间中全部的 Pods 和服务 +kubectl -n my-ns delete pod,svc --all # 删除在 my-ns 名字空间中全部的 Pods 和服务 # 删除所有与 pattern1 或 pattern2 awk 模式匹配的 Pods kubectl get pods -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{print $1}' | xargs kubectl delete -n mynamespace pod ``` @@ -674,14 +706,14 @@ kubectl api-resources --api-group=extensions # "extensions" API 组中的所有 ### 格式化输出 -要以特定格式将详细信息输出到终端窗口,可以将 `-o` 或 `--output` 参数添加到支持的 `kubectl` 命令。 +要以特定格式将详细信息输出到终端窗口,将 `-o`(或者 `--output`)参数添加到支持的 `kubectl` 命令中。 -* 进一步了解 [kubectl 概述](/zh/docs/reference/kubectl/overview/)。 -* 参阅 [kubectl](/zh/docs/reference/kubectl/kubectl/) 选项. +* 参阅 [kubectl 概述](/zh/docs/reference/kubectl/overview/),进一步了解[JsonPath](/zh/docs/reference/kubectl/jsonpath)。 +* 参阅 [kubectl](/zh/docs/reference/kubectl/kubectl/) 选项。 * 参阅 [kubectl 使用约定](/zh/docs/reference/kubectl/conventions/)来理解如何在可复用的脚本中使用它。 * 查看社区中其他的 [kubectl 备忘单](https://github.com/dennyzhang/cheatsheet-kubernetes-A4)。 diff --git a/content/zh/docs/reference/kubectl/jsonpath.md b/content/zh/docs/reference/kubectl/jsonpath.md index 900251c09d..c1302ec3d6 100644 --- a/content/zh/docs/reference/kubectl/jsonpath.md +++ b/content/zh/docs/reference/kubectl/jsonpath.md @@ -34,7 +34,7 @@ JSONPath 模板由 {} 包起来的 JSONPath 表达式组成。Kubectl 使用 JSO --> 1. 使用双引号将 JSONPath 表达式内的文本引起来。 2. 使用 `range`,`end` 运算符来迭代列表。 -3. 使用负片索引后退列表。负索引不会"环绕"列表,并且只要 `-index + listLength> = 0` 就有效。 +3. 使用负片索引后退列表。负索引不会“环绕”列表,并且只要 `-index + listLength> = 0` 就有效。 {{< note >}} -在 Windows 上,您必须 _double_ 引用任何包含空格的 JSONPath 模板(不是上面 bash 所示的单引号)。反过来,这意味着您必须在模板中的所有文字周围使用单引号或转义的双引号。例如: +{{< note >}} +在 Windows 上,您必须用双引号把任何包含空格的 JSONPath 模板(不是上面 bash 所示的单引号)。 +反过来,这意味着您必须在模板中的所有文字周围使用单引号或转义的双引号。 +例如: ```cmd C:\> kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{'\t'}{.status.startTime}{'\n'}{end}" C:\> kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{\"\t\"}{.status.startTime}{\"\n\"}{end}" ``` +{{< /note >}} + +{{< note >}} +不支持 JSONPath 正则表达式。如需使用正则表达式进行匹配操作,您可以使用如 `jq` 之类的工具。 + +```shell +# kubectl 的 JSONpath 输出不支持正则表达式 +# 下面的命令不会生效 +kubectl get pods -o jsonpath='{.items[?(@.metadata.name=~/^test$/)].metadata.name}' + +# 下面的命令可以获得所需的结果 +kubectl get pods -o json | jq -r '.items[] | select(.metadata.name | test("test-")).spec.containers[].image' +``` +{{< /note >}}