6
6
name : reference
7
7
weight : 30
8
8
---
9
- <!-- ---
9
+ <!--
10
10
title: kubectl Cheat Sheet
11
11
reviewers:
12
12
- erictune
@@ -17,7 +17,6 @@ weight: 10 # highlight it
17
17
card:
18
18
name: reference
19
19
weight: 30
20
- ---
21
20
-->
22
21
23
22
<!-- overview -->
@@ -35,7 +34,6 @@ This page contains a list of commonly used `kubectl` commands and flags.
35
34
36
35
### BASH
37
36
-->
38
-
39
37
## Kubectl 自动补全 {#kubectl-autocomplete}
40
38
41
39
### BASH
@@ -79,7 +77,7 @@ echo '[[ $commands[kubectl] ]] && source <(kubectl completion zsh)' >> ~/.zshrc
79
77
### 关于 ` --all-namespaces ` 的一点说明 {#a-note-on-all-namespaces}
80
78
81
79
<!--
82
- Appending `--all-namespaces` happens frequently enough where you should be aware of the shorthand for `--all-namespaces`:
80
+ Appending `--all-namespaces` happens frequently enough where you should be aware of the shorthand for `--all-namespaces`:
83
81
-->
84
82
我们经常用到 ` --all-namespaces ` 参数,你应该要知道它的简写:
85
83
@@ -385,6 +383,9 @@ kubectl get pods --all-namespaces -o jsonpath='{range .items[*].status.initConta
385
383
# List Events sorted by timestamp
386
384
kubectl get events --sort-by=.metadata.creationTimestamp
387
385
386
+ # List all warning events
387
+ kubectl events --types=Warning
388
+
388
389
# Compares the current state of the cluster against the state that the cluster would be in if the manifest was applied.
389
390
kubectl diff -f ./my-manifest.yaml
390
391
@@ -470,6 +471,9 @@ kubectl get pods --all-namespaces -o jsonpath='{range .items[*].status.initConta
470
471
# 列出事件(Events),按时间戳排序
471
472
kubectl get events --sort-by=.metadata.creationTimestamp
472
473
474
+ # 列出所有警告事件
475
+ kubectl events --types=Warning
476
+
473
477
# 比较当前的集群状态和假定某清单被应用之后的集群状态
474
478
kubectl diff -f ./my-manifest.yaml
475
479
@@ -566,7 +570,7 @@ kubectl patch deployment valid-deployment --type json -p='[{"op": "remove", "
566
570
# Add a new element to a positional array
567
571
kubectl patch sa default --type='json' -p='[{"op": "add", "path": "/secrets/1", "value": {"name": "whatever" } }]'
568
572
569
- # Update a deployment's replica count by patching it's scale subresource
573
+ # Update a deployment's replica count by patching its scale subresource
570
574
kubectl patch deployment nginx-deployment --subresource='scale' --type='merge' -p '{"spec":{"replicas":2}}'
571
575
```
572
576
-->
@@ -638,6 +642,7 @@ kubectl scale --replicas=5 rc/foo rc/bar rc/baz # 伸缩多个
638
642
<!--
639
643
```bash
640
644
kubectl delete -f ./pod.json # Delete a pod using the type and name specified in pod.json
645
+ kubectl delete pod unwanted --now # Delete a pod with no grace period
641
646
kubectl delete pod,service baz foo # Delete pods and services with same names "baz" and "foo"
642
647
kubectl delete pods,services -l name=myLabel # Delete pods and services with label name=myLabel
643
648
kubectl -n my-ns delete pod,svc --all # Delete all pods and services in namespace my-ns,
@@ -647,6 +652,7 @@ kubectl get pods -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{pr
647
652
-->
648
653
``` bash
649
654
kubectl delete -f ./pod.json # 删除在 pod.json 中指定的类型和名称的 Pod
655
+ kubectl delete pod unwanted --now # 删除 Pod 且无宽限期限(无优雅时段)
650
656
kubectl delete pod,service baz foo # 删除名称为 "baz" 和 "foo" 的 Pod 和服务
651
657
kubectl delete pods,services -l name=myLabel # 删除包含 name=myLabel 标签的 pods 和服务
652
658
kubectl -n my-ns delete pod,svc --all # 删除在 my-ns 名字空间中全部的 Pods 和服务
@@ -860,7 +866,7 @@ To output details to your terminal window in a specific format, add the `-o` (or
860
866
861
867
要以特定格式将详细信息输出到终端窗口,将 ` -o ` (或者 ` --output ` )参数添加到支持的 ` kubectl ` 命令中。
862
868
863
- <!-- O
869
+ <!--
864
870
Output format | Description
865
871
--------------| -----------
866
872
`-o=custom-columns=<spec>` | Print a table using a comma separated list of custom columns
@@ -898,8 +904,6 @@ kubectl get pods -A -o=custom-columns='DATA:spec.containers[?(@.image!="registry
898
904
899
905
# All fields under metadata regardless of name
900
906
kubectl get pods -A -o=custom-columns='DATA:metadata.*'
901
-
902
- More examples in the kubectl [reference documentation](/docs/reference/kubectl/#custom-columns).
903
907
```
904
908
-->
905
909
使用 ` -o=custom-columns ` 的示例:
0 commit comments