Helm包管理器
Helm包管理器
什么是Helm
Helm是Kubernetes的包管理器,用于定义、安装和升级复杂的Kubernetes应用。
核心概念
| 概念 | 说明 |
|---|---|
| Chart | 应用包,包含所有Kubernetes资源定义 |
| Repository | Chart仓库 |
| Release | Chart的运行实例 |
| Values | 配置参数 |
安装Helm
# macOS
brew install helm
# Linux
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
# 验证
helm version
常用命令
# 添加仓库
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
# 搜索Chart
helm search repo nginx
# 查看Chart信息
helm show chart bitnami/nginx
helm show values bitnami/nginx
# 安装Chart
helm install my-nginx bitnami/nginx
# 查看Release
helm list
helm status my-nginx
# 升级Release
helm upgrade my-nginx bitnami/nginx --set service.type=NodePort
# 卸载Release
helm uninstall my-nginx
创建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
└── .helmignore
Chart.yaml
apiVersion: v2
name: mychart
description: My Helm chart
type: application
version: 0.1.0
appVersion: "1.0.0"
dependencies:
- name: redis
version: "17.x"
repository: "https://charts.bitnami.com/bitnami"
values.yaml
replicaCount: 3
image:
repository: myapp
pullPolicy: IfNotPresent
tag: "latest"
service:
type: ClusterIP
port: 80
ingress:
enabled: true
className: nginx
hosts:
- host: myapp.example.com
paths:
- path: /
pathType: Prefix
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 100m
memory: 128Mi
模板语法
# 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 }}
{{- if .Values.resources }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- end }}
实践:部署应用
# 1. 创建Chart
helm create webapp
# 2. 修改配置
# 编辑 values.yaml 和模板文件
# 3. 打包Chart
helm package webapp
# 4. 安装
helm install my-webapp ./webapp \
--set replicaCount=5 \
--set image.tag=v2.0.0
# 5. 验证
kubectl get pods -l app.kubernetes.io/name=webapp
helm status my-webapp
Chart仓库管理
# 本地仓库
helm repo add local http://localhost:8080
# 推送Chart
cm-push mychart-0.1.0.tgz local
# 搜索Chart
helm search repo mychart
总结
Helm是Kubernetes应用管理的标准工具。通过Chart和Values,可以实现应用的可复用、可配置部署。