Helm Kubernetes 包管理器教程

Helm 是 Kubernetes 的包管理器,类似于 Ubuntu 的 apt 或 CentOS 的 yum。Helm 使用 Chart(图表)来定义、安装和升级 Kubernetes 应用,将复杂的 K8S 资源配置打包为可复用、可版本化的应用包。本文将介绍 Helm 的安装使用和 Chart 开发。

一、安装 Helm

1.1 脚本安装(推荐)

curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

1.2 手动安装

wget https://get.helm.sh/helm-v3.14.0-linux-amd64.tar.gz
tar -zxvf helm-v3.14.0-linux-amd64.tar.gz
install linux-amd64/helm /usr/local/bin/helm

1.3 验证安装

helm version

确保 kubectl 已配置并可连接到 KubernetesK3S 集群。

二、Chart 仓库管理

2.1 添加仓库

# 添加 Bitnami 仓库(最常用)
helm repo add bitnami https://charts.bitnami.com/bitnami

# 添加 Ingress-Nginx 仓库
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx

# 添加 Prometheus 仓库
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts

# 更新仓库索引
helm repo update

2.2 搜索 Chart

# 从仓库搜索
helm search repo nginx

# 从 Artifact Hub 搜索
helm search hub wordpress

2.3 管理仓库

# 列出所有仓库
helm repo list

# 移除仓库
helm repo remove bitnami

三、安装 Chart

3.1 基础安装

# 安装 Nginx
helm install my-nginx bitnami/nginx

# 指定命名空间
helm install my-nginx bitnami/nginx -n web --create-namespace

# 安装特定版本
helm install my-nginx bitnami/nginx --version 15.0.0

3.2 自定义参数

# 通过命令行设置参数
helm install my-nginx bitnami/nginx \
  --set replicaCount=3 \
  --set service.type=ClusterIP

# 通过 values 文件设置参数
helm install my-nginx bitnami/nginx -f my-values.yaml

创建 my-values.yaml

replicaCount: 3

service:
  type: ClusterIP
  port: 80

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

ingress:
  enabled: true
  hostname: nginx.yourdomain.com

3.3 查看默认参数

helm show values bitnami/nginx

四、管理已安装的 Release

# 列出所有 Release
helm list
helm list -A  # 所有命名空间

# 查看 Release 状态
helm status my-nginx

# 查看 Release 历史
helm history my-nginx

# 升级 Release
helm upgrade my-nginx bitnami/nginx --set replicaCount=5

# 回滚到上一版本
helm rollback my-nginx 1

# 卸载 Release
helm uninstall my-nginx

五、创建自定义 Chart

5.1 生成 Chart 骨架

helm create my-app

生成的目录结构:

my-app/
├── Chart.yaml          # Chart 元数据
├── values.yaml         # 默认参数值
├── charts/             # 依赖 Chart
├── templates/          # K8S 资源模板
│   ├── deployment.yaml
│   ├── service.yaml
│   ├── ingress.yaml
│   ├── hpa.yaml
│   ├── _helpers.tpl    # 模板辅助函数
│   └── NOTES.txt       # 安装后提示信息
└── .helmignore

5.2 编辑 Chart.yaml

apiVersion: v2
name: my-app
description: 我的应用 Helm Chart
type: application
version: 0.1.0
appVersion: "1.0.0"
maintainers:
  - name: yourname
    email: you@example.com

5.3 编辑 values.yaml

replicaCount: 2

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

service:
  type: ClusterIP
  port: 80

ingress:
  enabled: false
  hostname: ""

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

env:
  - name: NODE_ENV
    value: "production"
  - name: DB_HOST
    value: "mysql-service"

5.4 模板语法示例

templates/deployment.yaml 中使用 Go 模板语法:

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

六、Chart 开发调试

# 语法校验
helm lint my-app/

# 渲染模板(不实际部署)
helm template my-release my-app/

# 模拟安装(dry-run)
helm install my-release my-app/ --dry-run --debug

# 本地安装
helm install my-release my-app/

七、打包与分发

# 打包 Chart
helm package my-app/

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

# 推送到 OCI 仓库(Helm 3.8+)
helm push my-app-0.1.0.tgz oci://registry.yourdomain.com/charts

八、常见问题

Release 安装失败

helm list --pending
helm uninstall failed-release

查看实际渲染的 YAML

helm get manifest my-nginx

Chart 版本冲突

helm repo update
helm search repo bitnami/nginx --versions

总结

Helm 是 Kubernetes 生态中不可或缺的工具,通过 Chart 可以将复杂的应用部署简化为一条命令。掌握 Helm 后,可以更高效地部署 Ingress 控制器RancherArgoCD 等组件。选购搬瓦工 VPS 请参考 全部方案,使用优惠码 NODESEEK2026 享受 6.77% 折扣。

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。