Kubernetes
YAML
Deployment
DevOps
Automation

How to deploy a bunch of yaml files?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Deploying many Kubernetes YAML files is easy to start and easy to get wrong when environments drift or resource dependencies are unclear. A reliable workflow validates manifests, applies them in a controlled way, and verifies rollout health immediately after. The goal is reproducibility, not just making kubectl apply succeed once.

Organize Manifests for Safe Deployment

Before commands, structure files so environments are explicit. A common pattern is base plus overlays:

  • 'k8s/base for shared resources.'
  • 'k8s/overlays/dev, k8s/overlays/staging, k8s/overlays/prod for environment differences.'

This reduces copy-paste and keeps review diffs meaningful. If every environment has separate full YAML copies, drift appears quickly and rollout bugs become harder to trace.

For straightforward folders without kustomize, you can still apply a directory:

bash
kubectl apply -f ./k8s/manifests

However, large teams usually benefit from overlays so changes remain intentional.

Validate Before Apply

Never deploy many files blindly. Add validation first:

bash
kubectl apply --dry-run=client -f ./k8s/manifests
kubectl apply --dry-run=server -f ./k8s/manifests

Client dry run checks syntax and local structure. Server dry run checks API compatibility and admission behavior with the current cluster.

If you use kustomize overlays:

bash
kubectl kustomize ./k8s/overlays/prod | kubectl apply --dry-run=server -f -

This catches many errors before any live changes happen.

Apply with Kustomize for Environment Control

Using kustomize keeps one command path per environment.

bash
kubectl apply -k ./k8s/overlays/prod

Example kustomization.yaml snippet:

yaml
1apiVersion: kustomize.config.k8s.io/v1beta1
2kind: Kustomization
3namespace: payments
4resources:
5  - ../../base
6patches:
7  - target:
8      kind: Deployment
9      name: api
10    patch: |
11      - op: replace
12        path: /spec/replicas
13        value: 4

This approach is cleaner than maintaining separate full deployment files for each environment.

Stage Complex Rollouts

When deploying many YAML files with dependencies, stage rollout in predictable groups:

  1. Namespace and RBAC.
  2. ConfigMaps and Secrets.
  3. Deployments and StatefulSets.
  4. Services and Ingress.

This gives clearer failure boundaries.

bash
1kubectl apply -f ./k8s/00-namespaces
2kubectl apply -f ./k8s/01-rbac
3kubectl apply -f ./k8s/02-config
4kubectl apply -f ./k8s/03-workloads
5kubectl apply -f ./k8s/04-network

If a workload fails because a ConfigMap is missing, staged deployment surfaces that quickly.

Add Context and Namespace Guards

A common production accident is applying to the wrong cluster. Guard scripts should print context and require confirmation.

bash
1#!/usr/bin/env bash
2set -euo pipefail
3
4EXPECTED_CONTEXT="prod-cluster"
5CURRENT_CONTEXT="$(kubectl config current-context)"
6
7if [ "$CURRENT_CONTEXT" != "$EXPECTED_CONTEXT" ]; then
8  echo "Refusing deploy. Current context: $CURRENT_CONTEXT"
9  exit 1
10fi
11
12kubectl apply -k ./k8s/overlays/prod

Add namespace checks too, especially in shared clusters.

Verify After Apply

Apply success does not mean service success. Run rollout and event checks:

bash
kubectl rollout status deploy/api -n payments --timeout=180s
kubectl get pods -n payments
kubectl get events -n payments --sort-by=.lastTimestamp | tail -n 20

For incident readiness, log these outputs in CI artifacts so operators can audit exactly what happened.

Use kubectl diff in CI

Before merge, show intended object-level changes:

bash
kubectl diff -k ./k8s/overlays/staging

kubectl diff gives reviewers concrete expectations and catches surprise replacements, especially around immutable fields.

Common Pitfalls

  • Applying large folders without validating against server-side schema first.
  • Mixing environment values in one directory and causing accidental cross-environment deploys.
  • Running deployment commands on the wrong Kubernetes context.
  • Treating apply completion as success without rollout and event checks.
  • Ignoring dependency order between RBAC, config objects, and workloads.

Summary

  • Structure manifests by base and environment overlays for maintainability.
  • Run both client and server dry-run checks before live apply.
  • Use staged deployment for large sets of dependent resources.
  • Add context and namespace guardrails in deployment scripts.
  • Verify rollout health and capture logs after every deployment.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.