Chart结构、Helm命令、Repository管理与最佳实践
Helm是Kubernetes的包管理工具,用于简化应用的部署和管理。Helm的核心概念包括:
Helm Client → Helm Chart → Kubernetes API → Kubernetes Cluster
# Windows安装
choco install kubernetes-helm
# macOS安装
brew install helm
# Linux安装
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh
# 验证安装
helm version
# 添加官方Helm仓库
helm repo add stable https://charts.helm.sh/stable
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
# 更新仓库
helm repo update
# 查看已添加的仓库
helm repo list
# 搜索Chart
helm search repo nginx
helm search hub wordpress
一个标准的Helm Chart包含以下文件和目录:
my-chart/
├── Chart.yaml # Chart的元数据
├── values.yaml # 默认配置值
├── templates/ # 模板文件目录
│ ├── deployment.yaml # Deployment模板
│ ├── service.yaml # Service模板
│ ├── ingress.yaml # Ingress模板
│ └── _helpers.tpl # 辅助模板函数
├── charts/ # 依赖的子Chart
└── README.md # Chart的说明文档
apiVersion: v2
name: my-app
version: 0.1.0
description: A sample Helm Chart
appVersion: "1.0.0"
type: application
# 依赖项
dependencies:
- name: nginx
version: "1.21.0"
repository: "https://charts.bitnami.com/bitnami"
condition: nginx.enabled
# 默认配置值
replicaCount: 1
image:
repository: nginx
tag: stable
pullPolicy: IfNotPresent
service:
type: ClusterIP
port: 80
ingress:
enabled: false
hosts:
- host: chart-example.local
paths:
- path: /
pathType: Prefix
resources:
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 100m
memory: 128Mi
nginx:
enabled: true
service:
port: 8080
# templates/deployment.yaml
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:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "my-app.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: 80
protocol: TCP
resources:
{{- toYaml .Values.resources | nindent 12 }}
# 安装Chart
helm install my-release bitnami/nginx
helm install my-release ./my-chart
helm install my-release ./my-chart --values=custom-values.yaml
helm install my-release ./my-chart --set replicaCount=3
# 查看Release
helm list
helm status my-release
# 更新Release
helm upgrade my-release bitnami/nginx
helm upgrade my-release ./my-chart --values=new-values.yaml
# 回滚Release
helm rollback my-release
helm rollback my-release 2 # 回滚到特定版本
# 卸载Release
helm uninstall my-release
# 打包Chart
helm package ./my-chart
# 检查Chart
helm lint ./my-chart
# 模板渲染
helm template ./my-chart
helm template ./my-chart --values=values.yaml
# 依赖管理
helm dependency update ./my-chart
helm dependency build ./my-chart
# 创建新的Chart
helm create my-custom-chart
# 目录结构
ls -la my-custom-chart/
# 编辑Chart文件
# 修改Chart.yaml、values.yaml和模板文件
# 测试Chart
tree my-custom-chart/
helm lint my-custom-chart/
helm template my-custom-chart/
# 安装自定义Chart
helm install my-custom-release ./my-custom-chart/
# 查看安装结果
helm list
kubectl get all
# 添加常用Repository
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo add grafana https://grafana.github.io/helm-charts
# 搜索Chart
helm search repo mysql
helm search repo prometheus
# 查看Chart详情
helm show chart bitnami/mysql
helm show values bitnami/mysql
# 安装Chart
helm install mysql bitnami/mysql --set auth.rootPassword=secret --set auth.database=myapp
# 使用GitHub Pages作为私有Repository
# 1. 创建GitHub仓库
# 2. 克隆仓库到本地
# 3. 创建index.yaml
helm repo index ./my-repo
# 4. 复制打包好的Chart到仓库目录
cp my-chart-0.1.0.tgz ./my-repo/
# 5. 更新index.yaml
helm repo index ./my-repo --merge ./my-repo/index.yaml
# 6. 提交并推送更改
git add .
git commit -m "Add my-chart"
git push
# 7. 添加私有Repository
helm repo add my-repo https://<username>.github.io/my-repo/
helm repo update
# 8. 安装Chart
helm install my-release my-repo/my-chart
可能原因:权限不足、资源不足、配置错误
解决方案:检查RBAC权限、集群资源、Chart配置
可能原因:依赖版本不兼容、依赖Chart不存在
解决方案:更新依赖、指定正确的依赖版本
可能原因:YAML格式错误、配置项不存在
解决方案:验证YAML格式、查看Chart的values.yaml文件
Helm是Kubernetes生态系统中非常重要的工具,通过使用Helm,可以:
在实际应用中,需要根据应用的特性和需求,设计并实现合适的Helm Chart。Helm Chart的设计应该考虑可配置性、可扩展性和可维护性,以便于在不同环境中部署和管理应用。