Skip to content

Création d'un Chart HELM

Générer un Chart

Utilisez cette commande pour créer un nouveau chart dans un nouveau répertoire

Terminal window
helm create myhelm

Helm créera un nouveau répertoire dans votre projet appelé myhelm avec la structure ci-dessous.

myhelm/
├── charts
├── Chart.yaml // metadatas du Chart.
├── templates // les manifestes
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── NOTES.txt // Notes affichées à l'installation du Chart
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│   └── test-connection.yaml
└── values.yaml // valeurs des variables des templates

Templates

On retrouve dans le répertoire templates les définitions YAML pour vos services, déploiements et autres objets Kubernetes.

Si vous avez déjà des définitions pour votre application, il vous suffit de remplacer les fichiers YAML générés par les vôtres.

::: note ::: title Note :::

Helm exécute chaque fichier de ce répertoire via le moteur Go template. Helm étend le langage de modèle, en ajoutant un certain nombre de fonctions utilitaires pour écrire des charts. :::

Terminal window
cat myhelm/templates/service.yaml
Terminal window
apiVersion: v1
kind: Service
metadata:
name: {{ template "fullname" . }}
labels:
chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.externalPort }}
targetPort: {{ .Values.service.internalPort }}
protocol: TCP
name: {{ .Values.service.name }}
selector:
app: {{ template "fullname" . }}

Il s’agit d’une définition de service de base utilisant des modèles. Lors du déploiement du graphique, Helm générera une définition qui ressemblera beaucoup plus à un service valide. Nous pouvons effectuer une simulation d’une installation helm et activer le débogage pour inspecter les définitions générées :

Terminal window
helm install myapp --dry-run --debug ./myhelm
Terminal window
apiVersion: v1
kind: Service
metadata:
name: myhelm
labels:
chart: "myhelm-1.0"
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
protocol: TCP
name: nginx
selector:
app: myhelm
.../...

Values

Le fichier Values est un élément clé des charts Helm. Il fournit les valeurs par défaut pour toutes les variables utilisées dans les templates.

Terminal window
cat myhelm/values.yaml
Terminal window
# Default values for myhelm.
replicaCount: 1
image:
repository: nginx
pullPolicy: IfNotPresent
# Overrides the image tag whose default is the chart appVersion.
tag: ""
service:
type: ClusterIP
port: 80
resources: {}
# limits:
# cpu: 100m
# memory: 128Mi
# requests:
# cpu: 100m
# memory: 128Mi
autoscaling:
enabled: false
minReplicas: 1
maxReplicas: 100
nodeSelector: {}
tolerations: []
affinity: {}

::: note ::: title Note :::

On peut aussi surcharger ces variables à l’installation du chart par l’option —set :::

Terminal window
helm install myapp --dry-run ./myhelm --set service.internalPort=8080
Terminal window
NAME: myapp
LAST DEPLOYED: Wed Apr 6 06:27:29 2022
NAMESPACE: development
STATUS: pending-install
REVISION: 1
HOOKS:
---
# Source: myhelm/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: myapp-myhelm
spec:
type: ClusterIP
ports:
- port: 80
targetPort: http
protocol: TCP
name: http
selector:
app.kubernetes.io/name: myhelm
---
# Source: myhelm/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-myhelm
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: myhelm
.../...
NOTES:
1. Get the application URL by running these commands:
export POD_NAME=$(kubectl get pods --namespace development -l "app.kubernetes.io/name=myhelm,app.kubernetes.io/instance=myapp" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace development $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace development port-forward $POD_NAME 8080:$CONTAINER_PORT

Documentation

Un autre fichier utile dans le répertoire templates/ est le fichier NOTES.txt. Il contient une documenation qui sera affichée à la fin de l’installation du chart

::: note ::: title Note :::

Il peut aussi afficher les valeurs des variables des templates. :::

Terminal window
cat myhelm/templates/NOTES.txt
Terminal window
1. Get the application URL by running these commands:
{{- if .Values.ingress.enabled }}
{{- range $host := .Values.ingress.hosts }}
{{- range .paths }}
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }}
{{- end }}
{{- end }}
{{- else if contains "NodePort" .Values.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "myhelm.fullname" . }})
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "myhelm.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT
{{- end }}