Kubernetes常用指令集合

onlysheep
15
2026-08-19

由于最近项目要求在学习Kubernetes,所以将常用的一些指令记录下来,方便自己使用

一、kind 相关命令

查看 kind 版本

kind version

创建 Kubernetes 集群

默认集群:

kind create cluster

指定名称:

kind create cluster --name test-cluster

查看 kind 集群

kind get clusters

删除 kind 集群

kind delete cluster

指定集群:

kind delete cluster --name test-cluster

查看 kind 节点

kind get nodes

多节点集群配置

kind-config.yaml:

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4

nodes:
- role: control-plane
- role: worker
- role: worker

创建:

kind create cluster --name my-cluster --config kind-config.yaml

导入本地 Docker 镜像

kind load docker-image 镜像名

例如:

kind load docker-image my-springboot:v1


二、kubectl 集群管理

查看当前 context

kubectl config current-context

查看所有集群

kubectl config get-contexts

切换集群

kubectl config use-context 集群名称


三、查看 Kubernetes 信息

查看集群信息

kubectl cluster-info

查看节点

kubectl get nodes

简写:

kubectl get no


四、Pod 管理

查看 Pod

当前 namespace:

kubectl get pods

所有 namespace:

kubectl get pods -A

简写:

kubectl get po -A

查看 Pod 详细信息

kubectl describe pod Pod名称

可以查看:

  • Pod 状态

  • 所属节点

  • IP

  • 镜像

  • 容器信息

  • Events 事件

查看 Pod YAML

kubectl get pod Pod名称 -o yaml

查看 Pod 所在节点

kubectl get pods -o wide


五、Pod 日志

查看日志:

kubectl logs Pod名称

实时日志:

kubectl logs -f Pod名称

多容器:

kubectl logs Pod名称 -c 容器名称


六、进入 Pod 容器

bash:

kubectl exec -it Pod名称 -- bash

没有 bash 使用:

kubectl exec -it Pod名称 -- sh


七、Deployment 部署

创建:

kubectl create deployment nginx --image=nginx

查看:

kubectl get deployment

扩容:

kubectl scale deployment nginx --replicas=3

删除:

kubectl delete deployment nginx

---

八、Service 服务

查看 Service:

kubectl get svc

暴露服务:

kubectl expose deployment nginx \

--port=80 \

--type=NodePort


九、YAML 部署

应用:

kubectl apply -f xxx.yaml

删除:

kubectl delete -f xxx.yaml


十、Namespace

查看:

kubectl get ns

创建:

kubectl create namespace dev

指定 namespace:

kubectl get pods -n dev


十一、常用排错命令

查看事件:

kubectl get events

查看所有资源:

kubectl get all

节点详情:

kubectl describe node


十二、Spring Cloud 项目 Kubernetes 部署流程

    代码

     |

    Git

     |

    Jenkins

     |

    Maven 构建

     |

    Docker 镜像

     |

    Harbor

     |

    kubectl apply

     |

    Kubernetes

     |

    -------------------

     user-service

     order-service

     gateway

     redis

     mysql

    -------------------

部署:

kubectl apply -f user-service.yaml

查看:

kubectl get pods

日志:

kubectl logs -f user-service-xxx

进入:

kubectl exec -it user-service-xxx -- bash

扩容:

kubectl scale deployment user-service --replicas=5


十三、日常最常用组合

kind get clusters

kubectl config get-contexts

kubectl config use-context kind-xxx

kubectl get pods -A

kubectl describe pod xxx -n namespace

kubectl logs -f xxx -n namespace

kubectl exec -it xxx -n namespace -- bash

动物装饰