Skip to content

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!=Running

K8s 资源类型速查

缩写全称说明
popods最小调度单元
svcservices服务发现与负载均衡
deploydeployments无状态应用管理
rsreplicasets副本管理
stsstatefulsets有状态应用管理
dsdaemonsets节点级 Pod
cmconfigmaps配置管理
secretsecrets敏感信息
pvpersistentvolumes持久化存储
pvcpersistentvolumeclaims存储请求
scstorageclasses存储类
nsnamespaces命名空间
nonodes节点
ingingresses七层路由
saserviceaccounts服务账户
hpahorizontalpodautoscalers水平扩缩
pdbpoddisruptionbudgets中断预算
npnetworkpolicies网络策略

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:alpineWeb 服务器
redis:7-alpine缓存
postgres:16-alpine数据库
mysql:8数据库
busybox调试
curlimages/curlHTTP 测试
nicolaka/netshoot网络调试(全套工具)
registry.k8s.io/hpa-exampleHPA 压测