Skip to content

Commit 3a7af1e

Browse files
committed
feat: add kubernetes.md cheatsheet. #44
1 parent 9ef65a9 commit 3a7af1e

File tree

3 files changed

+348
-0
lines changed

3 files changed

+348
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Quick Reference
5555
[Java](./docs/java.md)<!--rehype:style=background: rgb(211 55 49);&class=contributing&data-info=👆看看还缺点儿什么?-->
5656
[Julia](./docs/julia.md)<!--rehype:style=background: rgb(211 55 49);&class=contributing&data-info=👆看看还缺点儿什么?-->
5757
[Kotlin](./docs/kotlin.md)<!--rehype:style=background: rgb(211 55 49);&class=contributing&data-info=👆看看还缺点儿什么?-->
58+
[Kubernetes](./docs/kubernetes.md)<!--rehype:style=background: rgb(211 55 49);&class=contributing&data-info=👆看看还缺点儿什么?-->
5859
[LaTeX](./docs/latex.md)<!--rehype:style=background: rgb(0 128 128);&class=contributing-->
5960
[Laravel 8](./docs/laravel.md)<!--rehype:style=background: rgb(249 50 44);&class=contributing tag&data-lang=PHP-->
6061
[Markdown](./docs/markdown.md)<!--rehype:style=background: rgb(103 61 156);-->

assets/kubernetes.svg

Lines changed: 5 additions & 0 deletions
Loading

docs/kubernetes.md

Lines changed: 342 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,342 @@
1+
Kubernetes 备忘清单
2+
===
3+
4+
查看资源信息
5+
---
6+
7+
### 节点
8+
9+
资源名称: nodes, 缩写: no
10+
11+
```bash
12+
$ kubectl get no # 显示所有节点信息
13+
$ kubectl get no -o wide # 显示所有节点的更多信息
14+
$ kubectl describe no # 显示节点详情
15+
$ kubectl get no -o yaml # 以yaml格式,显示节点详情
16+
$ kubectl get node --selector=[label_name] # 筛选指定标签的节点
17+
$ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
18+
# 输出jsonpath表达式定义的字段信息
19+
$ kubectl top node [node_name] # 显示节点(CPU/内存/存储)使用情况
20+
```
21+
22+
### 容器组
23+
24+
资源名称: pods, 缩写: po
25+
26+
```bash
27+
$ kubectl get po # 显示所有容器组信息
28+
$ kubectl get po -o wide
29+
$ kubectl describe po
30+
$ kubectl get po --show-labels # 查看容器组的labels
31+
$ kubectl get po -l app=nginx
32+
$ kubectl get po -o yaml
33+
$ kubectl get pod [pod_name] -o yaml --export
34+
$ kubectl get pod [pod_name] -o yaml --export > nameoffile.yaml
35+
# 以yaml格式导出容器组信息到yaml文件
36+
$ kubectl get pods --field-selector status.phase=Running
37+
# 使用字段选择器筛选出容器组信息
38+
```
39+
40+
### 命名空间
41+
42+
资源名称: namespaces, 缩写: ns
43+
44+
```bash
45+
$ kubectl get ns
46+
$ kubectl get ns -o yaml
47+
$ kubectl describe ns
48+
```
49+
50+
### 无状态
51+
52+
资源名称: deployments, 缩写: deploy
53+
54+
```bash
55+
$ kubectl get deploy
56+
$ kubectl describe deploy
57+
$ kubectl get deploy -o wide
58+
$ kubectl get deploy -o yaml
59+
```
60+
61+
### 服务
62+
63+
资源名称: services, 缩写: svc
64+
65+
```bash
66+
$ kubectl get svc
67+
$ kubectl describe svc
68+
$ kubectl get svc -o wide
69+
$ kubectl get svc -o yaml
70+
$ kubectl get svc --show-labels
71+
```
72+
73+
### 守护进程集
74+
75+
资源名称: daemonsets, 缩写: ds
76+
77+
```bash
78+
$ kubectl get ds
79+
$ kubectl describe ds --all-namespaces
80+
$ kubectl describe ds [daemonset_name] -n [namespace_name]
81+
$ kubectl get ds [ds_name] -n [ns_name] -o yaml
82+
```
83+
84+
### 事件
85+
86+
资源名称: events, 缩写: ev
87+
88+
```bash
89+
$ kubectl get events
90+
$ kubectl get events -n kube-system
91+
$ kubectl get events -w
92+
```
93+
94+
### 日志
95+
96+
```bash
97+
$ kubectl logs [pod_name]
98+
$ kubectl logs --since=1h [pod_name]
99+
$ kubectl logs --tail=20 [pod_name]
100+
$ kubectl logs -f -c [container_name] [pod_name]
101+
$ kubectl logs [pod_name] > pod.log
102+
```
103+
104+
### 服务帐户
105+
106+
资源名称: serviceaccounts, 缩写: sa
107+
108+
```bash
109+
$ kubectl get sa
110+
$ kubectl get sa -o yaml
111+
$ kubectl get serviceaccounts default -o yaml >./sa.yaml
112+
$ kubectl replace serviceaccount default -f ./sa.yaml
113+
```
114+
115+
### 副本集
116+
117+
资源名称: replicasets, 缩写: rs
118+
119+
```bash
120+
$ kubectl get rs
121+
$ kubectl describe rs
122+
$ kubectl get rs -o wide
123+
$ kubectl get rs -o yaml
124+
```
125+
126+
### 角色
127+
128+
```bash
129+
$ kubectl get roles --all-namespaces
130+
$ kubectl get roles --all-namespaces -o yaml
131+
```
132+
133+
### 保密字典
134+
135+
```bash
136+
$ kubectl get secrets
137+
$ kubectl get secrets --all-namespaces
138+
$ kubectl get secrets -o yaml
139+
```
140+
141+
### 配置项
142+
143+
资源名称: configmaps, 缩写: cm
144+
145+
```bash
146+
$ kubectl get cm
147+
$ kubectl get cm --all-namespaces
148+
$ kubectl get cm --all-namespaces -o yaml
149+
```
150+
151+
### 路由
152+
153+
资源名称: ingresses, 缩写: ing
154+
155+
```bash
156+
$ kubectl get ing
157+
$ kubectl get ing --all-namespaces
158+
```
159+
160+
### 持久卷
161+
162+
资源名称: persistentvolumes, 缩写: pv
163+
164+
```bash
165+
$ kubectl get pv
166+
$ kubectl describe pv
167+
```
168+
169+
### 持久卷声明
170+
171+
资源名称: persistentvolumeclaims, 缩写: pvc
172+
173+
```bash
174+
$ kubectl get pvc
175+
$ kubectl describe pvc
176+
```
177+
178+
### 存储类
179+
180+
资源名称: storageclasses, 缩写: sc
181+
182+
```bash
183+
$ kubectl get sc
184+
$ kubectl get sc -o yaml
185+
```
186+
187+
### 多个资源
188+
189+
```bash
190+
$ kubectl get svc, po
191+
$ kubectl get deploy, no
192+
$ kubectl get all
193+
$ kubectl get all --all-namespaces
194+
```
195+
196+
变更资源属性
197+
---
198+
199+
### 污点
200+
201+
```bash
202+
$ kubectl taint [node_name] [taint_name]
203+
```
204+
205+
### 标签
206+
207+
```bash
208+
$ kubectl label [node_name] disktype=ssd
209+
$ kubectl label [pod_name] env=prod
210+
```
211+
212+
### 维护/可调度
213+
214+
```bash
215+
$ kubectl cordon [node_name] # 节点维护
216+
$ kubectl uncordon [node_name] # 节点可调度
217+
```
218+
219+
### 清空节点
220+
221+
```bash
222+
$ kubectl drain [node_name] # 清空节点
223+
```
224+
225+
### 节点/容器组
226+
227+
```bash
228+
$ kubectl delete node [node_name]
229+
$ kubectl delete pod [pod_name]
230+
$ kubectl edit node [node_name]
231+
$ kubectl edit pod [pod_name]
232+
```
233+
234+
### 无状态/命名空间
235+
236+
```bash
237+
$ kubectl edit deploy [deploy_name]
238+
$ kubectl delete deploy [deploy_name]
239+
$ kubectl expose deploy [deploy_name] --port=80 --type=NodePort
240+
$ kubectl scale deploy [deploy_name] --replicas=5
241+
$ kubectl delete ns
242+
$ kubectl edit ns [ns_name]
243+
```
244+
245+
### 服务
246+
247+
```bash
248+
$ kubectl edit svc [svc_name]
249+
$ kubectl delete svc [svc_name]
250+
```
251+
252+
### 守护进程集
253+
254+
```bash
255+
$ kubectl edit ds [ds_name] -n kube-system
256+
$ kubectl delete ds [ds_name]
257+
```
258+
259+
### 服务账号
260+
261+
```bash
262+
$ kubectl edit sa [sa_name]
263+
$ kubectl delete sa [sa_name]
264+
```
265+
266+
### 注释
267+
268+
```bash
269+
$ kubectl annotatepo [pod_name] [annotation]
270+
$ kubectl annotateno [node_name]
271+
```
272+
273+
添加资源
274+
---
275+
276+
### 创建容器组
277+
278+
```bash
279+
$ kubectl create -f [name_of_file]
280+
$ kubectl apply -f [name_of_file]
281+
$ kubectl run [pod_name] --image=nginx --restart=Never
282+
$ kubectl run [pod_name] --generator=run-pod/v1 --image=nginx
283+
$ kubectl run [pod_name] --image=nginx --restart=Never
284+
```
285+
286+
### 创建服务
287+
288+
```bash
289+
$ kubectl create svc nodeport [svc_name] --tcp=8080:80
290+
```
291+
292+
### 创建无状态应用
293+
294+
```bash
295+
$ kubectl create -f [name_of_file]
296+
$ kubectl apply -f [name_of_file]
297+
$ kubectl create deploy [deploy_name] --image=nginx
298+
```
299+
300+
### 容器交互
301+
302+
```bash
303+
$ kubectl run [pod_name] --image=busybox --rm -it --restart=Never -- sh
304+
```
305+
306+
### 输出YAML文件
307+
308+
```bash
309+
$ kubectl create deploy [deploy_name] --image=nginx --dry-run -o yaml > deploy.yaml
310+
$ kubectl get po [pod_name] -o yaml --export > pod.yaml
311+
```
312+
313+
### 获取帮助
314+
315+
```bash
316+
$ kubectl -h
317+
$ kubectl create -h
318+
$ kubectl run -h
319+
$ kubectl explain deploy.spec
320+
```
321+
322+
请求
323+
---
324+
325+
### API调用
326+
327+
```bash
328+
$ kubectl get --raw /apis/metrics.k8s.io/
329+
```
330+
331+
### 集群信息
332+
333+
```bash
334+
$ kubectl config
335+
$ kubectl cluster-info
336+
$ kubectl get componentstatus
337+
```
338+
339+
另见
340+
---
341+
342+
- [Kubernetes 官方文档 命令行工具 (kubectl)](https://kubernetes.io/zh-cn/docs/reference/kubectl/) _(kubernetes.io)_

0 commit comments

Comments
 (0)