Helm_charts
Helm Charts
Section titled “Helm Charts”Overview
Section titled “Overview”Helm is the Kubernetes package manager that helps define, install, and upgrade complex Kubernetes applications. Charts are packages of pre-configured Kubernetes resources.
Why Helm?
Section titled “Why Helm?”- Package management: Bundle Kubernetes manifests into a single deployable unit
- Templating: Use templates with configurable values
- Versioning: Track release history and rollback
- Reusability: Share charts via repositories
- Simplified deployment: One command to install entire applications
Helm Architecture
Section titled “Helm Architecture”┌─────────────────────────────────────────────────────────────────┐│ Helm Architecture ││ ││ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ││ │ helm CLI │────▶│ Tiller │────▶│ K8s API │ ││ │ (Client) │ │ (Server) │ │ Server │ ││ └──────────────┘ └──────────────┘ └──────────────┘ ││ ││ ┌──────────────────────────────────────────────────────────┐ ││ │ Chart Structure │ ││ │ mychart/ │ ││ │ ├── Chart.yaml # Chart metadata │ ││ │ ├── values.yaml # Default config values │ ││ │ ├── values.schema.json # Values validation schema │ ││ │ ├── templates/ # Kubernetes manifests │ ││ │ │ ├── deployment.yaml │ │ ││ │ │ ├── service.yaml │ │ ││ │ │ └── _helpers.tpl │ # Template helpers │ ││ │ └── charts/ # Chart dependencies │ ││ └──────────────────────────────────────────────────────────┘ │└─────────────────────────────────────────────────────────────────┘Note: Helm 3 removed Tiller and uses a purely client-side architecture.
Installing Helm
Section titled “Installing Helm”# Using scriptcurl -fsSL https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
# Using package managers# macOSbrew install helm
# Linuxsudo apt-get install helm
# Windowschoco install kubernetes-helmVerify installation:
helm versionhelm repo listCreating a Chart
Section titled “Creating a Chart”# Create new charthelm create mychart
# Chart structuremychart/├── Chart.yaml # Chart metadata├── values.yaml # Default values├── values.schema.json # JSON schema for values validation├── charts/ # Dependencies├── templates/ # Manifest templates│ ├── deployment.yaml│ ├── _helpers.tpl # Template helper functions│ ├── service.yaml│ ├── serviceaccount.yaml│ └── NOTES.txt # Post-install instructions└── tests/ # Test manifests └── test-connection.yamlChart.yaml
Section titled “Chart.yaml”apiVersion: v2name: mychartdescription: A Helm chart for Kubernetestype: applicationversion: 1.0.0appVersion: "1.0.0"kubeVersion: ">=1.20.0"keywords: - myapp - webhome: https://myapp.example.comsources: - https://github.com/example/myappmaintainers: - name: John Doe email: john@example.comdependencies: []annotations: category: WebApplicationvalues.yaml
Section titled “values.yaml”replicaCount: 2
image: repository: myapp/myapp pullPolicy: IfNotPresent tag: "1.0.0"
imagePullSecrets: []nameOverride: ""fullnameOverride: ""
serviceAccount: create: true annotations: {} name: ""
service: type: ClusterIP port: 80
ingress: enabled: true className: nginx annotations: cert-manager.io/cluster-issuer: letsencrypt-prod hosts: - host: myapp.example.com paths: - path: / pathType: Prefix tls: - secretName: myapp-tls hosts: - myapp.example.com
resources: limits: cpu: 500m memory: 512Mi requests: cpu: 250m memory: 256Mi
autoscaling: enabled: true minReplicas: 2 maxReplicas: 10 targetCPUUtilizationPercentage: 70 targetMemoryUtilizationPercentage: 80
nodeSelector: {}
tolerations: []
affinity: {}Template Basics
Section titled “Template Basics”Basic Template
Section titled “Basic Template”apiVersion: apps/v1kind: Deploymentmetadata: name: {{ include "mychart.fullname" . }} labels: {{- include "mychart.labels" . | nindent 4 }}spec: replicas: {{ .Values.replicaCount }} selector: matchLabels: {{- include "mychart.selectorLabels" . | nindent 6 }} template: metadata: {{- with .Values.podAnnotations }} annotations: {{- toYaml . | nindent 8 }} {{- end }} labels: {{- include "mychart.selectorLabels" . | nindent 8 }} spec: {{- with .Values.imagePullSecrets }} imagePullSecrets: {{- toYaml . | nindent 8 }} {{- end }} serviceAccountName: {{ include "mychart.serviceAccountName" . }} containers: - name: {{ .Chart.Name }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" imagePullPolicy: {{ .Values.image.pullPolicy }} ports: - name: http containerPort: {{ .Values.service.port }} protocol: TCP livenessProbe: httpGet: path: / port: http readinessProbe: httpGet: path: / port: http resources: {{- toYaml .Values.resources | nindent 12 }}Helper Functions (_helpers.tpl)
Section titled “Helper Functions (_helpers.tpl)”{{/*Expand the name of the chart.*/}}{{- define "mychart.name" -}}{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}{{- end }}
{{/*Create a default fully qualified app name.*/}}{{- 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 }}
{{/*Create chart name and version as used by the chart label.*/}}{{- define "mychart.chart" -}}{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}{{- end }}
{{/*Common labels*/}}{{- define "mychart.labels" -}}helm.sh/chart: {{ include "mychart.chart" . }}{{ include "mychart.selectorLabels" . }}{{- if .Chart.AppVersion }}app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}{{- end }}app.kubernetes.io/managed-by: {{ .Release.Service }}{{- end }}
{{/*Selector labels*/}}{{- define "mychart.selectorLabels" -}}app.kubernetes.io/name: {{ include "mychart.name" . }}app.kubernetes.io/instance: {{ .Release.Name }}{{- end }}
{{/*Create the name of the service account to use*/}}{{- define "mychart.serviceAccountName" -}}{{- if .Values.serviceAccount.create }}{{- include "mychart.fullname" . }}{{- else }}{{- default "default" .Values.serviceAccount.name }}{{- end }}{{- end }}Installing Charts
Section titled “Installing Charts”# Install chart from current directoryhelm install myrelease ./mychart
# Install with custom valueshelm install myrelease ./mychart --set replicaCount=3
# Install with values filehelm install myrelease ./mychart -f myvalues.yaml
# Dry run (see what would be installed)helm install myrelease ./mychart --dry-run
# Install with all values shownhelm install myrelease ./mychart --dry-run --debugManaging Releases
Section titled “Managing Releases”# List releaseshelm listhelm list --all
# Upgrade releasehelm upgrade myrelease ./mychart
# Upgrade with new valueshelm upgrade myrelease ./mychart --set replicaCount=5
# Rollback to previous releasehelm rollback myrelease
# Rollback to specific revisionhelm rollback myrelease 2
# Uninstall releasehelm uninstall myrelease
# Get release statushelm status myrelease
# Get release historyhelm history myreleaseUsing Repositories
Section titled “Using Repositories”# Add repositoryhelm repo add bitnami https://charts.bitnami.com/bitnami
# Update repositoryhelm repo update
# Search chartshelm search repo nginxhelm search repo bitnami
# Install from repositoryhelm install mynginx bitnami/nginx
# Remove repositoryhelm repo remove bitnamiWorking with Dependencies
Section titled “Working with Dependencies”Chart.yaml with dependencies
Section titled “Chart.yaml with dependencies”apiVersion: v2name: mychartversion: 1.0.0dependencies: - name: postgresql version: "11.x.x" repository: "https://charts.bitnami.com/bitnami" condition: postgresql.enabled - name: redis version: "17.x.x" repository: "https://charts.bitnami.com/bitnami" condition: redis.enabledvalues.yaml with dependency config
Section titled “values.yaml with dependency config”postgresql: enabled: true auth: username: myapp password: secretpassword database: myappdb
redis: enabled: true auth: password: redispasswordThen run:
# Download dependencieshelm dependency build ./mychart
# Update dependencieshelm dependency update ./mychartTemplate Command
Section titled “Template Command”# Render templates locallyhelm template myrelease ./mychart
# Render with valueshelm template myrelease ./mychart --set replicaCount=5
# Render with namespacehelm template myrelease ./mychart --namespace mynamespaceHelm Testing
Section titled “Helm Testing”apiVersion: v1kind: Podmetadata: name: "{{ include "mychart.fullname" . }}-test-connection" labels: {{- include "mychart.labels" . | nindent 4 }} annotations: "helm.sh/hook": testspec: containers: - name: wget image: busybox command: ['wget'] args: ['{{ include "mychart.fullname" . }}:{{ .Values.service.port }}'] restartPolicy: NeverRun tests:
# Run testshelm test myrelease
# Run tests with verbose outputhelm test myrelease --logsHelm Hooks
Section titled “Helm Hooks”metadata: annotations: "helm.sh/hook": pre-install,pre-upgrade "helm.sh/hook-weight": "1" "helm.sh/hook-delete-policy": before-hook-creation,hook-succeededAvailable hooks:
pre-install: Run before any resources are installedpost-install: Run after all resources are installedpre-upgrade: Run before upgradepost-upgrade: Run after upgradepre-rollback: Run before rollbackpost-rollback: Run after rollbackpre-uninstall: Run before uninstallpost-uninstall: Run after uninstall
Best Practices
Section titled “Best Practices”- Use semantic versioning: Follow semver for chart versions
- Validate values: Use values.schema.json
- Template everything: Avoid duplication with templates
- Use _helpers.tpl: Share common template logic
- Write tests: Include Helm tests for validation
- Document values: Add comments in values.yaml
- Version control: Keep charts in Git
- Use CI/CD: Automate testing and publishing
Summary
Section titled “Summary”Helm is essential for:
- Package management: Distribute Kubernetes applications
- Version control: Track and rollback releases
- Templating: Customize deployments
- Reusability: Share and reuse charts
- Production-ready: Handle complex deployments
Key concepts:
- Chart: Package definition
- Release: Deployed instance
- Repository: Chart storage
- Template: Manifest generation
- Values: Configuration
- Hooks: Lifecycle management