Shashikant shah

Saturday, 1 March 2025

Helm Package Management for Kubernetes.

 Helm :-


Helm is a package manager for Kubernetes that helps you define, install, and upgrade applications using charts (pre-configured application templates).

Helm repositories are about packaging and deploying applications in a reusable and parameterized way (k8s).

Key Features:

  • Simplifies Kubernetes manifest management.
  • Enables version-controlled deployments.
  • Supports configuration management with values.yaml.
  • Allows easy rollback to previous versions.
  • Has a large chart repository (Artifact Hub).
  • Tempalets funcation for Helm | Template Function List
Many Package Manager for k8s:

Package Manager

Best For

Key Feature

Helm

General-purpose package management

Uses charts, templates, and values.yaml

Kustomize

Native YAML overlays (no templates)

Built into kubectl

Carvel

Secure, immutable deployments

Uses ytt, kapp, imgpkg

Flux

GitOps & CI/CD automation

Automates Helm/Kustomize deployment

OLM

Managing Operators

Best for CRD-based apps

 There are two options to create a local repo.

    i) NGINX-Based Repository

    ii) Pythan-Based Helm Chart Repository.

i) NGINX-Based Repository.

# sudo yum install nginx -y

2.Create a directory to serve files.

# mkdir -p /var/www/repo

# cp /path/to/files/* /var/www/repo/

3.Configure NGINX

# vim /etc/nginx/sites-available/repo

server {

    listen 8080;

    server_name localhost;

    root /var/www/repo;

    autoindex on;

}

4.Enable the site & restart NGINX

# sudo ln -s /etc/nginx/sites-available/repo /etc/nginx/sites-enabled/

# sudo systemctl restart nginx

5.Access the repository.

http://localhost:8080


ii) Pythan-Based Helm Chart Repository.

1.Create a directory for Helm charts.

# helm  create  named_template

2.Create a archive package.

# helm package  named_template

3.Create a repo directory and copy to file.

# mkdir -p /var/www/helm

# cp named_template-0.1.0.tgz  /var/www/helm/

4.Generate the Helm repo index.

# helm repo index /var/www/helm --url http://localhost:8080

5.Serve the repository with Python.

# python3 -m http.server 8080 --directory /var/www/helm &

6.Add the repository in Helm.

# helm repo add mylocalrepo http://localhost:8080

# helm repo update

# helm  repo list





 

New package push to local-repo.

1.Archive the Package.

# helm package  range_in_helm




# cp -rvf range_in_helm-0.1.0.tgz /var/www/helm/

# helm repo index /var/www/helm --url http://localhost:8080

# cat /var/www/helm/index.yaml










# helm repo update local_repo

# helm  repo list





Search chart in repo.

# helm search repo mylocalrepo


If any package gets updated in local repo, then how to check for updated charts in repo.

#  helm repo update mylocalrepo

 

add a public Helm repository like Artifact Hub.

# helm repo add bitnami https://charts.bitnami.com/bitnami

# helm repo list




# helm search repo bitnami

# helm install my-nginx bitnami/nginx

 

Configure Artifactory as a Helm Repo:

1.Add Artifactory Helm Repo.

# helm repo add my-artifactory http://localhost:8081/artifactory/helm-repo/

2.Package & Push a Helm Chart.

# helm package my-chart

# curl -u admin:password --upload-file my-chart-0.1.0.tgz http://localhost:8081/artifactory/helm-repo/

3.Search & Pull Charts.

# helm search repo my-artifactory

# helm pull my-artifactory/my-chart


What is a Helm Chart?

A Helm chart is a collection of files organized in a specific directory structure. These files describe the Kubernetes resources (e.g., Deployments, Services, ConfigMaps, etc.) required to run an application. Helm charts also support templating, allowing you to parameterize and customize deployments.

Create a New Helm Chart.

# helm create mychart 

# cd mychart

mychart/

Chart.yaml         # Metadata about the Helm chart

values.yaml        # Default configuration values

values.schema.json # (Optional) Schema validation for values.yaml

charts/            # Directory for dependent charts

templates/         # Contains Kubernetes YAML templates

  |à deployment.yaml  # Template for Deployment

  |à service.yaml     # Template for Service

  |à ingress.yaml     # Template for Ingres

  |à _helpers.tpl     # Template for reusable functions

  |à NOTES.txt        # Instructions displayed after install

  |à hpa.yaml         # (Optional) Horizontal Pod Autoscaler

  |à secrets.yaml     # (Optional) Kubernetes Secrets

  |àconfigmap.yaml   # (Optional) Kubernetes ConfigMap

  |à serviceaccount.yaml # (Optional) Service Account

  |à role.yaml        # (Optional) Role-based access control (RBAC)

  |à rolebinding.yaml # (Optional) Role binding for RBAC

  |à pvc.yaml         # (Optional) Persistent Volume Claim

  |à cronjob.yaml     # (Optional) CronJob resource

  |à job.yaml         # (Optional) Job resource

hooks/              # (Optional) Hooks (pre-install, post-install scripts)

   |à pre-install-job.yaml

   |à post-install-job.yaml

tests/              # (Optional) Chart testing files

   |à test-connection.yaml

LICENSE             # (Optional) License file

README.md           # (Optional) Documentation for the chart


1.helm install:

# curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

# helm version





1. Helm Version & Help

# helm version # Check Helm version

# helm help # Get Helm help

# helm <command> --help # Get help for a specific command

2. Helm Repository Management

# helm repo add <repo_name> <repo_url>    # Add a new Helm repository

# helm repo list                          # List all added repositories

# helm repo update                        # Update repositories to fetch the latest charts

# helm repo remove <repo_name>            # Remove a repository

3. Helm Search for Charts

# helm search hub <keyword> # Search for charts in Helm Hub

# helm search repo <keyword> # Search for charts in added repositories

4. Helm Install & Upgrade

# helm install <release_name> <chart_name>             # Install a Helm chart

# helm install <release_name> <chart_name> --set key=value  # Install with custom values

helm upgrade <release_name> <chart_name>            # Upgrade a release

helm upgrade --install <release_name> <chart_name>  # Upgrade if exists, install if not

5. Helm List & Status

helm list                               # List all Helm releases

helm list -A                            # List releases from all namespaces

helm status <release_name>              # Get status of a specific release

6. Helm Uninstall

helm uninstall <release_name>           # Remove a Helm release

helm uninstall <release_name> --namespace <namespace>  # Remove from specific namespace

7. Helm Rollback

helm rollback <release_name> <revision_number>   # Rollback to a previous version

helm history <release_name>                      # View release history

 8. Helm Chart Management

helm create <chart_name>    # Create a new Helm chart

helm package <chart_path>   # Package a Helm chart

helm lint <chart_path>      # Check for errors in the chart

helm show values <chart>    # Show default values of a chart

9. Helm Template Rendering

helm template <release_name> <chart_name>  # Render a chart into Kubernetes YAML manifests

 10. Helm Dependency Management

helm dependency list <chart_path>     # List dependencies for a chart

helm dependency update <chart_path>   # Update dependencies

 11. Helm Upgrade Strategy

helm upgrade <release_name> <chart_name> --atomic      # Rollback on failure

helm upgrade <release_name> <chart_name> --wait        # Wait for resources to be ready

Command

Description

helm version

Check Helm version

helm repo add <repo> <url>

Add a Helm repository

helm search repo <chart>

Search for charts in repos

helm install <name> <chart>

Install a Helm chart

helm upgrade <name> <chart>

Upgrade a release

helm rollback <name> <rev>

Rollback to a previous version

helm uninstall <name>

Remove a Helm release

helm list

List installed releases

helm status <name>

Check release status

helm history <name>

View release history

helm template <name> <chart>

Render chart templates

helm create <chart>

Create a new chart


1.Chart.yaml (Metadata)

·      Contains metadata about the Helm chart.

·      Defines the name, version, description, and dependencies.

apiVersion: v2

name: mychart

description: A Helm chart for deploying an application

version: 1.0.0

appVersion: 1.0.0

keywords:

  - web

  - backend

maintainers:

  - name: Your Name

    email: your@email.com

2.values.yaml (Variable and Default Configuration).

replicaCount: 2

image:

  repository: nginx

  tag: latest

  pullPolicy: IfNotPresent

service:

  type: ClusterIP

  port: 80

3.templates/ (YAML Manifests with Templating object).

apiVersion: apps/v1

kind: Deployment

metadata:

  name: {{ .Release.Name }}-app

spec:

  replicas: {{ .Values.replicaCount }}

  selector:

    matchLabels:

      app: myapp

  template:

    metadata:

      labels:

        app: myapp

    spec:

      containers:

      - name: myapp

        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"

        ports:

        - containerPort: 80

4.charts/ (Dependencies).

This folder holds any dependent Helm charts that your chart relies on.

You can define dependencies in Chart.yaml and manage them with:

5..helmignore (Ignored Files).

Works like .gitignore, excluding certain files when packaging the chart.

*.md

 *.backup


1.Create a helm chart and set variable dynamic.

# helm create my-first-chart






# ls -ltr my-first-chart/





# cd /root/helm/my-first-chart/

# rm -rf templates/*

# > values.yaml

# kubectl create deploy my-deployment --image=nginx --dry-run=client -o yaml > deployment.yaml

# kubectl create service nodeport my-service --tcp=80:80 --dry-run=client -o yaml > service.yaml

# cat templates/deployment.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  labels:

    app: my-deployment

  name: my-deployment

spec:

  replicas: 1

  selector:

    matchLabels:

      app: my-deployment

  template:

    metadata:

      labels:

        app: my-deployment

    spec:

      containers:

      - image: nginx

        name: nginx

# cat templates/service.yaml

apiVersion: v1

kind: Service

metadata:

  labels:

    app: my-service

  name: my-service

spec:

  ports:

  - name: 80-80

    port: 80

    protocol: TCP

    targetPort: 80

  selector:

    app: my-deployment

  type: NodePort


Install the Helm Chart

#  helm install first-release .

Or

#  helm install myrelease mychart

# helm uninstall myrelease



 



# kubectl get pods,svc



 


run same command Again.

#  helm install first-release .




same name resource is already used.

#  helm install second-release .




Set Dynamic  variable in values.yaml file :  

# values.yaml

deployment:

    name: web-server

    image: nginx

    replica: 2

service:

    name: web-server


# cd /root/helm/my-first-chart/templates

# cat deployment.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  labels:

    app: {{ .Values.deployment.name }}

  name: {{ .Values.deployment.name }}

spec:

  replicas: {{ .Values.deployment.replica }}

  selector:

    matchLabels:

      app: {{ .Values.deployment.name }}

  template:

    metadata:

      labels:

        app: {{ .Values.deployment.name }}

    spec:

      containers:

      - image: {{ .Values.deployment.image }}

        name: {{ .Values.deployment.image }}

 

# cat service.yaml

apiVersion: v1

kind: Service

metadata:

  labels:

    app: {{ .Values.service.name }}

  name: {{ .Values.service.name }}

spec:

  ports:

  - name: 80-80

    port: 80

    protocol: TCP

    targetPort: 80

  selector:

    app: {{ .Values.deployment.name }}

# helm list




# helm install web-release .






# helm list




# kubectl get pods,svc








Uninstall helm chart.

# helm list




# helm uninstall first-release

# helm list



 

Validate syntax

# helm  lint  .





Not found Error in yaml file.









Manifests (object file) execute with Debug without applied.  

# cd /root/helm/my-first-chart

# helm  template  .














--dry-run can check what issues may arise during installation and what actions will be performed.

# helm install my-chart . --dry-run



 










Public repo add from URL.

Below for all helmchart available .

https://artifacthub.io/packages/helm/bitnami/wordpress?modal=install

# helm repo add bitnami https://charts.bitnami.com/bitnami





# helm install my-wordpress bitnami/wordpress --version 24.1.13

How to clone chart repo (download )

# helm pull oci://registry-1.docker.io/bitnamicharts/wordpress --version 24.1.13

Or

# helm  pull --untar  bitnamicharts/wordpress  --version 24.1.13

Custom values add.

# vim custom-values.yaml

replicaCount: 3

# helm  upgrade  wordpress  .  --values  custom-values.yaml

# helm history  

HelmChart  modify and update.

# helm list




# helm upgrade my-chart01  .






# helm history my-chart01




Rollback 1 version will go away.

# helm history my-chart01




# helm rollback my-chart01 1




# helm history my-chart01





Helm  template Functions.

Print(‘Hello world!’)

Len(‘Hello world!’)

Type(‘Hello world!’)

All helm template function:

https://helm.sh/docs/chart_template_guide/function_list/

How to apply function.

# cat values.yaml

name: test

drink: tea

food: pizza

# vim templates/configmap.yaml

apiVersion: v1

kind: ConfigMap

metadata:

  name: {{ .Values.name }}-configmap

data:

  myvalue: "Hello World"

  drink: {{ .Values.drink }}

  food: {{ .Values.food }}

# helm install my-function . --dry-run



 







upper: Convert the entire string to uppercase

 title: first word is capital.

quote: dual quote.

# vim templates/configmap.yaml

apiVersion: v1

kind: ConfigMap

metadata:

  name: {{ .Values.name }}-configmap

data:

  myvalue: "Hello World"

  drink: {{ .Values.drink | upper | quote }}

  food: {{ .Values.food | title | quote }}

# helm install my-function . --dry-run



 







If  Variable is empty in values.yaml file.

How to apply default function values in files.

We can apply function in two ways, one by pipeline | and second by writing default.

# vim values.yaml

name:

drink: tea

food: pizza

# helm install my-function . --dry-run



 







# vim templates/configmap.yaml

apiVersion: v1

kind: ConfigMap

metadata:

  name: {{  default "my-function" .Values.name }}-configmap

data:

  myvalue: "Hello World"

  drink: {{ .Values.drink | upper | quote }}

  food: {{ .Values.food | title | quote }}



 







Required function used.

Parameter is empty and getting Error for “This value is required”.

Values required hai

# vim templates/configmap.yaml

apiVersion: v1

kind: ConfigMap

metadata:

  name: {{ required "This value is required" .Values.name }}-configmap

data:

  myvalue: "Hello World"

  drink: {{ .Values.drink | upper | quote }}

  food: {{ .Values.food | title | quote }}

# helm install my-function . --dry-run

Error: INSTALLATION FAILED: execution error at (my-function/templates/configmap.yaml:4:11): This value is required

 

Helm template If-else.

# vim templates/deployment.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  creationTimestamp: null

  labels:

    app: webapp

  name: webapp

spec:

  replicas: 1

  selector:

    matchLabels:

      app: webapp

  strategy: {}

  template:

    metadata:

      creationTimestamp: null

      labels:

        app: webapp

    spec:

      containers:

      {{- if eq .Values.environment "prod"}}

      - image: webapp:prod

      {{- else if eq .Values.environment "dev" }}

      - image: webapp:dev

      {{- else }}

      - image: webapp:demo

      {{- end }}

        name: webapp

        resources: {}

status: {}

 

1.blank line remove by “-”

2.if condition

prod = prod : true  

dev = dev : true

prod and dev condition false then

webapp:demo :true

# helm install webapp . --dry-run



 

 










# cat values.yaml

environment: prod

# helm install webapp . --dry-run



 






This function is used to create a Resource or not.

false : The resource will not be created.

true : The resource will not be created.

# vim templates/serviceaccount.yaml

{{- if .Values.serviceaccount.create }}

apiVersion: v1

kind: ServiceAccount

metadata:

  name: my-service-account

  namespace: default

{{- end }}

 

# vim values.yaml

environment: prod

serviceaccount:

    create: false

Note : Service account not showing.



 











-With Block in Helm | Advanced Helm



 






Repeated values Remove {{ -  .Values.deployment.replica }}

Add block {{- with .Values.deployment }}    (file me global set karna, bar bar values dena na pade)

root level se search kare:  $.   {{- end }}   

 

{{- with .Values.deployment }}

apiVersion: apps/v1

kind: Deployment

metadata:

  name: {{ .name }}

spec:

  replicas: {{ .replica }}

  selector:

    matchLabels:

      app: {{ .name }}

  template:

    metadata:

      labels:

        app: {{ .name }}

    spec:

      containers:

      {{- with .image }}

      - image: {{ .repository }}:{{ .tag }}

      {{- end }}

        name:  {{ $.Values.container.name }}

      {{- with .ports }}

        ports:

        - containerPort: {{ .port1 }}

        - containerPort: {{ .port2 }}

      {{- end }}

{{- end }}

 

# values.yaml

container:

  name: webapp-container

deployment:

  name: webapp-deployment

  replica: 2

  image:

    repository: nginx

    tag: 1.19

  ports:

    port1: 80

    port2: 8080

# helm install webapp . --dry-run



 









Range in Helm Chart | Use Range Block in Helm

Multiple user defind

# cat values.yaml

users:

  - name: Alice

    role: admin

  - name: Bob

    role: editor

  - name: Charlie

    role: viewer

#####

{{- range .Values.users }} à global range set.

# vim templates/rolebinding.yaml

{{- range .Values.users }}

apiVersion: rbac.authorization.k8s.io/v1

kind: RoleBinding

metadata:

  name: {{ .name }}-role-binding

subjects:

  - kind: User

    name: {{ .name }}

    apiGroup: rbac.authorization.k8s.io

roleRef:

  kind: Role

  name: {{ .role }}

  apiGroup: rbac.authorization.k8s.io

{{- end }}

# helm install range-helm . --dry-run

# helm list --all-namespaces

 


 












Named Templates in Helm Charts.

Templates directory me _helpers.tpl  à ignore kar dega

Include à function 2 parameter ko leta hai.

Indent à  extra space add krne  ke liye.

{{- include "labels" . | indent 2 }}

 

# cat templates/_helpers.tpl

{{- define "labels" }}

    app: nginx

    env: dev

{{- end }}

# cat templates/service.yaml

apiVersion: v1

kind: Service

metadata:

  name: my-service

spec:

  selector:

    {{- template "labels" }}

  ports:

    - protocol: TCP

      port: 80

      targetPort: 80

# cat templates/deployment.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: nginx-deployment

  labels:

    {{- template "labels" }}

spec:

  replicas: 1

  selector:

    matchLabels:

      {{- include "labels" . | indent 2 }}

  template:

    metadata:

      labels:

        {{- include "labels" . | indent 4 }}

    spec:

      containers:

      - name: nginx

        image: nginx:1.14.2

        ports:

        - containerPort: 80



No comments:

Post a Comment