Guide to Creating an Immutable VPS with Docker and Ansible: Building a Self-Healing Server After Attacks
A detailed guide on building an immutable VPS using Docker and Ansible to enhance security and self-recovery after attacks. Includes configuration, testing, and system verification.
Creating an Immutable VPS with Docker and Ansible
In modern DevOps environments, building immutable infrastructure is a core principle to ensure stability, security, and recovery capability. This guide walks through creating an immutable VPS using Docker and Ansible to automatically recover from attacks or system failures.
Introduction to Immutable VPS
An immutable VPS is a system that cannot be modified directly after deployment. All changes are applied by rebuilding the entire system from a tested image. This approach minimizes risks from manual changes, misconfigurations, or network attacks.
In this guide, we use:
- Docker: To package applications and dependencies into isolated containers.
- Ansible: To automate system deployment and configuration management.
- Cloud VPS NVMe: High-performance VPS suitable for Docker-based applications.
Prerequisites and Initial Setup
Before starting, ensure you have:
- A VPS (Cloud VPS NVMe) with Linux OS (Ubuntu 20.04+ or CentOS 8+).
- SSH access with root or sudo user privileges.
- Ansible installed on your local machine (or management server).
- Docker and Docker Compose installed on the VPS.
To install Docker, refer to the guide at NVMe Cloud VPS for Docker, Nginx & Databases.
Configuration Implementation
We will use Ansible to create a playbook that deploys a Docker environment on the VPS. In this example, we deploy a simple application (e.g., Nginx web server).
1. Create Ansible Configuration
Create a project folder and set up the `inventory` file:
[vps]
your_vps_ip ansible_user=root ansible_ssh_private_key_file=/path/to/your/private/key
Create the `playbook.yml` file:
---
- name: Deploy Immutable VPS with Docker
hosts: vps
become: yes
tasks:
- name: Update apt cache
apt:
update_cache: yes
- name: Install Docker
apt:
name: docker.io
state: present
- name: Install Docker Compose
get_url:
url: https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)
dest: /usr/local/bin/docker-compose
mode: '0755'
- name: Start Docker service
systemd:
name: docker
state: started
enabled: yes
- name: Deploy Nginx container
docker_container:
name: nginx
image: nginx:latest
ports:
- "80:80"
state: started
2. Deploy the System
Run the playbook to deploy the system:
ansible-playbook playbook.yml
After the playbook runs successfully, your VPS will have a Docker environment with an Nginx container running.
Self-Healing After Attacks
To ensure the system can self-recover after attacks, we will:
- Set up log monitoring to detect abnormal activity.
- Implement automated VPS snapshot backups on a schedule.
- Configure a reboot script to restart the system if critical failures occur.
For automated backups, you can use a Python script as described in the article Writing a Python Script to Automate Snapshot Backup and Shutdown of Testing VPSs on Weekends to Reduce Cloud Costs by 30%.
Verification and Testing
To verify the system is deployed correctly, perform the following steps:
- Access the VPS via SSH.
- Run the command `docker ps` to check if the Nginx container is running.
- Access the VPS IP address in a browser to verify the default Nginx page loads.
To test self-healing, stop the Docker container or simulate an attack (e.g., use `nmap` to scan ports). Then, re-run the playbook to verify the system recovers automatically.
Troubleshooting
If you encounter deployment issues, check:
- SSH access and private key permissions.
- Compatibility between Ansible and Docker versions.
- File path and access permissions for configuration files.
If the system fails to self-heal, review Docker and Ansible logs to identify the cause.
References
Recommended NVMe Cloud VPS Plans to Deploy This Setup
Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.
VPS-3E
10% VAT included
- CPU: 2 vCPU
- RAM: 4 GB
- SSD: 50 GB NVMe
