K8s & Docker 速查手册
Docker 速查
bash
# === 镜像 ===
docker images # 列出镜像
docker pull <image>:<tag> # 拉取镜像
docker build -t <name>:<tag> . # 构建镜像
docker rmi <image> # 删除镜像
docker image prune -a # 清理未用镜像
# === 容器 ===
docker run -d -p 8080:80 --name web nginx # 运行
docker ps # 查看运行中
docker ps -a # 查看全部
docker stop <name> # 停止
docker start <name> # 启动
docker rm <name> # 删除
docker rm -f $(docker ps -aq) # 删除所有
# === 调试 ===
docker logs -f <name> # 查看日志
docker exec -it <name> sh # 进入容器
docker inspect <name> # 详细信息
docker stats # 资源使用
# === Compose ===
docker compose up -d # 后台启动
docker compose down # 停止清理
docker compose logs -f <service> # 查看服务日志
docker compose ps # 查看状态
# === 清理 ===
docker system prune -a # 清理所有未用资源
docker system df # 磁盘使用kubectl 速查
bash
# === 查看 ===
kubectl get pods # Pod 列表
kubectl get pods -o wide # 更多信息
kubectl get pods -A # 所有命名空间
kubectl get pods -l app=web # 按标签筛选
kubectl get all # 所有资源
kubectl get pods,svc,deploy # 多种资源
# === 详情 ===
kubectl describe pod <name> # Pod 详情
kubectl describe node <name> # 节点详情
kubectl get pod <name> -o yaml # YAML 输出
kubectl explain pod.spec # API 字段说明
# === 创建/更新 ===
kubectl apply -f <file.yaml> # 创建或更新
kubectl apply -f <directory>/ # 批量应用
kubectl create deployment web --image=nginx # 快速创建
# === 删除 ===
kubectl delete -f <file.yaml> # 按文件删除
kubectl delete pod <name> # 删除 Pod
kubectl delete namespace <ns> # 删除命名空间及全部资源
# === 调试 ===
kubectl logs <pod> # 查看日志
kubectl logs <pod> -f # 持续跟踪
kubectl logs <pod> --previous # 上一次日志
kubectl logs <pod> -c <container> # 指定容器
kubectl exec -it <pod> -- sh # 进入容器
kubectl port-forward <pod> 8080:80 # 端口转发
kubectl top pods # 资源使用
kubectl top nodes # 节点资源
# === 扩缩容 ===
kubectl scale deploy/<name> --replicas=5
kubectl autoscale deploy/<name> --min=2 --max=10 --cpu-percent=50
# === 更新/回滚 ===
kubectl set image deploy/<name> <container>=<image>:<tag>
kubectl rollout status deploy/<name>
kubectl rollout history deploy/<name>
kubectl rollout undo deploy/<name>
# === 上下文 ===
kubectl config current-context # 当前集群
kubectl config get-contexts # 所有上下文
kubectl config use-context <ctx> # 切换集群
kubectl config set-context --current --namespace=<ns> # 默认命名空间
# === 事件和排查 ===
kubectl get events --sort-by=.metadata.creationTimestamp
kubectl get pods -A --field-selector status.phase!=RunningK8s 资源类型速查
| 缩写 | 全称 | 说明 |
|---|---|---|
| po | pods | 最小调度单元 |
| svc | services | 服务发现与负载均衡 |
| deploy | deployments | 无状态应用管理 |
| rs | replicasets | 副本管理 |
| sts | statefulsets | 有状态应用管理 |
| ds | daemonsets | 节点级 Pod |
| cm | configmaps | 配置管理 |
| secret | secrets | 敏感信息 |
| pv | persistentvolumes | 持久化存储 |
| pvc | persistentvolumeclaims | 存储请求 |
| sc | storageclasses | 存储类 |
| ns | namespaces | 命名空间 |
| no | nodes | 节点 |
| ing | ingresses | 七层路由 |
| sa | serviceaccounts | 服务账户 |
| hpa | horizontalpodautoscalers | 水平扩缩 |
| pdb | poddisruptionbudgets | 中断预算 |
| np | networkpolicies | 网络策略 |
Helm 速查
bash
helm repo add <name> <url> # 添加仓库
helm repo update # 更新仓库
helm search repo <keyword> # 搜索 Chart
helm install <release> <chart> # 安装
helm upgrade <release> <chart> # 升级
helm uninstall <release> # 卸载
helm list # 查看安装列表
helm rollback <release> <rev> # 回滚
helm template <release> <chart> # 渲染模板
helm lint <chart-dir> # 检查语法常用镜像
| 镜像 | 用途 |
|---|---|
nginx:alpine | Web 服务器 |
redis:7-alpine | 缓存 |
postgres:16-alpine | 数据库 |
mysql:8 | 数据库 |
busybox | 调试 |
curlimages/curl | HTTP 测试 |
nicolaka/netshoot | 网络调试(全套工具) |
registry.k8s.io/hpa-example | HPA 压测 |
