DevOps

Running Magento on Kubernetes: From Local to Production

14 min read·276 words
Running Magento on Kubernetes: From Local to Production

Containerizing Magento is non-trivial — shared file systems, session handling, and cron jobs all need special attention. Here's our production-ready Kubernetes setup.

Kubernetes offers tremendous operational benefits for Magento — automatic scaling, self-healing, rolling deployments, and standardized environments. But Magento has characteristics that make containerization non-trivial: shared media files, Magento's cron daemon, and deploy-time compilation steps. Here's how we handle each.

Container Architecture

  • Web container: Nginx + PHP-FPM, stateless application code
  • CLI container: Same image, used for cron, setup:upgrade, and one-off commands
  • Redis: Deployed via helm chart or managed service (ElastiCache/Memorystore)
  • MySQL: Managed RDS or Cloud SQL — never run stateful databases in Kubernetes
  • Shared media: EFS (AWS) or Filestore (GCP) mounted as PersistentVolume

Handling Shared Media Files

The biggest K8s challenge for Magento: pub/media is written by one pod and must be readable by all other pods. Solution: mount a shared NFS-backed PersistentVolume to all web pods at the media path. On AWS, use EFS with the efs-csi-driver. Performance tip: offload media serving entirely to S3+CloudFront and use a Magento plugin to write uploads directly to S3.

Magento Cron in Kubernetes

Run Magento cron as a Kubernetes CronJob. Create a dedicated CLI container image (same base as web, no Nginx) and schedule the cron job to run every minute. Ensure only one cron instance runs at a time using CronJob's concurrencyPolicy: Forbid. For multi-instance deployments, use a distributed lock (Redis or the database) to prevent duplicate cron execution.

Deploy Process

Our Magento K8s deploy sequence: 1) Build and push the new Docker image, 2) Run a Kubernetes Job to execute setup:upgrade and di:compile, 3) Update the Deployment with the new image tag (triggers rolling update), 4) Wait for rollout to complete, 5) Warm the cache. The rolling update replaces pods one at a time with zero downtime.

Tip

Build Magento static content (setup:static-content:deploy) into the Docker image at build time, not at deploy time. This makes container startup faster and ensures all pods serve identical static assets.

# magento# ecommerce# adobe-commerce# devops# ci/cd# docker# kubernetes# deployment