Helm包管理 #
一、Helm概述 #
Helm是Kubernetes的包管理工具,用于简化应用的部署和管理。
1.1 Helm概念 #
text
Helm核心概念
│
├── Chart
│ └── 应用包,包含Kubernetes资源模板
│
├── Release
│ └── Chart的部署实例
│
├── Repository
│ └── Chart仓库
│
└── Values
└── 配置参数
1.2 Helm优势 #
| 优势 | 说明 |
|---|---|
| 标准化部署 | 统一的部署方式 |
| 版本管理 | 支持版本回滚 |
| 配置管理 | 参数化配置 |
| 依赖管理 | 自动处理依赖 |
二、Helm安装 #
2.1 安装Helm #
bash
# macOS
brew install helm
# Linux
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
# Windows
choco install kubernetes-helm
# 验证安装
helm version
2.2 配置仓库 #
bash
# 添加仓库
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
三、Chart结构 #
3.1 目录结构 #
text
mychart/
├── Chart.yaml # Chart元数据
├── values.yaml # 默认配置值
├── charts/ # 依赖Chart
├── templates/ # 模板文件
│ ├── deployment.yaml
│ ├── service.yaml
│ ├── configmap.yaml
│ ├── ingress.yaml
│ └── _helpers.tpl # 模板助手
└── templates/NOTES.txt # 安装说明
3.2 Chart.yaml #
yaml
apiVersion: v2
name: mychart
description: 学习 Kubernetes 集群管理和编排,用于容器运维
type: application
version: 1.0.0
appVersion: "1.0.0"
maintainers:
- name: maintainer
email: maintainer@example.com
dependencies:
- name: redis
version: "16.x"
repository: "https://charts.bitnami.com/bitnami"
3.3 values.yaml #
yaml
replicaCount: 3
image:
repository: nginx
tag: "1.25"
pullPolicy: IfNotPresent
service:
type: ClusterIP
port: 80
resources:
limits:
cpu: 200m
memory: 256Mi
requests:
cpu: 100m
memory: 128Mi
ingress:
enabled: false
className: ""
hosts:
- host: chart-example.local
paths:
- path: /
pathType: ImplementationSpecific
四、模板语法 #
4.1 基本模板 #
yaml
# 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: {{ .Values.service.port }}
4.2 控制结构 #
yaml
# 条件判断
{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ include "mychart.fullname" . }}
{{- end }}
# 循环
{{- range .Values.hosts }}
- host: {{ .host }}
{{- end }}
# with作用域
{{- with .Values.ingress }}
ingress:
enabled: {{ .enabled }}
{{- end }}
4.3 助手模板 #
yaml
# templates/_helpers.tpl
{{- define "mychart.labels" -}}
app.kubernetes.io/name: {{ .Chart.Name }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/version: {{ .Chart.AppVersion }}
{{- end }}
{{- define "mychart.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}
五、Helm命令 #
5.1 安装和升级 #
bash
# 安装Chart
helm install myrelease mychart/
# 使用自定义values
helm install myrelease mychart/ -f values.yaml
# 设置参数
helm install myrelease mychart/ --set replicaCount=5
# 升级Release
helm upgrade myrelease mychart/
# 升级并安装(推荐)
helm upgrade --install myrelease mychart/
5.2 查看和管理 #
bash
# 查看Release列表
helm list
# 查看Release状态
helm status myrelease
# 查看Release历史
helm history myrelease
# 查看Release values
helm get values myrelease
# 查看Release manifest
helm get manifest myrelease
5.3 回滚和删除 #
bash
# 回滚到上一版本
helm rollback myrelease
# 回滚到指定版本
helm rollback myrelease 2
# 删除Release
helm uninstall myrelease
# 保留历史记录删除
helm uninstall myrelease --keep-history
六、Chart开发 #
6.1 创建Chart #
bash
# 创建Chart骨架
helm create mychart
# 验证Chart
helm lint mychart/
# 打包Chart
helm package mychart/
# 测试渲染
helm template myrelease mychart/
6.2 调试Chart #
bash
# 调试渲染
helm template myrelease mychart/ --debug
# 干运行安装
helm install myrelease mychart/ --dry-run --debug
# 验证模板
helm lint mychart/
七、Chart依赖 #
7.1 定义依赖 #
yaml
# Chart.yaml
dependencies:
- name: redis
version: "16.x"
repository: "https://charts.bitnami.com/bitnami"
condition: redis.enabled
tags:
- cache
- name: postgresql
version: "12.x"
repository: "https://charts.bitnami.com/bitnami"
condition: postgresql.enabled
7.2 管理依赖 #
bash
# 下载依赖
helm dependency update mychart/
# 列出依赖
helm dependency list mychart/
# 构建依赖
helm dependency build mychart/
八、仓库管理 #
8.1 创建仓库 #
bash
# 打包Chart
helm package mychart/
# 生成索引
helm repo index ./charts/
# 启动本地仓库
helm serve
8.2 使用仓库 #
bash
# 添加仓库
helm repo add myrepo https://charts.example.com
# 更新仓库
helm repo update
# 搜索Chart
helm search repo mychart
九、最佳实践 #
9.1 Chart设计原则 #
text
Chart设计原则
│
├── 单一职责
│ └── 一个Chart一个应用
│
├── 可配置性
│ └── 使用values.yaml
│
├── 版本控制
│ └── 语义化版本
│
└── 文档完善
└── README和NOTES
9.2 values.yaml规范 #
yaml
# 分层配置
# 默认值 -> 环境values -> 命令行参数
# 合理默认值
replicaCount: 1
# 资源限制
resources:
limits:
cpu: 200m
memory: 256Mi
requests:
cpu: 100m
memory: 128Mi
# 开关控制
ingress:
enabled: false
十、总结 #
10.1 核心要点 #
| 概念 | 说明 |
|---|---|
| Chart | 应用包 |
| Release | 部署实例 |
| Values | 配置参数 |
| Template | 资源模板 |
10.2 下一步 #
掌握了Helm后,让我们学习 Operator模式,了解自定义控制器的开发。
最后更新:2026-03-28