Polski

Tutorial · Kubernetes · Secrets management

Kubernetes in production – Helm, namespaces and RBAC, NetworkPolicy, PDB, autoscaling, observability and GitOps

A cluster that works in a demo and a cluster you can trust with Friday-night sales are separated by a dozen decisions. We package the dumpling-shop API as a Helm chart, isolate teams with namespaces, quotas and RBAC, lock down traffic with network policies, protect availability with a disruption budget, scale pods (HPA, KEDA) and nodes, measure what users feel, and deploy through GitOps with Argo CD. Finally, a checklist and ways to lower the bill.

By · · 18 min read

Kubernetes basics ends with a working application in a cluster. Production starts with questions a demo never asks: who can change a deployment and how do you check it; what happens when an attacker takes over one pod; will the nightly node upgrade take down all replicas at once; how will the cluster cope with a "dumplings at half price" promotion, and what will it cost. This tutorial walks through those decisions using the dumpling-shop API – with manifests you can move into your own repository.

Prerequisites: Kubernetes basics (Deployment, Service, probes, HPA), Docker for developers, access to a cluster (managed: EKS, GKE, AKS – or a local one for practice).

7
security layers – namespace, quota, PSA, RBAC, NetworkPolicy, secrets, image scanning
2
autoscaling levels – pods (HPA, KEDA) and nodes (CA, Karpenter)
0
manual production changes – state from Git, Argo CD self-heal
1
chart, many environments – `values.yaml` + `values-prod.yaml`
Diagram: GitOps: from a pull request to a change in the cluster

Step 1. A Helm chart instead of copied manifests

The same manifests for dev, staging and production differ in a few values: image tag, replica count, resources, monitoring switched on. Helm (version 4 since November 2025) turns them into templates with values.

Chart: metadata, values and helpers

charts/pierogi-api/Chart.yaml
apiVersion: v2
name: pierogi-api
description: Pierogi API (NestJS) – Deployment, Service, HPA, PDB, NetworkPolicy and ServiceMonitor
type: application
version: 0.3.0 # chart version – bump on every chart change
appVersion: "1.0.0" # default image tag
charts/pierogi-api/values.yaml
image:
  repository: ghcr.io/pierogarnia/pierogi-api
  tag: "" # empty = .Chart.AppVersion; in CI set an immutable tag or a digest
  pullPolicy: IfNotPresent

replicaCount: 2 # ignored when autoscaling.enabled

resources:
  requests: { cpu: 100m, memory: 128Mi }
  limits: { memory: 256Mi }

env:
  PORT: "3000"
  REDIS_URL: redis://redis:6379
# name of a Secret with a DATABASE_URL key (created by External Secrets Operator)
databaseSecret: pierogi-api-db

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

podDisruptionBudget:
  enabled: true
  maxUnavailable: 1

networkPolicy:
  enabled: true
  # pods allowed to call the API (the web frontend and the Gateway namespace)
  allowFromPodLabels: { app.kubernetes.io/name: pierogi-web }
  allowFromNamespaceLabels: { kubernetes.io/metadata.name: gateway-system }

metrics:
  serviceMonitor:
    enabled: false # requires the Prometheus Operator CRDs (kube-prometheus-stack)
    interval: 30s
charts/pierogi-api/values-prod.yaml
# helm upgrade --install api ./charts/pierogi-api -n pierogi-prod -f values-prod.yaml
image:
  tag: "1.4.2"
resources:
  requests: { cpu: 250m, memory: 256Mi }
  limits: { memory: 512Mi }
autoscaling:
  minReplicas: 3
  maxReplicas: 20
metrics:
  serviceMonitor:
    enabled: true
charts/pierogi-api/templates/_helpers.tpl
{{- define "pierogi-api.name" -}}
{{- .Chart.Name -}}
{{- end -}}

{{- define "pierogi-api.labels" -}}
app.kubernetes.io/name: {{ include "pierogi-api.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/version: {{ .Values.image.tag | default .Chart.AppVersion | quote }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
helm.sh/chart: {{ printf "%s-%s" .Chart.Name .Chart.Version }}
{{- end -}}

{{- define "pierogi-api.selectorLabels" -}}
app.kubernetes.io/name: {{ include "pierogi-api.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end -}}

Templates: Deployment, Service, HPA and PDB

charts/pierogi-api/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}
  labels: {{- include "pierogi-api.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  strategy:
    rollingUpdate: { maxSurge: 25%, maxUnavailable: 0 }
  selector:
    matchLabels: {{- include "pierogi-api.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels: {{- include "pierogi-api.selectorLabels" . | nindent 8 }}
      annotations:
        # restart pods when the env values change
        checksum/env: {{ toJson .Values.env | sha256sum }}
    spec:
      serviceAccountName: {{ .Release.Name }}
      automountServiceAccountToken: false # the app does not talk to the Kubernetes API
      securityContext:
        runAsNonRoot: true
        runAsUser: 1000
        seccompProfile: { type: RuntimeDefault }
      topologySpreadConstraints: # spread replicas across zones
        - maxSkew: 1
          topologyKey: topology.kubernetes.io/zone
          whenUnsatisfiable: ScheduleAnyway
          labelSelector:
            matchLabels: {{- include "pierogi-api.selectorLabels" . | nindent 14 }}
      containers:
        - name: api
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - { name: http, containerPort: 3000 }
          env:
            {{- range $key, $value := .Values.env }}
            - name: {{ $key }}
              value: {{ $value | quote }}
            {{- end }}
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef: { name: {{ .Values.databaseSecret }}, key: DATABASE_URL }
          startupProbe:
            httpGet: { path: /api/live, port: http }
            periodSeconds: 2
            failureThreshold: 30
          livenessProbe:
            httpGet: { path: /api/live, port: http }
            periodSeconds: 10
          readinessProbe:
            httpGet: { path: /api/health, port: http }
            periodSeconds: 5
          resources: {{- toYaml .Values.resources | nindent 12 }}
          lifecycle:
            preStop:
              sleep: { seconds: 5 }
          securityContext:
            allowPrivilegeEscalation: false
            readOnlyRootFilesystem: true
            capabilities: { drop: ["ALL"] }
charts/pierogi-api/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: {{ .Release.Name }}
  labels: {{- include "pierogi-api.labels" . | nindent 4 }}
spec:
  selector: {{- include "pierogi-api.selectorLabels" . | nindent 4 }}
  ports:
    - { name: http, port: 3000, targetPort: http }
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ .Release.Name }}
  labels: {{- include "pierogi-api.labels" . | nindent 4 }}
automountServiceAccountToken: false
charts/pierogi-api/templates/hpa-pdb.yaml
{{- if .Values.autoscaling.enabled }}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: {{ .Release.Name }}
  labels: {{- include "pierogi-api.labels" . | nindent 4 }}
spec:
  scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: {{ .Release.Name }} }
  minReplicas: {{ .Values.autoscaling.minReplicas }}
  maxReplicas: {{ .Values.autoscaling.maxReplicas }}
  metrics:
    - type: Resource
      resource:
        name: cpu
        target: { type: Utilization, averageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} }
  behavior:
    scaleDown: { stabilizationWindowSeconds: 300 }
{{- end }}
{{- if .Values.podDisruptionBudget.enabled }}
---
# Voluntary disruptions (node drain, cluster upgrade) may take down at most N pods at a time.
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: {{ .Release.Name }}
  labels: {{- include "pierogi-api.labels" . | nindent 4 }}
spec:
  maxUnavailable: {{ .Values.podDisruptionBudget.maxUnavailable }}
  selector:
    matchLabels: {{- include "pierogi-api.selectorLabels" . | nindent 6 }}
{{- end }}

Decisions worth highlighting:

  • An empty image.tag = appVersion, and CI sets an immutable tag (a version or commit SHA). A latest tag makes rollback impossible, because "the previous version" is the same string.
  • checksum/env in the pod annotations – changing a value in env changes the checksum, so the Deployment performs a rolling update. Otherwise new variables would wait for an accidental restart.
  • topologySpreadConstraints spreads replicas across availability zones – a zone failure does not take the API down.
  • automountServiceAccountToken: false – the app does not talk to the Kubernetes API, so it gets no token an attacker could use.
  • No CPU limit, but a memory limit – a CPU limit causes throttling even when cores are idle; memory is not compressible, so its limit protects the node. Set requests based on measurements.

The rest of this article is for members

Create a free account or sign in to read it all. Still ahead:

  • Step 2. Namespaces, quotas, Pod Security and RBAC
  • Step 3. NetworkPolicy: deny by default
  • Step 4. PodDisruptionBudget and availability
  • Step 5. Autoscaling: pods and nodes
  • Step 6. Observability: measure what users feel
  • Step 7. GitOps with Argo CD
  • Costs: where money usually leaks
  • Pre-production checklist
  • Common mistakes
  • Check yourself
  • What next

Create a free account · Sign in