从入门到精通:Helm Charts 创建初学者指南
简介
Kubernetes 彻底革新了容器编排,Helm Chart 已成为管理 Kubernetes 应用程序的事实上的标准。如果您是 Helm 新手,本指南将指导您从零开始创建自己的 Helm Chart,助您从零开始成为大师!
什么是 Helm Chart?
Helm Chart 是一个预配置的 Kubernetes 资源包。它包含在 Kubernetes 中部署和管理应用程序所需的一切,包括:
- 模板:定义 Kubernetes 资源(例如部署、服务等)的 YAML 清单。
- 值:用于自定义部署的配置选项。
- 元数据:描述 Chart 的文件,例如其名称和版本。
先决条件
- Kubernetes 集群:确保您可以访问正在运行的 Kubernetes 集群。
- 已安装 kubectl:用于与您的集群交互。
- 已安装 Helm:按照官方指南安装 Helm。
步骤 1:创建 Helm Chart
首先使用 helm create 命令创建 Helm Chart:
ninjamac@ip-192-168-1-95 helm % helm create my-application
Creating my-application
这将生成一个名为 my-application 的文件夹,其结构如下:
ninjamac@ip-192-168-1-95 helm % tree -L 3
.
└── my-application├── Chart.yaml├── charts├── templates│ ├── NOTES.txt│ ├── _helpers.tpl│ ├── deployment.yaml│ ├── hpa.yaml│ ├── ingress.yaml│ ├── service.yaml│ ├── serviceaccount.yaml│ └── tests└── values.yaml5 directories, 9 files
步骤2:了解图表结构
Chart.yaml
此文件定义了图表的元数据。
apiVersion: v2
name: my-application
description: A Helm chart for Kubernetes
type: application
version: 0.1.0
appVersion: "1.16.0"
values.yaml
这是图表的默认配置文件。用户可以在部署期间覆盖这些值。
replicaCount: 1image:repository: nginxpullPolicy: IfNotPresenttag: "1.20"
service:type: ClusterIPport: 80
ingress:enabled: falseclassName: ""annotations: {}# kubernetes.io/ingress.class: nginx# kubernetes.io/tls-acme: "true"hosts:- host: example.compaths:
templates/
此文件夹包含使用 Go 模板进行动态配置的 Kubernetes 清单。
步骤 3:自定义部署
编辑 templates/deployment.yaml 文件以定义应用程序的部署资源:
apiVersion: apps/v1
kind: Deployment
metadata:name: {{ .Release.Name }}-deploymentlabels:app: {{ .Chart.Name }}
spec:replicas: {{ .Values.replicaCount }}selector:matchLabels:app: {{ .Chart.Name }}template:metadata:labels:app: {{ .Chart.Name }}spec:containers:- name: {{ .Chart.Name }}image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"ports:- containerPort: {{ .Values.service.port }}
步骤 4:部署 Helm Chart
要将 Chart 部署到 Kubernetes 集群,请使用:
ninjamac@ip-192-168-1-95 helm % helm install my-release ./my-application
NAME: my-release
LAST DEPLOYED: Wed Apr 16 15:51:05 2025
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=my-application,app.kubernetes.io/instance=my-release" -o jsonpath="{.items[0].metadata.name}")export CONTAINER_PORT=$(kubectl get pod --namespace default $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")echo "Visit http://127.0.0.1:8080 to use your application"kubectl --namespace default port-forward $POD_NAME 8080:$CONTAINER_PORT
检查deployment
ninjamac@ip-192-168-1-95 helm % kubectl get pod
NAME READY STATUS RESTARTS AGE
my-release-deployment-78b99bd958-zs79x 1/1 Running 0 71s
覆盖默认值
您可以使用 --set 参数覆盖 values.yaml 文件:helm install my-release ./my-application --set replicaCount=3
ninjamac@ip-192-168-1-95 helm % helm install my-release ./my-application --set replicaCount=3
NAME: my-release
LAST DEPLOYED: Wed Apr 16 16:02:30 2025
NAMESPACE: default
STATUS: deployed
REVISION: 1
步骤 5:测试和调试
查看发布状态
helm status my-release
ninjamac@ip-192-168-1-95 helm % helm status my-release
NAME: my-release
LAST DEPLOYED: Wed Apr 16 16:02:30 2025
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
dry-run
使用 --dry-run 参数模拟部署:
helm install my-release ./my-application --dry-run
ninjamac@ip-192-168-1-95 helm % helm install my-release ./my-application --dry-run
NAME: my-release
LAST DEPLOYED: Wed Apr 16 16:07:06 2025
NAMESPACE: default
STATUS: pending-install
REVISION: 1
HOOKS:
---
# Source: my-application/templates/tests/test-connection.yaml
apiVersion: v1
kind: Pod
metadata:name: "my-release-my-application-test-connection"labels:helm.sh/chart: my-application-0.1.0app.kubernetes.io/name: my-applicationapp.kubernetes.io/instance: my-releaseapp.kubernetes.io/version: "1.16.0"app.kubernetes.io/managed-by: Helmannotations:"helm.sh/hook": test
spec:containers:- name: wgetimage: busyboxcommand: ['wget']args: ['my-release-my-application:80']restartPolicy: Never
MANIFEST:
---
# Source: my-application/templates/serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:name: my-release-my-applicationlabels:helm.sh/chart: my-application-0.1.0app.kubernetes.io/name: my-applicationapp.kubernetes.io/instance: my-releaseapp.kubernetes.io/version: "1.16.0"app.kubernetes.io/managed-by: Helm
automountServiceAccountToken: true
---
# Source: my-application/templates/service.yaml
apiVersion: v1
kind: Service
metadata:name: my-release-my-applicationlabels:helm.sh/chart: my-application-0.1.0app.kubernetes.io/name: my-applicationapp.kubernetes.io/instance: my-releaseapp.kubernetes.io/version: "1.16.0"app.kubernetes.io/managed-by: Helm
spec:type: ClusterIPports:- port: 80targetPort: httpprotocol: TCPname: httpselector:app.kubernetes.io/name: my-applicationapp.kubernetes.io/instance: my-release
---
# Source: my-application/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: my-release-deploymentlabels:app: my-application
spec:replicas: 1selector:matchLabels:app: my-applicationtemplate:metadata:labels:app: my-applicationspec:containers:- name: my-applicationimage: "nginx:1.20"imagePullPolicy: IfNotPresentports:- name: httpcontainerPort: 80protocol: TCP
升级发布版本
更新 chart 并应用更改:
helm upgrade my-release ./my-application
ninjamac@ip-192-168-1-95 helm % helm upgrade my-release ./my-application
Release "my-release" has been upgraded. Happy Helming!
NAME: my-release
LAST DEPLOYED: Wed Apr 16 16:08:40 2025
NAMESPACE: default
STATUS: deployed
REVISION: 2
回滚到上一个版本
helm rollback my-release ./my-application
ninjamac@ip-192-168-1-95 helm % helm rollback my-release
Rollback was a success! Happy Helming!
步骤 6:打包并共享
打包 Chart
要将您的 Chart 打包为 .tgz 文件,请执行以下操作:
helm package ./my-application
ninjamac@ip-192-168-1-95 helm % helm package ./my-application
Successfully packaged chart and saved it to: /Users/ninjamac/Desktop/Rock/helm/my-application-0.1.0.tgz
ninjamac@ip-192-168-1-95 helm % ls -al
total 8
drwxr-xr-x 4 ninjamac staff 128 4 16 16:12 .
drwxr-xr-x@ 75 ninjamac staff 2400 4 16 15:15 ..
drwxr-xr-x 7 ninjamac staff 224 4 16 15:32 my-application
-rw-r--r-- 1 ninjamac staff 3811 4 16 16:12 my-application-0.1.0.tgz
托管您的 Chart
您可以将 Chart 托管在 Helm 仓库(例如 GitHub Pages 或 Artifact Hub)上。
Helm CLI
# General Commands
helm help # Display help for Helm
helm version # Show the Helm version information# Chart Management
helm create <name> # Create a new Helm chart
helm lint <chart> # Run a linter to validate a chart
helm dependency update # Update dependencies for a chart
helm dependency build # Rebuild dependencies for a chart
helm dependency list # List dependencies of a chart# Repository Management
helm repo add <name> <url> # Add a Helm repository
helm repo list # List all Helm repositories
helm repo update # Update Helm repositories
helm repo remove <name> # Remove a Helm repository
helm search hub <keyword> # Search charts on Artifact Hub
helm search repo <keyword> # Search charts in Helm repositories# Release Management
helm install <name> <chart> # Install a Helm chart
helm upgrade <release> <chart> # Upgrade a release
helm rollback <release> <revision> # Roll back a release to a previous revision
helm uninstall <release> # Uninstall a release
helm list # List all Helm releases
helm status <release> # Show the status of a release
helm history <release> # View the history of a release# Template Rendering
helm template <name> <chart> [flags] # Render chart templates locally
helm get values <release> # Get the values of a release
helm get manifest <release> # Get the manifest of a release
helm get notes <release> # Get the notes of a release# Debugging and Testing
helm test <release> [flags] # Run tests for a release
helm install --dry-run <name> <chart> # Simulate an install without executing
helm upgrade --dry-run <release> <chart> # Simulate an upgrade without executing
helm get hooks <release> # Get hooks of a release# Chart Packaging and Publishing
helm package <chart> # Package a chart into a .tgz file
helm push <chart.tgz> <repository> # Push a packaged chart to a repository (Helm plugin required)
helm show chart <chart> # Show detailed information about a chart
helm show values <chart> # Show default values for a chart
helm show all <chart> # Show all information about a chart# Cleanup
helm delete <release> # Alias for `uninstall`
helm repo cleanup # Remove old cache files from repositories# Plugin Management
helm plugin install <url> # Install a Helm plugin
helm plugin list # List installed Helm plugins
helm plugin update <name> # Update a Helm plugin
helm plugin uninstall <name> # Remove a Helm plugin