This repository is a comprehensive guide to setting up and managing a DevOps environment using various tools and technologies. It covers the entire process of automating infrastructure using Vagrant, managing server configurations with Ansible, containerizing applications using Docker, and orchestrating containers with Docker Swarm.
-
Install Vagrant and VirtualBox
-
Create and Initialize the Vagrantfile
- Create a
Vagrantfile
in your project directory. - Ensure that the static IP addresses assigned to the VMs are within the subnet of the VirtualBox network interface.
- Create a
-
Start the VMs
- Run
vagrant up
in your CLI to bring up the VMs. This process might take some time as it involves downloading and setting up the box images. - Use
vagrant halt
to turn off the VMs gracefully. - Use
vagrant destroy
to remove the VMs.
- Run
-
SSH into the Control VM
- Run
vagrant ssh control
to establish an SSH connection to the control VM. The default password isvagrant
.
- Run
-
File Sharing
- Files in the
/vagrant
directory inside the VM are shared with the host machine (project directory).
- Files in the
-
Configure /etc/hosts
- Add the node hostnames and IP addresses to the
/etc/hosts
file to enable communication between the nodes. Copy the file using:sudo cp /vagrant/hosts /etc/hosts
- Test the connectivity by pinging the other nodes.
- Add the node hostnames and IP addresses to the
-
Setup SSH Key Authentication
- Run
ssh-keygen
to generate SSH key pairs. - Distribute the keys using:
ssh-copy-id node1 ssh-copy-id node2 ssh-copy-id node3
- Login to the nodes using
ssh vagrant@node1
,ssh vagrant@node2
, orssh vagrant@node3
.
- Run
-
Ansible Setup
- Create an
ansible/
directory with themyhosts
(inventory) andplaybook_docker.yml
(playbook) files. - Install Ansible on the control VM.
- Test Ansible connectivity with:
ansible nodes -i /vagrant/ansible/myhosts -m command -a hostname
- Create an
-
Deploy Docker with Ansible
- Run the playbook to install Docker on the nodes:
ansible-playbook -i /vagrant/ansible/myhosts -K /vagrant/ansible/playbook_docker.yml
- The
BECOME
password isvagrant
.
- Run the playbook to install Docker on the nodes:
-
Verify Docker Installation
- SSH into
node1
and verify Docker installation by running:docker --version
- SSH into
-
Deploy the Flask Web Application
- Create the
app.py
,requirements.txt
,Dockerfile
, anddocker-compose.yml
files in your project directory. - SSH into
node1
and run:docker compose -f /vagrant/docker-compose.yml up -d
- Access the application by navigating to
<node1_ip_address>:5000
in your browser or usingcurl node1:5000
from the control VM.
- Create the
-
Setup Docker Swarm
- Create a
docker-swarm/
directory withswarm.yml
(playbook) andmyhosts
(inventory). - Run the playbook to set up Docker Swarm:
ansible-playbook -i /vagrant/docker-swarm/myhosts -K /vagrant/docker-swarm/swarm.yml
- Create a
-
Deploy the Application in Docker Swarm
- Scale down the existing application:
docker compose down
- Build the Docker image:
docker build -t vagrant-project/flaskwebapp .
- Deploy the stack:
docker stack deploy --compose-file docker-compose.yml myapp
- Scale the service:
docker service scale myapp_web=3
- Test the load balancing by running multiple
curl
commands tonode1:5000
.
- Scale down the existing application:
- Starting VMs:
vagrant up
- SSH into Control VM:
vagrant ssh control
- Stop VMs:
vagrant halt
- Destroy VMs:
vagrant destroy
Contributions are welcome! Please fork the repository, create a new branch for your feature or bugfix, and submit a pull request.
For any inquiries or questions, feel free to contact me at [email protected] .