kb.erickguedes.com
Kubernetes: Orquestração em Produção

Helm — Gerenciamento de Pacotes

Aula 6 de 8

O que é Helm?

Helm é o gerenciador de pacotes do Kubernetes. Charts são pacotes pré-configurados.

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

# Comandos básicos
helm version
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
helm search repo nginx
helm search hub nginx  # Artifact Hub

Instalando Charts

# Instalar nginx-ingress
helm install ingress-nginx ingress-nginx/ingress-nginx \
  --namespace ingress \
  --create-namespace \
  --set controller.replicaCount=3

# Instalar PostgreSQL
helm install db bitnami/postgresql \
  --set auth.database=appdb \
  --set auth.password=secret

# Listar releases
helm list
helm list -n ingress

# Atualizar
helm upgrade db bitnami/postgresql --set auth.password=newsecret

# Rollback
helm rollback db 2

# Deletar
helm uninstall db

Values Customizados

# values.yaml
replicaCount: 3

image:
  repository: nginx
  tag: alpine
  pullPolicy: IfNotPresent

service:
  type: ClusterIP
  port: 80

ingress:
  enabled: true
  className: nginx
  hosts:
    - host: app.meudominio.com
      paths:
        - path: /
          pathType: Prefix

resources:
  limits:
    cpu: 500m
    memory: 256Mi
  requests:
    cpu: 100m
    memory: 128Mi

autoscaling:
  enabled: true
  minReplicas: 2
  maxReplicas: 10
  targetCPUUtilizationPercentage: 80
helm install app ./chart -f values.yaml
helm install app ./chart -f values-prod.yaml
helm install app ./chart --set image.tag=v1.2.0

Criando um Chart

helm create meu-chart
# Cria estrutura:
# meu-chart/
# ├── Chart.yaml          # metadados
# ├── values.yaml         # valores padrão
# ├── charts/             # dependências
# └── templates/          # templates Go
#     ├── _helpers.tpl    # helpers
#     ├── deployment.yaml
#     ├── service.yaml
#     ├── ingress.yaml
#     ├── hpa.yaml
#     ├── serviceaccount.yaml
#     └── tests/
# templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "meu-chart.fullname" . }}
  labels:
    {{- include "meu-chart.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "meu-chart.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "meu-chart.selectorLabels" . | nindent 8 }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          ports:
            - containerPort: {{ .Values.service.port }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}

Template Functions

# _helpers.tpl
{{- define "meu-chart.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{- define "meu-chart.labels" -}}
helm.sh/chart: {{ include "meu-chart.name" . }}-{{ .Chart.Version }}
app.kubernetes.io/name: {{ include "meu-chart.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

Dependências

# Chart.yaml
dependencies:
  - name: postgresql
    version: "~15.0"
    repository: https://charts.bitnami.com/bitnami
    condition: postgresql.enabled
  - name: redis
    version: "~19.0"
    repository: https://charts.bitnami.com/bitnami
    condition: redis.enabled
helm dependency update
helm dependency build

Valores por Ambiente

# values-dev.yaml
replicaCount: 1
ingress:
  enabled: false
resources:
  requests:
    cpu: 100m
    memory: 128Mi

# values-prod.yaml
replicaCount: 5
ingress:
  enabled: true
  hosts:
    - host: app.prod.com
autoscaling:
  enabled: true
helm upgrade --install app ./chart -f values-prod.yaml --namespace prod

Helm = pacotes reutilizáveis. Use values por ambiente (dev/prod). Sempre faça helm template para ver o YAML gerado antes de aplicar. Charts no Artifact Hub têm milhares de aplicações prontas.