Helm 全景概览
什么是 Helm
Helm 是 Kubernetes 的包管理器,将一组 K8s 资源打包为 Chart,支持版本管理、参数化配置、依赖管理。
Chart(包)
├── Chart.yaml # 元数据
├── values.yaml # 默认配置
├── templates/ # K8s 资源模板
│ ├── deployment.yaml
│ ├── service.yaml
│ ├── ingress.yaml
│ └── _helpers.tpl # 模板辅助函数
├── charts/ # 依赖 Chart
└── README.md快速开始
bash
# 安装 Helm
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
# 添加仓库
helm repo add stable https://charts.helm.sh/stable
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
# 搜索 Chart
helm search repo nginx
helm search hub wordpress # 搜索 Artifact Hub
# 安装
helm install my-nginx bitnami/nginx \
--namespace production \
--create-namespace \
--set replicaCount=3 \
--set service.type=LoadBalancer
# 查看安装状态
helm list -n production
helm status my-nginx -n production
# 升级
helm upgrade my-nginx bitnami/nginx \
--namespace production \
--set replicaCount=5 \
--reuse-values # 保留之前的 values
# 回滚
helm rollback my-nginx 1 -n production
# 卸载
helm uninstall my-nginx -n production创建 Chart
bash
# 创建 Chart 骨架
helm create my-app
# 目录结构
my-app/
├── Chart.yaml
├── values.yaml
├── templates/
│ ├── deployment.yaml
│ ├── service.yaml
│ ├── ingress.yaml
│ ├── hpa.yaml
│ ├── serviceaccount.yaml
│ ├── NOTES.txt # 安装后显示的说明
│ └── _helpers.tpl
└── .helmignoreChart.yaml
yaml
apiVersion: v2
name: my-app
description: 我的应用 Helm Chart
type: application # application | library
version: 0.1.0 # Chart 版本
appVersion: "1.0.0" # 应用版本
dependencies:
- name: postgresql
version: "13.x.x"
repository: https://charts.bitnami.com/bitnami
condition: postgresql.enabled
- name: redis
version: "18.x.x"
repository: https://charts.bitnami.com/bitnami
condition: redis.enabledvalues.yaml
yaml
replicaCount: 1
image:
repository: my-app
pullPolicy: IfNotPresent
tag: "" # 默认使用 Chart.appVersion
serviceAccount:
create: true
name: ""
service:
type: ClusterIP
port: 80
ingress:
enabled: false
className: nginx
hosts:
- host: my-app.example.com
paths:
- path: /
pathType: Prefix
tls: []
resources:
limits:
cpu: 500m
memory: 512Mi
requests:
cpu: 100m
memory: 128Mi
autoscaling:
enabled: false
minReplicas: 1
maxReplicas: 10
targetCPUUtilizationPercentage: 80
postgresql:
enabled: true
auth:
database: myapp
username: myapp
password: "" # 生产环境通过 --set 传入
config:
logLevel: info
maxConnections: 100模板语法
yaml
# templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "my-app.fullname" . }}
labels:
{{- include "my-app.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
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 | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- containerPort: 8080
env:
- name: LOG_LEVEL
value: {{ .Values.config.logLevel | quote }}
{{- if .Values.postgresql.enabled }}
- name: DB_HOST
value: {{ include "my-app.fullname" . }}-postgresql
{{- end }}
resources:
{{- toYaml .Values.resources | nindent 10 }}常用命令
bash
# 渲染模板(不安装,用于调试)
helm template my-app ./my-app -f custom-values.yaml
# 验证 Chart
helm lint ./my-app
# 打包 Chart
helm package ./my-app
# 推送到 OCI 仓库
helm push my-app-0.1.0.tgz oci://registry.example.com/charts
# 查看 Release 历史
helm history my-nginx -n production
# 获取当前 values
helm get values my-nginx -n production
# 获取渲染后的 manifest
helm get manifest my-nginx -n production