😀 Intro
Hi, in this post, we will use to provision 2 KVM locally and after that, we will Deploy Flask App & PostgreSQL using
🔨 Requirements
I used Ubuntu 22.04 LTS as the OS for this project. If you're using a different OS, please make the necessary adjustments when installing the required dependencies.
The major pre-requisite for this setup is KVM hypervisor. So you need to install KVM in your system. If you use Ubuntu you can follow this step:
sudo apt -y install bridge-utils cpu-checker libvirt-clients libvirt-daemon qemu qemu-kvm
Execute the following command to make sure your processor supports virtualisation capabilities:
$ kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used
Install Terraform
wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform -y
Verify installation:
$ terraform version
Terraform v1.9.8
on linux_amd64
Install Ansible
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible -y
Verify installation:
$ ansible --version
ansible [core 2.15.1]
...
Create KVM
we will use the , add compose file, replace config.py and create new container using docker compose:
---
- name: Clone html project
changed_when: false
ansible.builtin.git:
repo: https://github.com/danielcristho/Flask_TODO.git
dest: /home/ubuntu/Flask_TODO
clone: true
- name: Add Flask Compose
ansible.builtin.template:
src: docker-compose.yml.j2
dest: /home/ubuntu/Flask_TODO/docker-compose.yml
mode: preserve
- name: Update config.py
ansible.builtin.template:
src: config.py.j2
dest: /home/ubuntu/Flask_TODO/flask/app/config.py
mode: preserve
- name: Run docker-compose up -d
shell: docker-compose up -d --build
args:
chdir: /home/ubuntu/Flask_TODO
Our project structure should look like this:
├── ansible-flask-psql
│ ├── ansible.cfg
│ ├── hosts
│ ├── playbook.yml
│ └── roles
│ ├── docker
│ │ └── tasks
│ │ └── main.yml
│ ├── flask
│ │ ├── tasks
│ │ │ └── main.yml
│ │ ├── templates
│ │ │ ├── config.py.j2
│ │ │ └── docker-compose.yml.j2
│ │ └── vars
│ │ └── main.yml
│ └── psql
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ │ └── docker-compose.yml.j2
│ └── vars
│ └── main.yml
├── libvirt-kvm
│ ├── config
│ │ ├── cloud_init.yml
│ │ └── network_config.yml
│ ├── main.tf
│ ├── variables.tf
Run Playbook and testing
Finally, let's run ansible-playbook to deploy PostgreSQL and Flask:
$ ls
ansible.cfg hosts playbook.yml roles
$ ansible-playbook -i host playbook.yml
_____________________
< PLAY [Deploy Flask] >
---------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
________________________
< TASK [Gathering Facts] >
------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| |
...
____________
< PLAY RECAP >
------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
192.168.122.15 : ok=13 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.122.19 : ok=15 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
After complete, just make sure there is no error. Then you see there are two created. In VM1 is Flask and VM2 is Postgresql:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
f3978427e34c flask_todo_flask "python run.py" About a minute ago Up About a minute 0.0.0.0:5000->5000/tcp, :
::5000->5000/tcp app
$ docker ps
fbebdff75a6e postgres:13 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:5433->5432/tcp, [::]:5433
->5432/tcp db
Try to access the app using browsers, just type http://<vm1_ip>:
Conclusion
Finally, thank you for reading this article. Feel free to leave a comment if you have any questions, suggestions, or feedback.
Nb: Project Repo: danielcristho/that-i-write
SOCIAL SHARE CARD GENERATOR