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.
| Aspect | Mutable servers | Immutable infrastructure |
|---|---|---|
| Updates | Applied in place | New image, then replace |
| Server lifetime | Months or years | Until the next release |
| Configuration drift | Grows over time | Effectively eliminated |
| Rollback | Undo changes manually | Redeploy the previous image |
| Debugging | Log in and inspect | Logs and metrics are shipped out; servers are disposable |
| Reproducibility | Hard to guarantee | Built 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
- 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.
- Predictable releases. The exact artifact that passed testing is what runs in production.
- Simple rollback. Keep the previous image and redeploy it.
- 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.
- 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.2Infrastructure 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:
- 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.
- Containerize the application and tag images with a version or commit hash, never just
latest. - Stop editing production by hand. Every change goes through the script or the image, then is redeployed.
- Create a golden snapshot after provisioning, and launch new servers from it when you scale or recover.
- 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.