Back to articles
DevOps & Systems

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.

DPTCloud Engineering4 min read
Guide to Creating an Immutable VPS with Docker and Ansible: Building a Self-Healing Server After Attacks

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:

  1. Access the VPS via SSH.
  2. Run the command `docker ps` to check if the Nginx container is running.
  3. 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

DPTCloud Tier III Infrastructure

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.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478