Kubernetes 操作与命令
核对集群与上下文
kubectl-config · 检查或切换 kubeconfig 中的集群上下文。执行写操作前核对当前上下文,避免误操作其他集群
- 查看当前上下文名称:
bash
kubectl config current-context- 列出已配置的上下文:
bash
kubectl config get-contexts- 查看当前上下文生效的配置;输出可能包含敏感信息,勿直接公开分享:
bash
kubectl config view --minify- 确认目标后切换上下文:
bash
kubectl config use-context {{context-name}}查看资源、事件与日志
kubectl-describe · 查看资源详情和相关事件;Pod 启动失败时先检查 Events 中的调度、拉取镜像和探针信息
查看资源详情和相关事件;Pod 启动失败时先检查 Events 中的调度、拉取镜像和探针信息。
- 查看 Pod 的容器状态和事件:
bash
kubectl describe pod {{pod}} -n {{namespace}}- 查看部署的副本条件及事件:
bash
kubectl describe deployment {{deployment}} -n {{namespace}}- 查看节点资源和调度条件:
bash
kubectl describe node {{node}}kubectl-get · 列出 Kubernetes 资源及简要状态。先确认当前集群与命名空间,避免把其他环境的资源当成目标
列出 Kubernetes 资源及简要状态。先确认当前集群与命名空间,避免把其他环境的资源当成目标。
- 列出指定命名空间的 Pod,并显示节点与 IP:
bash
kubectl get pods -n {{namespace}} -o wide- 查看部署、Pod 和服务的概况:
bash
kubectl get deployments,pods,services -n {{namespace}}- 持续观察 Pod 状态变化:
bash
kubectl get pods -n {{namespace}} --watch- 输出指定 Pod 的完整 YAML,便于核对配置:
bash
kubectl get pod {{pod}} -n {{namespace}} -o yamlkubectl-logs · 读取 Pod 容器的标准输出和错误输出;只能看到日志驱动保留的内容,重启后可尝试上一实例的日志
- 查看指定容器最近 100 行日志:
bash
kubectl logs {{pod}} -n {{namespace}} -c {{container}} --tail=100- 读取上一次崩溃的容器日志:
bash
kubectl logs {{pod}} -n {{namespace}} -c {{container}} --previous --tail=100- 持续跟踪日志,结束时按 Ctrl+C:
bash
kubectl logs {{pod}} -n {{namespace}} -c {{container}} --follow准备仓库与查找 Chart
helm · 管理 Kubernetes Chart 与发布;修改操作前确认 kube-context 和命名空间
- 查看客户端版本:
bash
helm version --short- 查看当前命名空间中的发布:
参见 helm list。
- 查看指定操作的选项:
bash
helm help {{upgrade}}helm repo · 管理本地 Chart 仓库配置与索引;仓库应来自可信来源
- 添加仓库,会修改本地配置:
bash
helm repo add {{team}} {{https://charts.example.com}}- 列出已配置仓库:
bash
helm repo list- 刷新本地索引,会访问已配置仓库:
bash
helm repo update- 移除本地仓库配置,不卸载已有发布:
bash
helm repo remove {{team}}helm search · 查找 Chart;repo 搜索依赖本地索引,可先执行 helm repo update
- 搜索本地配置仓库中的 Chart:
bash
helm search repo {{nginx}}- 显示所有匹配版本:
bash
helm search repo {{team/app}} --versions- 按版本范围筛选:
bash
helm search repo {{team/app}} --version "{{>=1.0.0 <2.0.0}}"- 从公共 Hub 搜索,不代表已审核 Chart 安全性:
bash
helm search hub {{postgresql}}检查与渲染部署清单
helm lint · 检查 Chart 的结构和常见问题;通过 lint 不等于部署一定成功
- 检查本地 Chart:
bash
helm lint {{./chart}}- 使用实际值文件检查:
bash
helm lint {{./chart}} --values {{values.yaml}}- 将警告也作为失败处理:
bash
helm lint {{./chart}} --strict- 同时检查子 Chart:
bash
helm lint {{./chart}} --with-subchartshelm template · 在本地渲染 Chart;结果不包含完整集群校验,输出可能包含机密
- 使用值文件渲染:
bash
helm template {{demo}} {{./chart}} --namespace {{apps}} --values {{values.yaml}}- 包含 Chart 的 CRD 文件:
bash
helm template {{demo}} {{./chart}} --include-crds- 仅查看某个模板:
bash
helm template {{demo}} {{./chart}} --show-only {{templates/deployment.yaml}}- 输出到目录,会写入文件;谨慎存放机密:
bash
helm template {{demo}} {{./chart}} --output-dir {{rendered}}安装与更新发布
helm install · 安装发布会创建集群资源;先用 template 审阅清单并确认目标集群
- 安装指定 Chart 版本并创建命名空间:
bash
helm install {{demo}} {{team/app}} --version {{1.2.3}} --namespace {{apps}} --create-namespace- 使用值文件安装;文件中的机密需要妥善保管:
bash
helm install {{demo}} {{./chart}} --namespace {{apps}} --values {{values.yaml}}- 安装并等待资源就绪,超时不等同于自动回滚:
bash
helm install {{demo}} {{./chart}} --namespace {{apps}} --wait --timeout {{5m}}helm upgrade · 更新发布会改变集群资源;先检查值文件和 Chart 版本
更新发布会改变集群资源;先检查值文件和 Chart 版本。
- 升级到明确指定的 Chart 版本:
bash
helm upgrade {{demo}} {{team/app}} --version {{1.2.4}} --namespace {{apps}} --values {{values.yaml}}- 不存在时安装,可能创建新的资源:
bash
helm upgrade --install {{demo}} {{./chart}} --namespace {{apps}} --create-namespace --values {{values.yaml}}- 升级并等待资源就绪;超时后检查发布状态:
bash
helm upgrade {{demo}} {{./chart}} --namespace {{apps}} --wait --timeout {{5m}}检查发布与回退版本
helm list · 列出集群中的 Helm 发布;命名空间和状态选项决定可见结果
- 列出指定命名空间的发布:
bash
helm list --namespace {{apps}}- 跨命名空间查找发布,需要对应权限:
bash
helm list --all-namespaces- 按发布名正则过滤:
bash
helm list --namespace {{apps}} --filter "{{^demo}}"- 以 JSON 输出结果:
bash
helm list --namespace {{apps}} --output jsonhelm rollback · 回退发布会改变集群资源;先通过 history 确认修订号,不能保证回退外部数据
回退发布会改变集群资源;先通过 history 确认修订号,不能保证回退外部数据。
- 查看可用修订历史:
bash
helm history {{demo}} --namespace {{apps}}- 回退到已确认的修订号,会更新资源:
bash
helm rollback {{demo}} {{2}} --namespace {{apps}}- 回退并等待资源就绪:
bash
helm rollback {{demo}} {{2}} --namespace {{apps}} --wait --timeout {{5m}}helm status · 查看 Helm 发布的状态与资源信息
- 查看命名空间内的发布状态:
bash
helm status {{demo}} --namespace {{apps}}- 查看资源列表:
bash
helm status {{demo}} --namespace {{apps}} --show-resources- 用 JSON 输出便于脚本处理:
bash
helm status {{demo}} --namespace {{apps}} --output jsonkubectl-rollout · 查看和管理工作负载的发布。检查状态和历史是只读操作;撤销发布会变更集群资源
- 等待部署完成,超时后继续排查 Pod 和事件:
bash
kubectl rollout status deployment/{{deployment}} -n {{namespace}} --timeout=60s- 查看部署的修订历史:
bash
kubectl rollout history deployment/{{deployment}} -n {{namespace}}- 确认目标修订后撤销最近一次发布:
bash
kubectl rollout undo deployment/{{deployment}} -n {{namespace}}卸载与清理发布
helm uninstall · 卸载发布会删除相关资源,可能中断服务或影响持久数据;先检查资源与备份
卸载发布会删除相关资源,可能中断服务或影响持久数据;先检查资源与备份。
- 卸载已确认的发布:
bash
helm uninstall {{demo}} --namespace {{apps}}- 卸载资源但保留发布历史;不会保留工作负载:
bash
helm uninstall {{demo}} --namespace {{apps}} --keep-history- 卸载并等待删除完成:
bash
helm uninstall {{demo}} --namespace {{apps}} --wait --timeout {{5m}}示例整理自 tldr-pages,作者为 tldr-pages team and contributors,遵循 CC BY 4.0。
继续阅读
- 知识专题
- 知识专题
- 知识专题