Get the FREE Ultimate OpenClaw Setup Guide →
npx machina-cli add skill chaterm/terminal-skills/helm --openclaw
Files (1)
SKILL.md
6.6 KB

Helm 包管理

概述

Helm Chart 开发、仓库管理、版本升级等技能。

基础命令

仓库管理

# 添加仓库
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo add stable https://charts.helm.sh/stable

# 更新仓库
helm repo update

# 列出仓库
helm repo list

# 搜索 Chart
helm search repo nginx
helm search hub nginx               # 搜索 Artifact Hub

# 删除仓库
helm repo remove bitnami

安装与卸载

# 安装 Chart
helm install my-release bitnami/nginx
helm install my-release bitnami/nginx -n namespace --create-namespace

# 指定 values
helm install my-release bitnami/nginx -f values.yaml
helm install my-release bitnami/nginx --set replicaCount=3

# 模拟安装(不实际执行)
helm install my-release bitnami/nginx --dry-run

# 生成模板
helm template my-release bitnami/nginx

# 卸载
helm uninstall my-release
helm uninstall my-release -n namespace

查看与管理

# 列出已安装的 Release
helm list
helm list -A                        # 所有命名空间
helm list -n namespace

# 查看 Release 状态
helm status my-release

# 查看 Release 历史
helm history my-release

# 获取 Release 的 values
helm get values my-release
helm get values my-release --all    # 包含默认值

# 获取 Release 的 manifest
helm get manifest my-release

# 获取 Release 的 notes
helm get notes my-release

升级与回滚

# 升级 Release
helm upgrade my-release bitnami/nginx
helm upgrade my-release bitnami/nginx -f values.yaml
helm upgrade my-release bitnami/nginx --set replicaCount=5

# 安装或升级
helm upgrade --install my-release bitnami/nginx

# 回滚
helm rollback my-release            # 回滚到上一版本
helm rollback my-release 2          # 回滚到指定版本

# 查看历史
helm history my-release

Chart 开发

创建 Chart

# 创建新 Chart
helm create mychart

# Chart 目录结构
mychart/
├── Chart.yaml          # Chart 元数据
├── values.yaml         # 默认配置值
├── charts/             # 依赖 Chart
├── templates/          # 模板文件
│   ├── deployment.yaml
│   ├── service.yaml
│   ├── ingress.yaml
│   ├── _helpers.tpl    # 模板助手
│   ├── NOTES.txt       # 安装说明
│   └── tests/
└── .helmignore

Chart.yaml

apiVersion: v2
name: mychart
description: A Helm chart for my application
type: application
version: 0.1.0
appVersion: "1.0.0"
keywords:
  - app
  - web
maintainers:
  - name: Your Name
    email: your@email.com
dependencies:
  - name: postgresql
    version: "12.x.x"
    repository: https://charts.bitnami.com/bitnami
    condition: postgresql.enabled

values.yaml

replicaCount: 1

image:
  repository: nginx
  tag: "latest"
  pullPolicy: IfNotPresent

service:
  type: ClusterIP
  port: 80

ingress:
  enabled: false
  className: nginx
  hosts:
    - host: example.com
      paths:
        - path: /
          pathType: Prefix

resources:
  limits:
    cpu: 100m
    memory: 128Mi
  requests:
    cpu: 100m
    memory: 128Mi

postgresql:
  enabled: true

模板语法

# templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "mychart.fullname" . }}
  labels:
    {{- include "mychart.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "mychart.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "mychart.selectorLabels" . | nindent 8 }}
    spec:
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        ports:
        - containerPort: 80
        {{- if .Values.resources }}
        resources:
          {{- toYaml .Values.resources | nindent 10 }}
        {{- end }}

模板助手

# templates/_helpers.tpl
{{- define "mychart.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{- define "mychart.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}

{{- define "mychart.labels" -}}
helm.sh/chart: {{ include "mychart.chart" . }}
app.kubernetes.io/name: {{ include "mychart.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

Chart 打包与发布

# 验证 Chart
helm lint mychart/

# 打包 Chart
helm package mychart/

# 生成索引
helm repo index . --url https://example.com/charts

# 推送到 OCI 仓库
helm push mychart-0.1.0.tgz oci://registry.example.com/charts

依赖管理

# 更新依赖
helm dependency update mychart/

# 构建依赖
helm dependency build mychart/

# 列出依赖
helm dependency list mychart/

常见场景

场景 1:多环境部署

# 不同环境的 values 文件
helm upgrade --install my-release ./mychart \
  -f values.yaml \
  -f values-prod.yaml \
  -n production

场景 2:条件渲染

# templates/ingress.yaml
{{- if .Values.ingress.enabled -}}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ include "mychart.fullname" . }}
spec:
  # ...
{{- end }}

场景 3:循环渲染

{{- range .Values.ingress.hosts }}
- host: {{ .host | quote }}
  http:
    paths:
    {{- range .paths }}
    - path: {{ .path }}
      pathType: {{ .pathType }}
      backend:
        service:
          name: {{ include "mychart.fullname" $ }}
          port:
            number: {{ $.Values.service.port }}
    {{- end }}
{{- end }}

场景 4:调试模板

# 渲染模板但不安装
helm template my-release ./mychart -f values.yaml

# 调试特定模板
helm template my-release ./mychart --show-only templates/deployment.yaml

# 使用 --debug 查看详细信息
helm install my-release ./mychart --dry-run --debug

故障排查

问题排查方法
模板渲染错误helm template --debug
安装失败helm status, kubectl describe
升级失败helm history, helm rollback
依赖问题helm dependency update
# 查看渲染后的 manifest
helm get manifest my-release

# 查看安装 hooks
helm get hooks my-release

# 强制替换资源
helm upgrade my-release ./mychart --force

Source

git clone https://github.com/chaterm/terminal-skills/blob/main/kubernetes/helm/SKILL.mdView on GitHub

Overview

你可以通过 Chart 打包应用及其依赖,托管到仓库,并通过 Release 进行版本化部署。Helm 提供模板化配置、依赖管理和历史记录,显著提升在多环境中的一致性和可重复性。

How This Skill Works

Helm 将应用打包成 Chart,其中包含 Chart.yaml、values.yaml、templates、charts、NOTES.txt 等。你通过 helm install/upgrade 渲染模板并在 Kubernetes 集群中应用资源;Release 会记录安装历史、并支持回滚、查看历史、获取值和 manifest。

When to Use It

  • 需要在不同环境(开发/测试/生产)复用相同部署定义并覆盖特定配置
  • 希望集中管理应用的配置、依赖与版本,避免逐个 YAML 的重复工作
  • 需要对应用进行幂等的升级、回滚以及历史追踪
  • 在 CI/CD 流程中自动化部署、打包并分发自建 Chart
  • 希望将自定义应用打包成可分发的 Chart,并推送到仓库或 OCI 注册中心

Quick Start

  1. Step 1: 设置仓库并更新索引,例如 helm repo add bitnami https://charts.bitnami.com/bitnami && helm repo update
  2. Step 2: 选择一个 Chart 并安装,例如 helm install my-release bitnami/nginx -n my-namespace --create-namespace
  3. Step 3: 使用 values.yaml 或 --set 覆盖默认配置,例如 -f values.yaml 或 --set replicaCount=3
  4. Step 4: 验证状态和清单,例如 helm status my-release、helm list -A 以及 kubectl get pods

Best Practices

  • 将 Chart 的元数据保持清晰,确保 Chart.yaml 的 version 与 appVersion 一致
  • 在 templates/_helpers.tpl 中编写可复用的模板逻辑与标签,提升 charts 的可维护性
  • 避免将敏感信息写入 values.yaml,考虑使用 helm secrets、SOPS 等外部秘密管理方案
  • 在正式发布前执行 helm lint、helm template --debug 与 --dry-run 的验证
  • 使用 dependencies 在 Chart.yaml 中声明外部依赖、并通过 helm dependency update 管理依赖版本

Example Use Cases

  • 通过 helm upgrade --install 实现幂等部署并在生产环境中平滑滚动更新
  • 将自建应用封装成 Chart,并在私有或公有仓库中分发给团队成员
  • 将一个应用的前端、后端与数据库等组件的 Chart 通过 dependencies 引用,统一管理生命周期
  • 将打好的 Chart 推送到 OCI 仓库,便于跨团队分发与治理
  • 针对多环境(dev/stage/prod)使用不同的 values-*.yaml 文件,确保环境隔离与配置一致性

Frequently Asked Questions

Add this skill to your agents

Related Skills

Sponsor this space

Reach thousands of developers