[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 ...
This commit is contained in:
@@ -20,23 +20,18 @@ card:
|
|||||||
<!-- overview -->
|
<!-- overview -->
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
See also: [Kubectl Overview](/docs/reference/kubectl/overview/) and [JsonPath Guide](/docs/reference/kubectl/jsonpath).
|
This page contains a list of commonly used `kubectl` commands and flags.
|
||||||
|
|
||||||
This page is an overview of the `kubectl` command.
|
|
||||||
-->
|
-->
|
||||||
另见: [Kubectl 概述](/zh/docs/reference/kubectl/overview/) 和 [JsonPath 指南](/zh/docs/reference/kubectl/jsonpath)。
|
|
||||||
|
|
||||||
本页面是 `kubectl` 命令的概述。
|
|
||||||
|
|
||||||
|
本页列举了常用的 “kubectl” 命令和标志
|
||||||
|
|
||||||
<!-- body -->
|
<!-- body -->
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
# kubectl - Cheat Sheet
|
## Kubectl autocomplete
|
||||||
|
|
||||||
## Kubectl Autocomplete
|
### BASH
|
||||||
-->
|
-->
|
||||||
# kubectl - 备忘单
|
|
||||||
|
|
||||||
## Kubectl 自动补全
|
## Kubectl 自动补全
|
||||||
|
|
||||||
@@ -67,12 +62,12 @@ complete -F __start_kubectl k
|
|||||||
<!--
|
<!--
|
||||||
```bash
|
```bash
|
||||||
source <(kubectl completion zsh) # setup autocomplete in zsh into the current shell
|
source <(kubectl completion zsh) # setup autocomplete in zsh into the current shell
|
||||||
echo "if [ $commands[kubectl] ]; then source <(kubectl completion zsh); fi" >> ~/.zshrc # add autocomplete permanently to your zsh shell
|
echo "[[ $commands[kubectl] ]] && source <(kubectl completion zsh)" >> ~/.zshrc # add autocomplete permanently to your zsh shell
|
||||||
```
|
```
|
||||||
-->
|
-->
|
||||||
```bash
|
```bash
|
||||||
source <(kubectl completion zsh) # 在 zsh 中设置当前 shell 的自动补全
|
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 中永久的添加自动补全
|
||||||
```
|
```
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
@@ -84,12 +79,12 @@ detailed config file information.
|
|||||||
-->
|
-->
|
||||||
## Kubectl 上下文和配置
|
## Kubectl 上下文和配置
|
||||||
|
|
||||||
设置 `kubectl` 与哪个 Kubernetes 集群进行通信并修改配置信息。查看
|
设置 `kubectl` 与哪个 Kubernetes 集群进行通信并修改配置信息。
|
||||||
[使用 kubeconfig 跨集群授权访问](/zh/docs/tasks/access-application-cluster/configure-access-multiple-clusters/)
|
查看[使用 kubeconfig 跨集群授权访问](/zh/docs/tasks/access-application-cluster/configure-access-multiple-clusters/)
|
||||||
文档获取配置文件详细信息。
|
文档获取配置文件详细信息。
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
``bash
|
```bash
|
||||||
kubectl config view # Show Merged kubeconfig settings.
|
kubectl config view # Show Merged kubeconfig settings.
|
||||||
|
|
||||||
# use multiple kubeconfig files at the same time and view merged config
|
# 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}'
|
|||||||
kubectl config view -o jsonpath='{.users[].name}' # display the first user
|
kubectl config view -o jsonpath='{.users[].name}' # display the first user
|
||||||
kubectl config view -o jsonpath='{.users[*].name}' # get a list of users
|
kubectl config view -o jsonpath='{.users[*].name}' # get a list of users
|
||||||
kubectl config get-contexts # display list of contexts
|
kubectl config get-contexts # display list of contexts
|
||||||
kubectl config current-context # display the current-context
|
kubectl config current-context # display the current-context
|
||||||
kubectl config use-context my-cluster-name # set the default context to my-cluster-name
|
kubectl config use-context my-cluster-name # set the default context to my-cluster-name
|
||||||
|
|
||||||
# add a new cluster to your kubeconf that supports basic auth
|
# add a new user to your kubeconf that supports basic auth
|
||||||
kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword
|
kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword
|
||||||
|
|
||||||
# permanently save the namespace for all subsequent kubectl commands in that context.
|
# permanently save the namespace for all subsequent kubectl commands in that context.
|
||||||
@@ -134,7 +129,7 @@ kubectl config get-contexts # 显示上下文列表
|
|||||||
kubectl config current-context # 展示当前所处的上下文
|
kubectl config current-context # 展示当前所处的上下文
|
||||||
kubectl config use-context my-cluster-name # 设置默认的上下文为 my-cluster-name
|
kubectl config use-context my-cluster-name # 设置默认的上下文为 my-cluster-name
|
||||||
|
|
||||||
# 添加新的集群配置到 kubeconf 中,使用 basic auth 进行身份认证
|
# 添加新的用户配置到 kubeconf 中,使用 basic auth 进行身份认证
|
||||||
kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword
|
kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword
|
||||||
|
|
||||||
# 在指定上下文中持久性地保存名字空间,供所有后续 kubectl 命令使用
|
# 在指定上下文中持久性地保存名字空间,供所有后续 kubectl 命令使用
|
||||||
@@ -148,12 +143,12 @@ kubectl config unset users.foo # 删除用户 foo
|
|||||||
```
|
```
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
## Apply
|
## Kubectl apply
|
||||||
`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).
|
`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).
|
||||||
-->
|
-->
|
||||||
## Apply
|
## Kubectl apply
|
||||||
`apply` 通过定义 Kubernetes 资源的文件来管理应用。它通过运行
|
`apply` 通过定义 Kubernetes 资源的文件来管理应用。
|
||||||
`kubectl apply` 在集群中创建和更新资源。
|
它通过运行 `kubectl apply` 在集群中创建和更新资源。
|
||||||
这是在生产中管理 Kubernetes 应用的推荐方法。
|
这是在生产中管理 Kubernetes 应用的推荐方法。
|
||||||
参见 [Kubectl 文档](https://kubectl.docs.kubernetes.io)。
|
参见 [Kubectl 文档](https://kubectl.docs.kubernetes.io)。
|
||||||
|
|
||||||
@@ -170,12 +165,19 @@ Kubernetes 配置可以用 YAML 或 JSON 定义。可以使用的文件扩展名
|
|||||||
|
|
||||||
<!--
|
<!--
|
||||||
```bash
|
```bash
|
||||||
kubectl apply -f ./my-manifest.yaml # create resource(s)
|
kubectl apply -f ./my-manifest.yaml # create resource(s)
|
||||||
kubectl apply -f ./my1.yaml -f ./my2.yaml # create from multiple files
|
kubectl apply -f ./my1.yaml -f ./my2.yaml # create from multiple files
|
||||||
kubectl apply -f ./dir # create resource(s) in all manifest files in dir
|
kubectl apply -f ./dir # create resource(s) in all manifest files in dir
|
||||||
kubectl apply -f https://git.io/vPieo # create resource(s) from url
|
kubectl apply -f https://git.io/vPieo # create resource(s) from url
|
||||||
kubectl create deployment nginx --image=nginx # start a single instance of nginx
|
kubectl create deployment nginx --image=nginx # start a single instance of nginx
|
||||||
kubectl explain pods # get the documentation for pod and svc manifests
|
|
||||||
|
# create a Job which prints "Hello World"
|
||||||
|
kubectl create job hello --image=busybox -- echo "Hello World"
|
||||||
|
|
||||||
|
# create a CronJob that prints "Hello World" every minute
|
||||||
|
kubectl create cronjob hello --image=busybox --schedule="*/1 * * * *" -- echo "Hello World"
|
||||||
|
|
||||||
|
kubectl explain pods # get the documentation for pod manifests
|
||||||
|
|
||||||
# Create multiple YAML objects from stdin
|
# Create multiple YAML objects from stdin
|
||||||
cat <<EOF | kubectl apply -f -
|
cat <<EOF | kubectl apply -f -
|
||||||
@@ -223,7 +225,14 @@ kubectl apply -f ./my1.yaml -f ./my2.yaml # 使用多个文件创建
|
|||||||
kubectl apply -f ./dir # 基于目录下的所有清单文件创建资源
|
kubectl apply -f ./dir # 基于目录下的所有清单文件创建资源
|
||||||
kubectl apply -f https://git.io/vPieo # 从 URL 中创建资源
|
kubectl apply -f https://git.io/vPieo # 从 URL 中创建资源
|
||||||
kubectl create deployment nginx --image=nginx # 启动单实例 nginx
|
kubectl create deployment nginx --image=nginx # 启动单实例 nginx
|
||||||
kubectl explain pods,svc # 获取 pod 清单的文档说明
|
|
||||||
|
# 创建一个打印 “Hello World” 的 Job
|
||||||
|
kubectl create job hello --image=busybox -- echo "Hello World"
|
||||||
|
|
||||||
|
# 创建一个打印 “Hello World” 间隔1分钟的 CronJob
|
||||||
|
kubectl create cronjob hello --image=busybox --schedule="*/1 * * * *" -- echo "Hello World"
|
||||||
|
|
||||||
|
kubectl explain pods # 获取 pod 清单的文档说明
|
||||||
|
|
||||||
# 从标准输入创建多个 YAML 对象
|
# 从标准输入创建多个 YAML 对象
|
||||||
cat <<EOF | kubectl apply -f -
|
cat <<EOF | kubectl apply -f -
|
||||||
@@ -266,7 +275,7 @@ EOF
|
|||||||
```
|
```
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
## Viewing, Finding Resources
|
## Viewing, finding resources
|
||||||
-->
|
-->
|
||||||
## 查看和查找资源
|
## 查看和查找资源
|
||||||
|
|
||||||
@@ -275,7 +284,7 @@ EOF
|
|||||||
# Get commands with basic output
|
# Get commands with basic output
|
||||||
kubectl get services # List all services in the namespace
|
kubectl get services # List all services in the namespace
|
||||||
kubectl get pods --all-namespaces # List all pods in all namespaces
|
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 deployment my-dep # List a particular deployment
|
||||||
kubectl get pods # List all pods in the namespace
|
kubectl get pods # List all pods in the namespace
|
||||||
kubectl get pod my-pod -o yaml # Get a pod's YAML
|
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 \
|
kubectl get pods --selector=app=cassandra -o \
|
||||||
jsonpath='{.items[*].metadata.labels.version}'
|
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
|
# Get all worker nodes (use a selector to exclude results that have a label
|
||||||
# named 'node-role.kubernetes.io/master')
|
# named 'node-role.kubernetes.io/master')
|
||||||
kubectl get node --selector='!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.
|
# 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
|
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
|
```bash
|
||||||
@@ -359,6 +380,10 @@ kubectl get pv --sort-by=.spec.capacity.storage
|
|||||||
kubectl get pods --selector=app=cassandra -o \
|
kubectl get pods --selector=app=cassandra -o \
|
||||||
jsonpath='{.items[*].metadata.labels.version}'
|
jsonpath='{.items[*].metadata.labels.version}'
|
||||||
|
|
||||||
|
# 检索带有 “.” 键值,例: 'ca.crt'
|
||||||
|
kubectl get configmap myconfig \
|
||||||
|
-o jsonpath='{.data.ca\.crt}'
|
||||||
|
|
||||||
# 获取所有工作节点(使用选择器以排除标签名称为 'node-role.kubernetes.io/master' 的结果)
|
# 获取所有工作节点(使用选择器以排除标签名称为 'node-role.kubernetes.io/master' 的结果)
|
||||||
kubectl get node --selector='!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
|
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(".")'
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
## Updating Resources
|
## Updating resources
|
||||||
-->
|
-->
|
||||||
## 更新资源
|
## 更新资源
|
||||||
|
|
||||||
@@ -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 自动伸缩容
|
kubectl autoscale deployment foo --min=2 --max=10 # 对 "foo" Deployment 自动伸缩容
|
||||||
```
|
```
|
||||||
|
|
||||||
<!-- ## Patching Resources -->
|
<!-- ## Patching resources -->
|
||||||
## 部分更新资源
|
## 部分更新资源
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
@@ -487,9 +520,9 @@ kubectl patch sa default --type='json' -p='[{"op": "add", "path": "/secrets/1",
|
|||||||
```
|
```
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
## Editing Resources
|
## Editing resources
|
||||||
|
|
||||||
The edit any API resource in an editor.
|
Edit any API resource in your preferred editor.
|
||||||
-->
|
-->
|
||||||
## 编辑资源
|
## 编辑资源
|
||||||
|
|
||||||
@@ -507,7 +540,7 @@ KUBE_EDITOR="nano" kubectl edit svc/docker-registry # 使用其他编辑器
|
|||||||
```
|
```
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
## Scaling Resources
|
## Scaling resources
|
||||||
-->
|
-->
|
||||||
## 对资源进行伸缩
|
## 对资源进行伸缩
|
||||||
|
|
||||||
@@ -526,7 +559,7 @@ kubectl scale --replicas=5 rc/foo rc/bar rc/baz # 伸缩多个
|
|||||||
```
|
```
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
## Deleting Resources
|
## Deleting resources
|
||||||
-->
|
-->
|
||||||
## 删除资源
|
## 删除资源
|
||||||
|
|
||||||
@@ -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 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 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
|
# 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
|
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 -f ./pod.json # 删除在 pod.json 中指定的类型和名称的 Pod
|
||||||
kubectl delete pod,service baz foo # 删除名称为 "baz" 和 "foo" 的 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 # 删除包含 name=myLabel 标签的 pods 和服务
|
||||||
kubectl delete pods,services -l name=myLabel --include-uninitialized # 删除包含 label name=myLabel 标签的 Pods 和服务
|
kubectl -n my-ns delete pod,svc --all # 删除在 my-ns 名字空间中全部的 Pods 和服务
|
||||||
kubectl -n my-ns delete po,svc --all # 删除在 my-ns 名字空间中全部的 Pods 和服务
|
|
||||||
# 删除所有与 pattern1 或 pattern2 awk 模式匹配的 Pods
|
# 删除所有与 pattern1 或 pattern2 awk 模式匹配的 Pods
|
||||||
kubectl get pods -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{print $1}' | xargs kubectl delete -n mynamespace pod
|
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 组中的所有
|
|||||||
<!--
|
<!--
|
||||||
### Formatting output
|
### Formatting output
|
||||||
|
|
||||||
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.
|
To output details to your terminal window in a specific format, add the `-o` (or `--output`) flag to a supported `kubectl` command.
|
||||||
-->
|
-->
|
||||||
### 格式化输出
|
### 格式化输出
|
||||||
|
|
||||||
要以特定格式将详细信息输出到终端窗口,可以将 `-o` 或 `--output` 参数添加到支持的 `kubectl` 命令。
|
要以特定格式将详细信息输出到终端窗口,将 `-o`(或者 `--output`)参数添加到支持的 `kubectl` 命令中。
|
||||||
|
|
||||||
<!--O
|
<!--O
|
||||||
utput format | Description
|
Output format | Description
|
||||||
--------------| -----------
|
--------------| -----------
|
||||||
`-o=custom-columns=<spec>` | Print a table using a comma separated list of custom columns
|
`-o=custom-columns=<spec>` | Print a table using a comma separated list of custom columns
|
||||||
`-o=custom-columns-file=<filename>` | Print a table using the custom columns template in the `<filename>` file
|
`-o=custom-columns-file=<filename>` | Print a table using the custom columns template in the `<filename>` file
|
||||||
@@ -773,12 +805,16 @@ Verbosity | Description
|
|||||||
## {{% heading "whatsnext" %}}
|
## {{% heading "whatsnext" %}}
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
* Learn more about [Overview of kubectl](/docs/reference/kubectl/overview/).
|
|
||||||
|
* Read the [kubectl overview](/docs/reference/kubectl/overview/) and learn about [JsonPath](/docs/reference/kubectl/jsonpath).
|
||||||
|
|
||||||
* See [kubectl](/docs/reference/kubectl/kubectl/) options.
|
* See [kubectl](/docs/reference/kubectl/kubectl/) options.
|
||||||
* Also [kubectl Usage Conventions](/docs/reference/kubectl/conventions/) to understand how to use it in reusable scripts.
|
|
||||||
|
* Also read [kubectl Usage Conventions](/docs/reference/kubectl/conventions/) to understand how to use kubectl in reusable scripts.
|
||||||
|
|
||||||
* See more community [kubectl cheatsheets](https://github.com/dennyzhang/cheatsheet-kubernetes-A4).
|
* See more community [kubectl cheatsheets](https://github.com/dennyzhang/cheatsheet-kubernetes-A4).
|
||||||
-->
|
-->
|
||||||
* 进一步了解 [kubectl 概述](/zh/docs/reference/kubectl/overview/)。
|
* 参阅 [kubectl 概述](/zh/docs/reference/kubectl/overview/),进一步了解[JsonPath](/zh/docs/reference/kubectl/jsonpath)。
|
||||||
* 参阅 [kubectl](/zh/docs/reference/kubectl/kubectl/) 选项.
|
* 参阅 [kubectl](/zh/docs/reference/kubectl/kubectl/) 选项。
|
||||||
* 参阅 [kubectl 使用约定](/zh/docs/reference/kubectl/conventions/)来理解如何在可复用的脚本中使用它。
|
* 参阅 [kubectl 使用约定](/zh/docs/reference/kubectl/conventions/)来理解如何在可复用的脚本中使用它。
|
||||||
* 查看社区中其他的 [kubectl 备忘单](https://github.com/dennyzhang/cheatsheet-kubernetes-A4)。
|
* 查看社区中其他的 [kubectl 备忘单](https://github.com/dennyzhang/cheatsheet-kubernetes-A4)。
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ JSONPath 模板由 {} 包起来的 JSONPath 表达式组成。Kubectl 使用 JSO
|
|||||||
-->
|
-->
|
||||||
1. 使用双引号将 JSONPath 表达式内的文本引起来。
|
1. 使用双引号将 JSONPath 表达式内的文本引起来。
|
||||||
2. 使用 `range`,`end` 运算符来迭代列表。
|
2. 使用 `range`,`end` 运算符来迭代列表。
|
||||||
3. 使用负片索引后退列表。负索引不会"环绕"列表,并且只要 `-index + listLength> = 0` 就有效。
|
3. 使用负片索引后退列表。负索引不会“环绕”列表,并且只要 `-index + listLength> = 0` 就有效。
|
||||||
|
|
||||||
{{< note >}}
|
{{< note >}}
|
||||||
<!--
|
<!--
|
||||||
@@ -95,7 +95,7 @@ Function | Description | Example
|
|||||||
--------------------|---------------------------|-----------------------------------------------------------------|------------------
|
--------------------|---------------------------|-----------------------------------------------------------------|------------------
|
||||||
`text` | the plain text | `kind is {.kind}` | `kind is List`
|
`text` | the plain text | `kind is {.kind}` | `kind is List`
|
||||||
`@` | the current object | `{@}` | the same as input
|
`@` | the current object | `{@}` | the same as input
|
||||||
`.` or `[]` | child operator | `{.kind}` or `{['kind']}` | `List`
|
`.` or `[]` | child operator | `{.kind}`, `{['kind']}` or `{['name\.type']}` | `List`
|
||||||
`..` | recursive descent | `{..name}` | `127.0.0.1 127.0.0.2 myself e2e`
|
`..` | recursive descent | `{..name}` | `127.0.0.1 127.0.0.2 myself e2e`
|
||||||
`*` | wildcard. Get all objects | `{.items[*].metadata.name}` | `[127.0.0.1 127.0.0.2]`
|
`*` | wildcard. Get all objects | `{.items[*].metadata.name}` | `[127.0.0.1 127.0.0.2]`
|
||||||
`[start:end :step]` | subscript operator | `{.users[0].name}` | `myself`
|
`[start:end :step]` | subscript operator | `{.users[0].name}` | `myself`
|
||||||
@@ -108,7 +108,7 @@ Function | Description | Example
|
|||||||
--------------------|---------------------------|-----------------------------------------------------------------|------------------
|
--------------------|---------------------------|-----------------------------------------------------------------|------------------
|
||||||
`text` | 纯文本 | `kind is {.kind}` | `kind is List`
|
`text` | 纯文本 | `kind is {.kind}` | `kind is List`
|
||||||
`@` | 当前对象 | `{@}` | 与输入相同
|
`@` | 当前对象 | `{@}` | 与输入相同
|
||||||
`.` or `[]` | 子运算符 | `{.kind}` or `{['kind']}` | `List`
|
`.` or `[]` | 子运算符 | `{.kind}`, `{['kind']}` or `{['name\.type']}` | `List`
|
||||||
`..` | 递归下降 | `{..name}` | `127.0.0.1 127.0.0.2 myself e2e`
|
`..` | 递归下降 | `{..name}` | `127.0.0.1 127.0.0.2 myself e2e`
|
||||||
`*` | 通配符。获取所有对象 | `{.items[*].metadata.name}` | `[127.0.0.1 127.0.0.2]`
|
`*` | 通配符。获取所有对象 | `{.items[*].metadata.name}` | `[127.0.0.1 127.0.0.2]`
|
||||||
`[start:end :step]` | 下标运算符 | `{.users[0].name}` | `myself`
|
`[start:end :step]` | 下标运算符 | `{.users[0].name}` | `myself`
|
||||||
@@ -127,17 +127,53 @@ kubectl get pods -o json
|
|||||||
kubectl get pods -o=jsonpath='{@}'
|
kubectl get pods -o=jsonpath='{@}'
|
||||||
kubectl get pods -o=jsonpath='{.items[0]}'
|
kubectl get pods -o=jsonpath='{.items[0]}'
|
||||||
kubectl get pods -o=jsonpath='{.items[0].metadata.name}'
|
kubectl get pods -o=jsonpath='{.items[0].metadata.name}'
|
||||||
|
kubectl get pods -o=jsonpath="{.items[*]['metadata.name', 'status.capacity']}"
|
||||||
kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'
|
kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'
|
||||||
```
|
```
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
On Windows, you must _double_ quote any JSONPath template that contains spaces (not single quote as shown above for bash). This in turn means that you must use a single quote or escaped double quote around any literals in the template. For example:
|
{{< note >}}
|
||||||
|
On Windows, you must _double_ quote any JSONPath template that contains spaces (not single quote as shown above for bash). This in turn means that you must use a single quote or escaped double quote around any literals in the template. For example:
|
||||||
|
|
||||||
|
```cmd
|
||||||
|
kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{'\t'}{.status.startTime}{'\n'}{end}"
|
||||||
|
kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{\"\t\"}{.status.startTime}{\"\n\"}{end}"
|
||||||
|
```
|
||||||
|
{{< /note >}}
|
||||||
-->
|
-->
|
||||||
在 Windows 上,您必须 _double_ 引用任何包含空格的 JSONPath 模板(不是上面 bash 所示的单引号)。反过来,这意味着您必须在模板中的所有文字周围使用单引号或转义的双引号。例如:
|
{{< note >}}
|
||||||
|
在 Windows 上,您必须用双引号把任何包含空格的 JSONPath 模板(不是上面 bash 所示的单引号)。
|
||||||
|
反过来,这意味着您必须在模板中的所有文字周围使用单引号或转义的双引号。
|
||||||
|
例如:
|
||||||
|
|
||||||
```cmd
|
```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}"
|
||||||
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 >}}
|
||||||
|
|
||||||
|
<!--
|
||||||
|
JSONPath regular expressions are not supported. If you want to match using regular expressions, you can use a tool such as `jq`.
|
||||||
|
|
||||||
|
```shell
|
||||||
|
# kubectl does not support regular expressions for JSONpath output
|
||||||
|
# The following command does not work
|
||||||
|
kubectl get pods -o jsonpath='{.items[?(@.metadata.name=~/^test$/)].metadata.name}'
|
||||||
|
|
||||||
|
# The following command achieves the desired result
|
||||||
|
kubectl get pods -o json | jq -r '.items[] | select(.metadata.name | test("test-")).spec.containers[].image'
|
||||||
|
```
|
||||||
|
-->
|
||||||
|
{{< 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 >}}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user