DevOps & Containers

Immutable Infrastructure: Replace Servers Instead of Patching Them

By NileVPS Team · Published · 4 min read

اقرأ المقال بالعربي

Think about a server that has been running for three years. Packages were upgraded by hand, someone edited a config file during an incident, a library was installed for a script nobody remembers. The server works, but nobody can say exactly how to rebuild it. Immutable infrastructure is the answer to this problem: once a server or container is deployed, you never modify it. When something needs to change, you build a new one from a known definition, deploy it, and throw the old one away.

Mutable vs immutable

In the traditional mutable model, servers are long-lived and updated in place: you SSH in, run apt upgrade, edit /etc/nginx/nginx.conf, and restart services. Configuration management tools can automate this, but the server still accumulates history.

In the immutable model, the unit you deploy is an artifact (a machine image or a container image) built automatically from code. Changing anything means building a new artifact.

AspectMutable serversImmutable infrastructure
UpdatesApplied in placeNew image, then replace
Server lifetimeMonths or yearsUntil the next release
Configuration driftGrows over timeEffectively eliminated
RollbackUndo changes manuallyRedeploy the previous image
DebuggingLog in and inspectLogs and metrics are shipped out; servers are disposable
ReproducibilityHard to guaranteeBuilt from versioned definitions

The common metaphor is "pets vs cattle": pets are named and nursed back to health; cattle are numbered and replaced.

Why teams adopt it

  1. No configuration drift. Every instance built from the same image is identical. Our article on configuration drift explains why drift causes so many hard-to-debug outages.
  2. Predictable releases. The exact artifact that passed testing is what runs in production.
  3. Simple rollback. Keep the previous image and redeploy it.
  4. Better security posture. Servers can run with minimal access; there is less reason to keep SSH open to everyone, and a compromised instance can be destroyed and replaced.
  5. Disaster recovery becomes routine. If you can build a server from code in minutes, recovering from a lost server is the same operation as a normal deploy.

Building blocks

Machine images

Tools such as HashiCorp Packer build VM images from a template: start a base OS, run provisioning scripts or Ansible, then save the result as an image. On a VPS platform, a snapshot of a fully configured server can serve as a simple golden image that you create new servers from.

Container images

Containers are the most common form of immutable infrastructure today. A Dockerfile describes the image, the image gets a version tag, and running containers are never patched; you build and deploy a new tag instead.

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
USER 1000
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]

First-boot configuration

cloud-init applies instance-specific settings (hostname, SSH keys, which image version to run) at first boot. Keep this layer thin; the heavy setup belongs in the image.

#cloud-config
users:
  - name: deploy
    ssh_authorized_keys:
      - ssh-ed25519 AAAA... deploy@ci
runcmd:
  - docker run -d --restart unless-stopped -p 80:8000 registry.example.com/app:1.4.2

Infrastructure as code

Terraform or OpenTofu define which servers, networks and firewall rules exist. Replacing a server becomes a change to a version number in code.

The part that must stay mutable: data

Immutable does not mean stateless. Databases, uploaded files and logs must survive server replacement. Typical approaches:

  • Keep the database on a separate server or managed service, with regular backups.
  • Mount persistent volumes for data directories.
  • Put user uploads in object storage.
  • Ship logs to a central system (for example Loki, Elasticsearch or a syslog server) instead of reading them on the instance.
  • Keep secrets out of images; inject them at runtime through environment variables or a secrets manager.

A practical path on a VPS

You do not need a large cloud platform to benefit:

  1. Script the build. Write a provisioning script or Ansible playbook that turns a fresh OS into a working server. Run it only on new servers.
  2. Containerize the application and tag images with a version or commit hash, never just latest.
  3. Stop editing production by hand. Every change goes through the script or the image, then is redeployed.
  4. Create a golden snapshot after provisioning, and launch new servers from it when you scale or recover.
  5. Practice replacement. Periodically deploy a fresh VPS from your definition, switch traffic to it and delete the old one. If that is painful, something is still undocumented.

Common pitfalls

  • Patching "just this once" in production, which reintroduces drift.
  • Huge images that are slow to build and pull; use slim base images and multi-stage builds.
  • Rebuilding too rarely, so images fall behind on security updates. Rebuild on a schedule.
  • Forgetting that the base image itself needs updates.

FAQ

Does immutable infrastructure mean I can never log in to a server?

You can log in to investigate, but you should not change anything there. Any fix you find gets applied to the image or script, and a new instance replaces the old one.

How do I apply security updates if servers are never patched?

You rebuild the image with the updated packages and roll it out. Many teams rebuild images automatically every week or whenever a base image is updated, so new instances always include the latest patches.

Is immutable infrastructure only for Kubernetes or big companies?

No. A single VPS running versioned Docker images, rebuilt from a script, already follows the core idea. The tooling can grow later as your infrastructure grows.

Related guides