doc: update docs/kubernetes.md

This commit is contained in:
jaywcjlove 2024-04-09 18:10:19 +08:00
parent 0bced7ac0f
commit 38bbf5d6d3

View File

@ -10,24 +10,29 @@ Kubernetes 备忘清单
```bash ```bash
$ kubectl get no # 显示所有节点信息 $ kubectl get no # 显示所有节点信息
$ kubectl get no -o wide # 显示所有节点的更多信息 # 显示所有节点的更多信息
$ kubectl get no -o wide
$ kubectl describe no # 显示节点详情 $ kubectl describe no # 显示节点详情
$ kubectl get no -o yaml # 以yaml格式显示节点详情 # 以yaml格式显示节点详情
$ kubectl get node --selector=[label_name] # 筛选指定标签的节点 $ kubectl get no -o yaml
# 筛选指定标签的节点
$ kubectl get node --selector=[label_name]
# 输出 jsonpath 表达式定义的字段信息
$ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}' $ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
# 输出jsonpath表达式定义的字段信息 # 显示节点(CPU/内存/存储)使用情况
$ kubectl top node [node_name] # 显示节点(CPU/内存/存储)使用情况 $ kubectl top node [node_name]
``` ```
### 容器组 ### 容器组
<!--rehype:wrap-class=col-span-2-->
资源名称: pods, 缩写: po 资源名称: pods, 缩写: po
```bash ```bash
$ kubectl get po # 显示所有容器组信息 $ kubectl get po # 显示所有容器组信息
$ kubectl get po -o wide $ kubectl get po -o wide
$ kubectl describe po $ kubectl describe po
$ kubectl get po --show-labels # 查看容器组的labels $ kubectl get po --show-labels # 查看容器组的labels
$ kubectl get po -l app=nginx $ kubectl get po -l app=nginx
$ kubectl get po -o yaml $ kubectl get po -o yaml
$ kubectl get pod [pod_name] -o yaml --export $ kubectl get pod [pod_name] -o yaml --export
@ -39,7 +44,7 @@ $ kubectl get pods --field-selector status.phase=Running
### 命名空间 ### 命名空间
资源名称: namespaces, 缩写: ns 资源名称: `namespaces`, 缩写: `ns`
```bash ```bash
$ kubectl get ns $ kubectl get ns
@ -49,7 +54,7 @@ $ kubectl describe ns
### 无状态 ### 无状态
资源名称: deployments, 缩写: deploy 资源名称: `deployments`, 缩写: `deploy`
```bash ```bash
$ kubectl get deploy $ kubectl get deploy
@ -60,7 +65,7 @@ $ kubectl get deploy -o yaml
### 服务 ### 服务
资源名称: services, 缩写: svc 资源名称: `services`, 缩写: `svc`
```bash ```bash
$ kubectl get svc $ kubectl get svc
@ -71,8 +76,9 @@ $ kubectl get svc --show-labels
``` ```
### 守护进程集 ### 守护进程集
<!--rehype:wrap-class=col-span-2-->
资源名称: daemonsets, 缩写: ds 资源名称: `daemonsets`, 缩写: `ds`
```bash ```bash
$ kubectl get ds $ kubectl get ds
@ -83,7 +89,7 @@ $ kubectl get ds [ds_name] -n [ns_name] -o yaml
### 事件 ### 事件
资源名称: events, 缩写: ev 资源名称: `events`, 缩写: `ev`
```bash ```bash
$ kubectl get events $ kubectl get events
@ -91,27 +97,29 @@ $ kubectl get events -n kube-system
$ kubectl get events -w $ kubectl get events -w
``` ```
### 服务帐户
<!--rehype:wrap-class=col-span-2-->
资源名称: `serviceaccounts`, 缩写: `sa`
```bash
$ kubectl get sa
$ kubectl get sa -o yaml
$ kubectl get serviceaccounts default -o yaml >./sa.yaml
$ kubectl replace serviceaccount default -f ./sa.yaml
```
### 日志 ### 日志
```bash ```bash
$ kubectl logs [pod_name] $ kubectl logs [pod_name]
$ kubectl logs --since=1h [pod_name] $ kubectl logs --since=1h [pod_name]
$ kubectl logs --tail=20 [pod_name] $ kubectl logs --tail=20 [pod_name]
$ kubectl logs -f -c [container_name] [pod_name] $ kubectl logs \
-f -c [container_name] [pod_name]
$ kubectl logs [pod_name] > pod.log $ kubectl logs [pod_name] > pod.log
``` ```
### 服务帐户
资源名称: serviceaccounts, 缩写: sa
```bash
$ kubectl get sa
$ kubectl get sa -o yaml
$ kubectl get serviceaccounts default -o yaml >./sa.yaml
$ kubectl replace serviceaccount default -f ./sa.yaml
```
### 副本集 ### 副本集
资源名称: replicasets, 缩写: rs 资源名称: replicasets, 缩写: rs
@ -127,7 +135,13 @@ $ kubectl get rs -o yaml
```bash ```bash
$ kubectl get roles --all-namespaces $ kubectl get roles --all-namespaces
$ kubectl get roles --all-namespaces -o yaml ```
---
```
$ kubectl get roles \
--all-namespaces -o yaml
``` ```
### 保密字典 ### 保密字典
@ -203,6 +217,7 @@ $ kubectl taint [node_name] [taint_name]
``` ```
### 标签 ### 标签
<!--rehype:wrap-class=col-span-2-->
```bash ```bash
$ kubectl label nodes <node-name> <label-key>=<label-value> #增加 $ kubectl label nodes <node-name> <label-key>=<label-value> #增加
@ -233,6 +248,7 @@ $ kubectl edit pod [pod_name]
``` ```
### 无状态/命名空间 ### 无状态/命名空间
<!--rehype:wrap-class=col-span-2 row-span-2-->
```bash ```bash
$ kubectl edit deploy [deploy_name] $ kubectl edit deploy [deploy_name]
@ -265,6 +281,7 @@ $ kubectl delete sa [sa_name]
``` ```
### 注释 ### 注释
<!--rehype:wrap-class=col-span-2-->
```bash ```bash
$ kubectl annotatepo [pod_name] [annotation] $ kubectl annotatepo [pod_name] [annotation]
@ -275,6 +292,7 @@ $ kubectl annotateno [node_name]
--- ---
### 创建容器组 ### 创建容器组
<!--rehype:wrap-class=col-span-2-->
```bash ```bash
$ kubectl create -f [name_of_file] $ kubectl create -f [name_of_file]
@ -287,7 +305,8 @@ $ kubectl run [pod_name] --image=nginx --restart=Never
### 创建服务 ### 创建服务
```bash ```bash
$ kubectl create svc nodeport [svc_name] --tcp=8080:80 $ kubectl create svc nodeport [svc_name] \
--tcp=8080:80
``` ```
### 创建无状态应用 ### 创建无状态应用
@ -295,16 +314,12 @@ $ kubectl create svc nodeport [svc_name] --tcp=8080:80
```bash ```bash
$ kubectl create -f [name_of_file] $ kubectl create -f [name_of_file]
$ kubectl apply -f [name_of_file] $ kubectl apply -f [name_of_file]
$ kubectl create deploy [deploy_name] --image=nginx $ kubectl create deploy [deploy_name] \
``` --image=nginx
### 容器交互
```bash
$ kubectl run [pod_name] --image=busybox --rm -it --restart=Never -- sh
``` ```
### 输出YAML文件 ### 输出YAML文件
<!--rehype:wrap-class=col-span-2-->
```bash ```bash
$ kubectl create deploy [deploy_name] --image=nginx --dry-run -o yaml > deploy.yaml $ kubectl create deploy [deploy_name] --image=nginx --dry-run -o yaml > deploy.yaml
@ -312,6 +327,14 @@ $ kubectl get po [pod_name] -o yaml --export > pod.yaml
$ kubectl run nginx --image=nginx:alpine --dry-run -o -yaml > deploy.yaml $ kubectl run nginx --image=nginx:alpine --dry-run -o -yaml > deploy.yaml
``` ```
### 容器交互
```bash
$ kubectl run [pod_name] \
--image=busybox --rm -it \
--restart=Never -- sh
```
### 获取帮助 ### 获取帮助
```bash ```bash