Migrating a PHP monolith to microservices with Docker and Kubernetes
Replacing a working PHP monolith with a fleet of microservices is rarely one deployment. It is a sequence of changes to request routing, data ownership, background jobs and release controls, each with a different blast radius. Docker and Kubernetes can make that sequence safer, but only when service boundaries follow business capabilities rather than folders in the old codebase. The practical goal is smaller failures and faster releases, not a diagram crowded with containers.
Begin with evidence rather than ambition. Inventory the PHP version, Composer dependencies, Apache or Nginx configuration, cron jobs, sessions, file uploads, scheduled reports and every write into the shared MySQL database. Record which capabilities already have clean contracts and which depend on undocumented tables or local state. That map reveals the seams worth testing and the dependencies that can make an apparently tidy extraction risky.
A strangler approach lets the existing application remain productive while narrow routes move to new services. Each service should own its deployment, dependencies and observable interface; Kubernetes then supplies repeatable scheduling, health checks and scaling rather than becoming the architecture by itself. For Australian teams, placement matters: major public cloud regions in Sydney and Melbourne can support in-country workloads, while the privacy, contractual and latency requirements of each service still need to be assessed.
Many Australian agencies, retailers and service businesses still rely on PHP systems built years ago, often combined with WooCommerce, WordPress or bespoke integrations. They need predictable migration costs and clear commercial ownership, not cloud theatre. Success should be measured through deployment lead time, rollback time, error rates and recovery objectives. This approach moves one capability at a time and keeps a workable PHP platform available until the new path has earned trust.
Choose boundaries around business capabilities
A useful service boundary enacts one business decision with a clear owner, input, output and failure policy. “Invoice” or “notification” may work; “controllers” and “models” usually do not. Avoid extracting a class merely because it has calculations. The service must justify its operational cost with independent scaling, security boundaries, release autonomy or a stable integration contract.
Good early candidates include PDF generation, webhook delivery, scheduled reconciliation, authentication or a narrow catalogue read. Select a capability with modest data ownership and observable outcomes. Keep a thin PHP facade in front while requests are split by account, header or cohort. Compare responses, latency and side effects before moving any customer.
Extracted workflows also benefit from explicit states. Karl’s article on Exploring F# discriminated unions demonstrates how typed states can replace tangled conditionals, even when the production service remains in PHP for the first release. Model transitions, retries and terminal outcomes in the contract so operators can understand why work was accepted, deferred or rejected.
Containerise the application and its dependencies
Docker should package a reproducible runtime, not the team’s workstation. A multi-stage build can compile PHP extensions, install locked Composer dependencies and emit a small runtime image. Pin the PHP patch version and base image, run as a non-root user, and publish by immutable digest. Keep environment-specific credentials outside the image; Kubernetes Secrets and external secret stores are safer than build arguments copied into layers.
Treat sessions, uploads, cron work and the relational database as explicit dependencies. Replace local files with object storage, move sessions to a supported cache, and run queues through separate worker Deployments. A container that starts successfully but cannot reach MySQL is not healthy. Readiness probes should test critical connections, while liveness probes should detect a wedged process rather than create a restart loop during a database outage.
Keep the PHP platform deployable during the transition. A gradual route split lets the team compare old and new responses and roll traffic back without rebuilding artefacts. Store images in a registry close to the workload, apply vulnerability scanning and provenance checks, and retain the previous image digest. This gives the release pipeline time to improve without coupling every PHP change to a cluster-wide redesign.
Define Kubernetes contracts before production traffic
Define Kubernetes resources from the service contract, not from copied Helm templates. A Deployment needs resource requests and limits, a Service, an externally managed route, probes, and a disruption budget. Separate configuration from secrets, restrict service accounts, apply network policies, and use separate namespaces for production and non-production. Autoscaling belongs only after load tests establish sensible CPU, memory and queue-depth signals.
Data ownership is the hardest part of extraction. Use expand-and-contract schema changes: add compatible fields, move reads, switch writes, wait until old code has stopped using them, then remove them. Prefer idempotent events and transactional outbox patterns over distributed transactions. Coordinate caching, session invalidation and webhook retries explicitly; otherwise a clean HTTP boundary can conceal a brittle shared-state dependency.
Build delivery around pull requests, policy checks, signed images and GitOps promotion. Instrument PHP and new services with correlated logs, metrics and OpenTelemetry traces before traffic grows. Define service-level objectives for availability and latency, then connect alerts to runbooks. Karl’s independent work through technical consultancy can support architecture review and cloud-migration planning when internal teams need an outside perspective.
Make migration controls and operational readiness explicit
Treat the first extraction as a controlled change programme rather than a big-bang cutover. The following sequence keeps the monolith reversible while contracts and operational evidence improve.
Service extraction sequence
- Capture baseline endpoint behaviour, dependencies, latency, error rates and accountable owners.
- Route a small customer cohort to the new service while the PHP application retains fallback handling.
- Move reads first, keeping the existing database authoritative until write ownership is proven.
- Introduce events through an outbox, with idempotency keys, retry limits and a dead-letter path.
- Switch writes only after reconciliation, rollback and schema compatibility have been rehearsed.
The second set of gates turns an apparently working deployment into a service the team can operate through an Australian business day and overnight. Privacy obligations also belong here: document where personal information is stored, map access and retention against the Australian Privacy Principles, and rehearse incident response under the OAIC Notifiable Data Breaches scheme.
Production readiness gates
- Run automated tests, dependency scans, image signing and software-bill-of-materials checks in CI.
- Set requests and limits from load tests, and exercise pod disruption and autoscaling behaviour.
- Verify startup, readiness and liveness probes against expected dependency failures.
- Alert on error budgets, saturation, queue age, retries and PHP session or job failures.
- Maintain tested rollback, restore, ownership and escalation procedures for every on-call rotation.
Local operating details can change the plan. Support teams in Brisbane or Perth may share a roster with Sydney or Melbourne, so daylight-saving changes and state public holidays need clear ownership rather than relying on informal goodwill. Contracts should state GST treatment, data-location commitments and renewal dates; teams importing gateways or backup appliances should also include Australian customs clearance and landed costs before choosing hardware over a managed service.
Begin with one reversible production slice
Choose one bounded capability and make it the first production slice. Write a short decision record covering its contract, data owner, failure modes, observability, security review and rollback. Keep the PHP route available until the service has met its objectives under real traffic. This keeps the programme funded by demonstrable progress rather than a large platform programme with no customer-facing result.
Pilot with production-like data and a cohort that can provide fast feedback. Set a go/no-go review around error rate, latency, reconciliation, operational toil and support load. If a gate fails, fix the service or route traffic back; do not weaken the threshold to preserve the schedule. Record the result and use it to select the next capability.
Start this quarter by naming the first capability, its accountable owners and the date for its container image. Then schedule the first staging exercise and reserve a production canary slot. A small, reversible step will reveal more about the architecture than another round of diagrams, and it gives the team a concrete outcome to review.
Karl Katzke