正在加载,请稍候…

Kubernetes Helm Charts: Templating, Values Management, and GitOps with ArgoCD

Master Helm for Kubernetes deployments. Production-ready chart design, values management across environments, Helmfile orchestration, and GitOps workflow with ArgoCD.

Helm is Kubernetes's package manager. A well-designed chart makes deployments repeatable, configurable, and reversible across environments.

Chart Structure

my-service/
├── Chart.yaml
├── values.yaml              # Defaults
├── values-staging.yaml
├── values-production.yaml
└── templates/
    ├── _helpers.tpl
    ├── deployment.yaml
    ├── service.yaml
    ├── ingress.yaml
    ├── hpa.yaml
    └── NOTES.txt

Production Deployment Template

# templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "my-service.fullname" . }}
  labels:
    {{- include "my-service.labels" . | nindent 4 }}
  annotations:
    # Force restart when config changes
    checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0     # Zero-downtime deployment
  template:
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          livenessProbe:
            httpGet:
              path: /health/live
              port: {{ .Values.service.targetPort }}
            initialDelaySeconds: 30
            periodSeconds: 10
          readinessProbe:
            httpGet:
              path: /health/ready
              port: {{ .Values.service.targetPort }}
            initialDelaySeconds: 10
            periodSeconds: 5
          lifecycle:
            preStop:
              exec:
                command: ["/bin/sh", "-c", "sleep 10"]  # Drain connections
      terminationGracePeriodSeconds: 60
      topologySpreadConstraints:
        - maxSkew: 1
          topologyKey: topology.kubernetes.io/zone
          whenUnsatisfiable: DoNotSchedule
          labelSelector:
            matchLabels:
              {{- include "my-service.selectorLabels" . | nindent 14 }}

Values Management

# values.yaml (defaults)
image:
  repository: registry.company.com/my-service
  pullPolicy: IfNotPresent

replicaCount: 2
resources:
  requests: {cpu: 100m, memory: 256Mi}
  limits: {cpu: 500m, memory: 512Mi}

autoscaling:
  enabled: false
  minReplicas: 2
  maxReplicas: 10
  targetCPUUtilizationPercentage: 70

---
# values-production.yaml
replicaCount: 4
resources:
  requests: {cpu: 500m, memory: 512Mi}
  limits: {cpu: 2000m, memory: 2Gi}

autoscaling:
  enabled: true
  minReplicas: 4
  maxReplicas: 20

GitOps with ArgoCD

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: my-service-production
  namespace: argocd
spec:
  project: production
  source:
    repoURL: https://github.com/company/k8s-configs
    targetRevision: main
    path: services/my-service
    helm:
      valueFiles:
        - values.yaml
        - values-production.yaml
  destination:
    server: https://kubernetes.default.svc
    namespace: production
  syncPolicy:
    automated:
      prune: true      # Remove deleted resources
      selfHeal: true   # Revert manual changes
    syncOptions:
      - CreateNamespace=true
    retry:
      limit: 5
      backoff:
        duration: 5s
        factor: 2
        maxDuration: 3m

Helmfile for Multi-Chart Orchestration

# helmfile.yaml
environments:
  staging:
    values: [environments/staging.yaml]
  production:
    values: [environments/production.yaml]

releases:
  - name: my-service
    chart: ./charts/my-service
    values:
      - values/{{ .Environment.Name }}.yaml

  - name: postgresql
    chart: bitnami/postgresql
    version: "12.5.x"
    installed: {{ eq .Environment.Name "staging" }}  # Only in staging

Helm charts are infrastructure as code. Document your values.yaml thoroughly and test with helm lint and helm template.

→ Encode Kubernetes secrets with the Base64 Converter tool.