首页 抖音热门文章正文

K8S常用命令手册

抖音热门 2025年07月27日 23:46 0 aa

以下是Kubernetes(k8s)中最常用的命令分类整理,涵盖日常运维、资源管理、故障排查等场景,帮助你高效管理集群:

一、基础资源查看命令

# 查看节点信息kubectl get nodeskubectl describe node <节点名称># 查看命名空间kubectl get namespaceskubectl describe ns <命名空间名称># 查看Pod(默认当前命名空间)kubectl get podskubectl get pods -o wide # 显示详细信息(IP、节点等)kubectl get pods -n <命名空间> # 指定命名空间kubectl describe pod <Pod名称> # 查看Pod详细信息# 查看其他资源(Deployment、Service、Ingress等)kubectl get deployments,services,ingresskubectl describe deployment <Deployment名称>
K8S常用命令手册

K8S常用命令手册

二、Pod操作命令

# 创建Pod(基于YAML文件)kubectl apply -f pod.yaml# 删除Podkubectl delete pod <Pod名称>kubectl delete -f pod.yaml# 进入Pod内部(交互式终端)kubectl exec -it <Pod名称> -n namespace  -- /bin/bash   //注意命名空间kubectl exec -it <Pod名称> -c <容器名称> -n namespace -- /bin/sh # 多容器Pod指定容器# 查看Pod日志kubectl logs <Pod名称>kubectl logs -f <Pod名称> # 实时跟踪日志kubectl logs <Pod名称> -c <容器名称> # 多容器Pod指定容器

示例:

K8S常用命令手册

获取pod 容器名称

kubectl get pod tomcat-deployment-6764c49f98-78thd -n tomcat-test -o jsonpath='{.spec.containers[*].name}'

拷贝文件到pod 容器中命令,-c 是容器名称,容器名称获取pod获取

kubectl cp /nfs/default-tomcat-pvc-pvc-12c03e25-993d-4e02-a120-18d3ec02335d/index.jsp tomcat-test/tomcat-deployment-6764c49f98-78thd:/usr/local/tomcat/webapps -c tomcat

示例:宿主机拷贝文件到pod中

K8S常用命令手册

三、部署与伸缩命令

# 创建Deploymentkubectl apply -f deployment.yaml# 扩缩容Pod数量kubectl scale deployment <Deployment名称> --replicas=3# 滚动更新镜像kubectl set image deployment <Deployment名称> <容器名称>=<新镜像>:<标签>kubectl rollout status deployment <Deployment名称> # 查看更新状态kubectl rollout history deployment <Deployment名称> # 查看更新历史kubectl rollout undo deployment <Deployment名称> # 回滚到上一版本

示例:扩容、缩小副本数量

K8S常用命令手册

说明:

正常情况:扩缩容 Pod 数量对 Ingress 访问无影响,Service 会自动适配 Pod 变化。

潜在风险:极端场景下可能有毫秒级延迟或个别请求失败,但通过合理配置(就绪探针、优雅终止时间等)可避免。

核心保障:确保 Service 的标签选择器正确、Ingress 配置指向正确的 Service,即可稳定支持 Pod 的动态扩缩容。

因此,日常扩缩容操作无需担心 Ingress 访问问题,Kubernetes 的自愈机制会处理大部分细节。

四、Service与Ingress命令

# 创建Servicekubectl apply -f service.yaml# 查看Servicekubectl get serviceskubectl describe service <Service名称># 创建Ingresskubectl apply -f ingress.yaml# 查看Ingresskubectl get ingresskubectl describe ingress <Ingress名称>

示例:

K8S常用命令手册

五、配置与存储命令

# 创建ConfigMapkubectl create configmap <名称> --from-file=配置文件路径kubectl apply -f configmap.yaml# 创建Secretkubectl create secret generic <名称> --from-literal=key1=value1kubectl create secret tls <名称> --cert=证书路径 --key=私钥路径# 查看PVC和PVkubectl get pvc,pvkubectl describe pvc <PVC名称>
K8S常用命令手册

六、集群与上下文管理

# 查看当前配置上下文kubectl config viewkubectl config current-context# 切换上下文(集群/用户/命名空间)kubectl config use-context <上下文名称># 查看API资源kubectl api-resources # 查看所有资源类型kubectl explain <资源类型> # 查看资源字段说明

七、故障排查常用命令

# 查看Pod状态及事件kubectl get pods -o widekubectl describe pod <Pod名称> # 查看详细事件# 查看节点资源使用情况kubectl top nodeskubectl top pods# 端口转发(本地访问集群内部服务)kubectl port-forward service/<Service名称> 8080:80 # 本地8080→服务80# 查看系统组件状态kubectl get pods -n kube-systemkubectl logs <组件Pod名称> -n kube-system

八、高级命令技巧

# 标签筛选kubectl get pods -l app=nginx # 筛选标签app=nginx的Pod# 字段筛选kubectl get pods --field-selector=status.phase=Running# 导出资源配置(用于参考或备份)kubectl get deployment <名称> -o yaml > deployment.yaml# 执行一次性命令kubectl run --image=busybox test --restart=Never -- echo "Hello World"# 批量操作kubectl delete pods --all # 删除所有Podkubectl delete deployments,services --all # 删除所有Deployment和Service

九、命名空间命令

# 创建命名空间kubectl create namespace <命名空间名称>kubectl apply -f namespace.yaml# 切换默认命名空间kubectl config set-context --current --namespace=<命名空间名称># 在命令中指定命名空间kubectl get pods -n <命名空间名称>

十、资源配额与限制

# 创建资源配额kubectl apply -f resourcequota.yaml# 创建LimitRange限制单个容器资源kubectl apply -f limitrange.yaml# 查看配额使用情况kubectl get resourcequota

命令补全设置(提高效率)

# bash shellsource <(kubectl completion bash)echo "source <(kubectl completion bash)" >> ~/.bashrc# zsh shellsource <(kubectl completion zsh)echo "source <(kubectl completion zsh)" >> ~/.zshrc

十一、查看pod使用镜像,备份导出导入:

kubectl get pods -n tomcat-test -o widekubectl get pod tomcat-deployment-6764c49f98-78thd -n tomcat-test -o yaml | grep image:ctr -n k8s.io images export tomcat-v9.0.tar docker.io/library/tomcat:9.0   //导出镜像ctr -n k8s.io images import tomcat9.0.tar                                    //到具体节点导入镜像

示例:

K8S常用命令手册

掌握这些命令后,你可以完成Kubernetes集群的日常管理、部署应用、故障排查等核心操作。建议配合官方文档(https://kubernetes.io/docs/reference/kubectl/cheatsheet/)深入学习更多高级用法。

发表评论

长征号 Copyright © 2013-2024 长征号. All Rights Reserved.  sitemap