Commit 971d90d9 authored by Andre Blanke's avatar Andre Blanke
Browse files

Start a CockroachDB cluster from the frontend server

parent 45bf1c6d
......@@ -13,9 +13,16 @@
├─ ansible/ # Ansible files used by Vagrant for provisioning. Follows the Ansible directory layout
│ ├─ library/ # Additional Ansible modules
│ │ └─ aur.py # Embedded ansible-aur module. See https://github.com/kewlfft/ansible-aur.
│ ├─ roles/worker/files/
│ │ └─ shortener.service # systemd service to start the URL shortener
│ ├─ load_balancer.yml
│ ├─ roles/
│ │ ├─ frontend/files/
│ │ │ ├─ haproxy/
│ │ │ │ └─ haproxy.cfg
│ │ │ └─ dbsetup.sql
│ │ └─ worker/files/
│ │ └─ systemd/system/
│ │ ├─ cockroach.service.d/override.conf
│ │ └─ shortener.service # systemd service to start the URL shortener
│ ├─ frontend.yml
│ └─ worker.yml
├─ frontend/ # Angular frontend
│ └─ src/
......@@ -34,13 +41,12 @@
│ │ │ │ └─ ShortenerApplication.kt # Starting point of the backend
│ │ │ └─ resources/application.properties
│ │ └─ test/kotlin/xyz/upbshrt/shortener/
│ ├─ mvnw # Maven wrapper executable for Unix
│ ├─ mvnw # Maven wrapper executable for Unix systems
│ ├─ mvnw.cmd # Maven wrapper executable for Windows
│ └─ pom.xml
├─ ansible.cfg
├─ API.md
├─ README.md
├─ requirements.txt # Required Python packages
└─ Vagrantfile # Virtual machine configurations
```
......@@ -55,11 +61,9 @@ Accessing the short URL will then cause a redirect to the associated long URL st
## Deployment
The project is currently deployed using an Apache Web Server instance which serves the frontend itself
and redirects requests other than `/index.html` to the Spring backend.
Vagrant can be used to orchestrate multiple virtual machines provisioned using Ansible with multiple
worker servers running the web server and a single server acting as a load balancer at the present time.
The project is currently deployed using an Apache Web Server instance which serves the web frontend
by itself and redirects requests other than `/index.html` to the Spring backend running on one of the
workers picked by HAProxy which is running on the frontend server.
## API
......
num_workers = 3
Vagrant.configure("2") do |config|
config.vm.box = "archlinux/archlinux"
config.vm.provision :ansible_local do |ansible|
ansible.playbook = "ansible/worker.yml"
end
num_workers.times do |n|
worker_name = "worker-#{n + 1}"
config.vm.define "load_balancer" do |load_balancer|
load_balancer.vm.provision :ansible_local do |ansible|
ansible.playbook = "ansible/load_balancer.yml"
config.vm.define worker_name do |worker|
worker.vm.hostname = worker_name
worker.vm.network :private_network, ip: "192.168.10.#{10 + n + 1}"
worker.vm.provision :ansible_local do |ansible|
ansible.playbook = "ansible/worker.yml"
end
end
end
config.vm.define "worker_0"
config.vm.define "worker_1"
frontend_name = "frontend"
config.vm.define frontend_name do |frontend|
frontend.vm.hostname = frontend_name
frontend.vm.network :private_network, ip: "192.168.10.10"
frontend.vm.provision :ansible_local do |ansible|
ansible.playbook = "ansible/frontend.yml"
end
end
end
---
- hosts: all
become: yes
tasks:
- name: Install HAProxy
package:
name: haproxy
state: present
- name: Install base-devel
package:
name: base-devel
state: present
- name: Add aur_builder user
block:
- user:
name: aur_builder
group: wheel
- lineinfile:
path: /etc/sudoers.d/00-aur_builder
line: 'aur_builder ALL=(ALL) NOPASSWD: /usr/bin/pacman'
create: yes
validate: 'visudo --check --file %s'
- name: Install yay-bin
become_user: aur_builder
aur: name=yay-bin use=makepkg state=present
- name: Install CockroachDB
become_user: aur_builder
aur:
name: cockroachdb-bin
state: present
# cockroach init fails with a status code of 1 if the cluster has already been initialized.
# To keep the provisioning scripts idempotent we add the "|| true" part so those errors are
# ignored.
- name: Initialize CockroachDB cluster
shell:
cmd: cockroach init --insecure --host=192.168.10.11 || true
- name: Setup shortener database
shell:
cmd: cockroach sql --insecure --host=192.168.10.11 < /vagrant/ansible/roles/frontend/files/dbsetup.sql
---
- hosts: all
tasks:
- name: Install HAProxy
community.general.pacman:
name: haproxy
state: present
CREATE USER IF NOT EXISTS shortener;
CREATE DATABASE shortener;
CREATE USER IF NOT EXISTS shortener;
CREATE DATABASE IF NOT EXISTS shortener;
GRANT ALL ON DATABASE shortener TO shortener;
[Service]
ExecStart=
ExecStart=/usr/bin/cockroach start-single-node --certs-dir /etc/cockroach --store=${COCKROACH_STORE} --http-addr localhost:8081 $COCKROACH_FLAGS
ExecStart=/usr/bin/cockroach start --certs-dir=/etc/cockroach/ --store=${COCKROACH_STORE} --http-addr=localhost:8081 --join=192.168.10.11:26257,192.168.10.12:26257,192.168.10.13:26257 $COCKROACH_FLAGS
......@@ -2,7 +2,4 @@
Description=upbshrt.xyz backend
[Service]
User=shortener
Group=shortener
ExecStart=/usr/bin/test
......@@ -41,7 +41,7 @@
path: /etc/systemd/system/cockroach.service.d/
state: directory
- name: Copy override.conf override file to /etc/systemd/cockroach.service.d/
copy:
template:
src: /vagrant/ansible/roles/worker/files/systemd/system/cockroach.service.d/override.conf
dest: /etc/systemd/system/cockroach.service.d/override.conf
- name: Start and enable cockroach.service
......@@ -49,9 +49,6 @@
name: cockroach
state: started
enabled: yes
- name: Setup database
shell:
cmd: cockroach sql --insecure < /vagrant/ansible/roles/worker/files/setup.sql
- name: Install OpenJDK 15
package:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment