Skip to content

Commit 16b5435

Browse files
authored
Merge pull request #28401 from chenxuc/zh-resync-10-2
resync reference for overview and cheatsheet
2 parents 7d4ed84 + 6039c1b commit 16b5435

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
2-
title: "kubectl 命令行界面"
2+
title: "kubectl"
33
weight: 60
44
---

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

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ kubectl get pods --show-labels
332332
JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \
333333
&& kubectl get nodes -o jsonpath="$JSONPATH" | grep "Ready=True"
334334
335+
# Output decoded secrets without external tools
336+
kubectl get secret my-secret -o go-template='{{range $k,$v := .data}}{{"### "}}{{$k}}{{"\n"}}{{$v|base64decode}}{{"\n\n"}}{{end}}'
337+
335338
# List all Secrets currently in use by a pod
336339
kubectl get pods -o json | jq '.items[].spec.containers[].env[]?.valueFrom.secretKeyRef.name' | grep -v null | sort | uniq
337340
@@ -352,6 +355,9 @@ kubectl get nodes -o json | jq -c 'path(..)|[.[]|tostring]|join(".")'
352355
# Produce a period-delimited tree of all keys returned for pods, etc
353356
kubectl get pods -o json | jq -c 'path(..)|[.[]|tostring]|join(".")'
354357
358+
# Produce ENV for all pods, assuming you have a default container for the pods, default namespace and the `env` command is supported.
359+
# Helpful when running any supported command across all pods, not just `env`
360+
for pod in $(kubectl get po --output=jsonpath={.items..metadata.name}); do echo $pod && kubectl exec -it $pod env; done
355361
```
356362
-->
357363
```bash
@@ -405,11 +411,14 @@ kubectl get pods --show-labels
405411
JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \
406412
&& kubectl get nodes -o jsonpath="$JSONPATH" | grep "Ready=True"
407413

414+
# 不使用外部工具来输出解码后的 Secret
415+
kubectl get secret my-secret -o go-template='{{range $k,$v := .data}}{{"### "}}{{$k}}{{"\n"}}{{$v|base64decode}}{{"\n\n"}}{{end}}'
416+
408417
# 列出被一个 Pod 使用的全部 Secret
409418
kubectl get pods -o json | jq '.items[].spec.containers[].env[]?.valueFrom.secretKeyRef.name' | grep -v null | sort | uniq
410419

411420
# 列举所有 Pods 中初始化容器的容器 ID(containerID)
412-
# Helpful when cleaning up stopped containers, while avoiding removal of initContainers.
421+
# 可用于在清理已停止的容器时避免删除初始化容器
413422
kubectl get pods --all-namespaces -o jsonpath='{range .items[*].status.initContainerStatuses[*]}{.containerID}{"\n"}{end}' | cut -d/ -f3
414423

415424
# 列出事件(Events),按时间戳排序
@@ -425,6 +434,9 @@ kubectl get nodes -o json | jq -c 'path(..)|[.[]|tostring]|join(".")'
425434
# 生成一个句点分隔的树,其中包含为pod等返回的所有键
426435
kubectl get pods -o json | jq -c 'path(..)|[.[]|tostring]|join(".")'
427436

437+
# 假设你的 Pods 有默认的容器和默认的名字空间,并且支持 'env' 命令,可以使用以下脚本为所有 Pods 生成 ENV 变量。
438+
# 该脚本也可用于在所有的 Pods 里运行任何受支持的命令,而不仅仅是 'env'。
439+
for pod in $(kubectl get po --output=jsonpath={.items..metadata.name}); do echo $pod && kubectl exec -it $pod env; done
428440
```
429441

430442
<!--
@@ -609,6 +621,7 @@ kubectl exec my-pod -- ls / # Run command in existing po
609621
kubectl exec --stdin --tty my-pod -- /bin/sh # Interactive shell access to a running pod (1 container case)
610622
kubectl exec my-pod -c my-container -- ls / # Run command in existing pod (multi-container case)
611623
kubectl top pod POD_NAME --containers # Show metrics for a given pod and its containers
624+
kubectl top pod POD_NAME --sort-by=cpu # Show metrics for a given pod and sort it by 'cpu' or 'memory'
612625
```
613626
-->
614627
```bash
@@ -632,6 +645,35 @@ kubectl exec my-pod -- ls / # 在已有的 Pod 中运行
632645
kubectl exec --stdin --tty my-pod -- /bin/sh # 使用交互 shell 访问正在运行的 Pod (一个容器场景)
633646
kubectl exec my-pod -c my-container -- ls / # 在已有的 Pod 中运行命令(多容器场景)
634647
kubectl top pod POD_NAME --containers # 显示给定 Pod 和其中容器的监控数据
648+
kubectl top pod POD_NAME --sort-by=cpu # 显示给定 Pod 的指标并且按照 'cpu' 或者 'memory' 排序
649+
```
650+
651+
<!--
652+
## Interacting with Deployments and Services
653+
-->
654+
## 与 Deployments 和 Services 进行交互
655+
656+
<!--
657+
```bash
658+
kubectl logs deploy/my-deployment # dump Pod logs for a Deployment (single-container case)
659+
kubectl logs deploy/my-deployment -c my-container # dump Pod logs for a Deployment (multi-container case)
660+
661+
kubectl port-forward svc/my-service 5000 # listen on local port 5000 and forward to port 5000 on Service backend
662+
kubectl port-forward svc/my-service 5000:my-service-port # listen on local port 5000 and forward to Service target port with name <my-service-port>
663+
664+
kubectl port-forward deploy/my-deployment 5000:6000 # listen on local port 5000 and forward to port 6000 on a Pod created by <my-deployment>
665+
kubectl exec deploy/my-deployment -- ls # run command in first Pod and first container in Deployment (single- or multi-container cases)
666+
```
667+
-->
668+
```bash
669+
kubectl logs deploy/my-deployment # 获取一个 Deployment 的 Pod 的日志(单容器例子)
670+
kubectl logs deploy/my-deployment -c my-container # 获取一个 Deployment 的 Pod 的日志(多容器例子)
671+
672+
kubectl port-forward svc/my-service 5000 # 侦听本地端口 5000 并转发到 Service 后端端口 5000
673+
kubectl port-forward svc/my-service 5000:my-service-port # 侦听本地端口 5000 并转发到名字为 <my-service-port> 的 Service 目标端口
674+
675+
kubectl port-forward deploy/my-deployment 5000:6000 # 侦听本地端口 5000 并转发到 <my-deployment> 创建的 Pod 里的端口 6000
676+
kubectl exec deploy/my-deployment -- ls # 在 Deployment 里的第一个 Pod 的第一个容器里运行命令(单容器和多容器例子)
635677
```
636678

637679
<!--
@@ -672,9 +714,9 @@ kubectl taint nodes foo dedicated=special-user:NoSchedule
672714
### 资源类型
673715

674716
<!--
675-
List all supported resource types along with their shortnames, [API group](/docs/concepts/overview/kubernetes-api/#api-groups), whether they are [namespaced](/docs/concepts/overview/working-with-objects/namespaces), and [Kind](/docs/concepts/overview/working-with-objects/kubernetes-objects):
717+
List all supported resource types along with their shortnames, [API group](/docs/concepts/overview/kubernetes-api/#api-groups-and-versioning), whether they are [namespaced](/docs/concepts/overview/working-with-objects/namespaces), and [Kind](/docs/concepts/overview/working-with-objects/kubernetes-objects):
676718
-->
677-
列出所支持的全部资源类型和它们的简称、[API 组](/zh/docs/concepts/overview/kubernetes-api/#api-groups), 是否是[名字空间作用域](/zh/docs/concepts/overview/working-with-objects/namespaces)[Kind](/zh/docs/concepts/overview/working-with-objects/kubernetes-objects)
719+
列出所支持的全部资源类型和它们的简称、[API 组](/zh/docs/concepts/overview/kubernetes-api/#api-groups-and-versioning), 是否是[名字空间作用域](/zh/docs/concepts/overview/working-with-objects/namespaces)[Kind](/zh/docs/concepts/overview/working-with-objects/kubernetes-objects)
678720

679721
```bash
680722
kubectl api-resources
@@ -689,7 +731,7 @@ Other operations for exploring API resources:
689731
```bash
690732
kubectl api-resources --namespaced=true # All namespaced resources
691733
kubectl api-resources --namespaced=false # All non-namespaced resources
692-
kubectl api-resources -o name # All resources with simple output (just the resource name)
734+
kubectl api-resources -o name # All resources with simple output (only the resource name)
693735
kubectl api-resources -o wide # All resources with expanded (aka "wide") output
694736
kubectl api-resources --verbs=list,get # All resources that support the "list" and "get" request verbs
695737
kubectl api-resources --api-group=extensions # All resources in the "extensions" API group
@@ -743,7 +785,10 @@ Examples using `-o=custom-columns`:
743785
# All images running in a cluster
744786
kubectl get pods -A -o=custom-columns='DATA:spec.containers[*].image'
745787
746-
# All images excluding "k8s.gcr.io/coredns:1.6.2"
788+
# All images running in namespace: default, grouped by Pod
789+
kubectl get pods --namespace default --output=custom-columns="NAME:.metadata.name,IMAGE:.spec.containers[*].image"
790+
791+
# All images excluding "k8s.gcr.io/coredns:1.6.2"
747792
kubectl get pods -A -o=custom-columns='DATA:spec.containers[?(@.image!="k8s.gcr.io/coredns:1.6.2")].image'
748793
749794
# All fields under metadata regardless of name
@@ -758,7 +803,10 @@ More examples in the kubectl [reference documentation](/docs/reference/kubectl/o
758803
# 集群中运行着的所有镜像
759804
kubectl get pods -A -o=custom-columns='DATA:spec.containers[*].image'
760805

761-
# 除 "k8s.gcr.io/coredns:1.6.2" 之外的所有镜像
806+
# 列举 default 名字空间中运行的所有镜像,按 Pod 分组
807+
kubectl get pods --namespace default --output=custom-columns="NAME:.metadata.name,IMAGE:.spec.containers[*].image"
808+
809+
# 除 "k8s.gcr.io/coredns:1.6.2" 之外的所有镜像
762810
kubectl get pods -A -o=custom-columns='DATA:spec.containers[?(@.image!="k8s.gcr.io/coredns:1.6.2")].image'
763811

764812
# 输出 metadata 下面的所有字段,无论 Pod 名字为何

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ Flags that you specify from the command line override default values and any cor
139139
{{< /caution >}}
140140

141141
<!--
142-
If you need help, just run `kubectl help` from the terminal window.
142+
If you need help, run `kubectl help` from the terminal window.
143143
-->
144-
如果你需要帮助,只需从终端窗口运行 ` kubectl help ` 即可
144+
如果你需要帮助,从终端窗口运行 `kubectl help`
145145

146146
<!--
147147
## Operations

0 commit comments

Comments
 (0)