diff --git a/ansible/rqlite/README.md b/ansible/rqlite/README.md new file mode 100644 index 0000000..c43edea --- /dev/null +++ b/ansible/rqlite/README.md @@ -0,0 +1,82 @@ +# Rqlite + +Go to `https://github.com/rqlite/rqlite/releases` +This presupposes you use `Rqlite v8.36.11` +The service user you use is `ubuntu` + +```bash +version="v8.36.11" +curl -L https://github.com/rqlite/rqlite/releases/download/"$version"/rqlite-"$version"-linux-amd64.tar.gz -o rqlite-"$version"-linux-amd64.tar.gz +tar xvfz rqlite-"$version"-linux-amd64.tar.gz +cd rqlite-"$version"-linux-amd64 +sudo cp rq* /usr/local/bin/ +cd +rm -r rqlite-"$version"-linux-amd64.tar.gz rqlite-"$version"-linux-amd64 +``` + +Create a service for initial leader node at `sudo vim /etc/systemd/system/rqlited.service` +```conf +[Unit] +Description=rqlite distributed SQLite node +After=network.target + +[Service] +Type=simple +User=ubuntu +Group=ubuntu +WorkingDirectory=/home/ubuntu +ExecStart=/usr/local/bin/rqlited -node-id <1, 2, 3, etc> -http-addr :4001 -raft-addr :4002 /home/ubuntu/data +Restart=on-failure +RestartSec=5 + +[Install] +WantedBy=multi-user.target +``` + + +Create a service for joining nodes at `sudo vim /etc/systemd/system/rqlited.service` +```conf +[Unit] +Description=rqlite distributed SQLite node +After=network.target + +[Service] +Type=simple +User=ubuntu +Group=ubuntu +WorkingDirectory=/home/ubuntu +ExecStart=/usr/local/bin/rqlited -node-id <1, 2, 3, etc> -http-addr :4001 -raft-addr :4002 -join :4002 /home/ubuntu/data +Restart=on-failure +RestartSec=5 + +[Install] +WantedBy=multi-user.target +``` + + +Prepare directory and permissions +```bash +mkdir -p /home/ubuntu/data + +sudo chown -R ubuntu:ubuntu /home/ubuntu/data +``` + +start and enable service +```bash +sudo systemctl daemon-reload +sudo systemctl enable --now rqlited +``` + +check status +```bash +sudo systemctl status rqlited +``` + + +from your dev machine which has access to all machines (or one of the nodes) run +```bash +# On any node +rqlite -H +``` + + diff --git a/ansible/rqlite/ansible/deploy-rqlite.yml b/ansible/rqlite/ansible/deploy-rqlite.yml new file mode 100644 index 0000000..a315db6 --- /dev/null +++ b/ansible/rqlite/ansible/deploy-rqlite.yml @@ -0,0 +1,340 @@ +--- +# Ansible playbook for deploying a rqlite cluster +# Run with: ansible-playbook -i inventory.yml deploy-rqlite.yml -e "node_ips=10.203.146.136,10.203.146.91,10.203.146.232 leader_ip=10.203.146.232 ansible_user=ubuntu" + +- name: Process input parameters and set up host groups + hosts: localhost + connection: local # Use local connection instead of SSH + gather_facts: no + tasks: + - name: Validate required variables are provided + fail: + msg: "Required variables 'node_ips' and 'leader_ip' must be provided" + when: node_ips is not defined or leader_ip is not defined + + - name: Create array of IPs from comma-separated string + set_fact: + ip_list: "{{ node_ips.split(',') }}" + + - name: Verify leader_ip is in the node_ips list + fail: + msg: "Leader IP ({{ leader_ip }}) must be included in the list of node IPs" + when: leader_ip not in ip_list + + - name: Add leader node to inventory + add_host: + name: "leader" + ansible_host: "{{ leader_ip }}" + node_id: "{{ ip_list.index(leader_ip) + 1 }}" + groups: "rqlite_leader,rqlite_cluster" + + # Explicitly handle up to 2 follower nodes - this approach avoids complex loops + - name: Add first follower node if it exists + add_host: + name: "follower1" + ansible_host: "{{ item }}" + node_id: "{{ ip_list.index(item) + 1 }}" + groups: "rqlite_followers,rqlite_cluster" + with_items: "{{ ip_list }}" + when: item != leader_ip + run_once: true + ignore_errors: yes + + - name: Add second follower node if it exists + add_host: + name: "follower2" + ansible_host: "{{ item }}" + node_id: "{{ ip_list.index(item) + 1 }}" + groups: "rqlite_followers,rqlite_cluster" + with_items: "{{ ip_list }}" + when: + - item != leader_ip + - item != (hostvars['follower1']['ansible_host'] | default('')) + run_once: true + ignore_errors: yes + + - name: Display hosts configuration + debug: + msg: | + Leader: {{ hostvars['leader']['ansible_host'] }} (node ID: {{ hostvars['leader']['node_id'] }}) + Followers: {{ groups['rqlite_followers'] | map('extract', hostvars, ['ansible_host']) | join(', ') }} + +# Deploy leader node first, then followers +- name: Deploy leader node + hosts: rqlite_leader + gather_facts: yes + become: yes + vars: + rqlite_version: "8.36.11" + rqlite_http_port: 4001 + rqlite_raft_port: 4002 + rqlite_data_dir: "/home/{{ ansible_user }}/data" + rqlite_download_url: "https://github.com/rqlite/rqlite/releases/download/v{{ rqlite_version }}/rqlite-v{{ rqlite_version }}-linux-amd64.tar.gz" + rqlite_extract_dir: "/opt/rqlite" + leader_host: "{{ hostvars['leader']['ansible_host'] }}" + + tasks: + - name: Install required packages + apt: + name: + - tar + - wget + - curl + state: present + update_cache: yes + when: ansible_os_family == "Debian" + + - name: Install required packages (RHEL/CentOS) + yum: + name: + - tar + - wget + - curl + state: present + when: ansible_os_family == "RedHat" + + - name: Create rqlite directories + file: + path: "{{ item }}" + state: directory + mode: '0755' + owner: "{{ ansible_user }}" + group: "{{ ansible_user }}" + with_items: + - "{{ rqlite_extract_dir }}" + - "{{ rqlite_data_dir }}" + + - name: Download and extract rqlite + unarchive: + src: "{{ rqlite_download_url }}" + dest: "{{ rqlite_extract_dir }}" + remote_src: yes + creates: "{{ rqlite_extract_dir }}/rqlited" + extra_opts: + - --strip-components=1 + owner: "{{ ansible_user }}" + group: "{{ ansible_user }}" + + - name: Copy rqlite binaries to /usr/local/bin + copy: + src: "{{ rqlite_extract_dir }}/{{ item }}" + dest: "/usr/local/bin/{{ item }}" + mode: '0755' + remote_src: yes + with_items: + - rqlited + - rqlite + + - name: Create leader service file + copy: + dest: /etc/systemd/system/rqlited.service + content: | + [Unit] + Description=rqlite distributed SQLite leader node + After=network.target + + [Service] + Type=simple + User={{ ansible_user }} + Group={{ ansible_user }} + WorkingDirectory=/home/{{ ansible_user }} + ExecStart=/usr/local/bin/rqlited -node-id {{ node_id }} -http-addr {{ ansible_host }}:{{ rqlite_http_port }} -raft-addr {{ ansible_host }}:{{ rqlite_raft_port }} {{ rqlite_data_dir }} + Restart=on-failure + RestartSec=5 + + [Install] + WantedBy=multi-user.target + + - name: Configure firewall (UFW) + ufw: + rule: allow + port: "{{ item }}" + proto: tcp + with_items: + - "{{ rqlite_http_port }}" + - "{{ rqlite_raft_port }}" + when: ansible_os_family == "Debian" + ignore_errors: yes # In case UFW isn't installed or enabled + + - name: Configure firewall (firewalld) + firewalld: + port: "{{ item }}/tcp" + permanent: yes + state: enabled + with_items: + - "{{ rqlite_http_port }}" + - "{{ rqlite_raft_port }}" + when: ansible_os_family == "RedHat" + ignore_errors: yes # In case firewalld isn't installed or enabled + + - name: Start and enable rqlite service on leader node + systemd: + name: rqlited + enabled: yes + daemon_reload: yes + state: started + + # Verify leader is fully operational before moving to followers + - name: Wait for leader node to be fully operational + uri: + url: "http://{{ leader_host }}:{{ rqlite_http_port }}/readyz" + return_content: yes + register: leader_ready + until: leader_ready.status == 200 + retries: 12 # Try for up to 1 minute (5 seconds × 12) + delay: 5 # 5 second delay between retries + + - name: Ensure leader has stabilized + pause: + seconds: 5 + + - name: Check leader status + shell: "curl -s http://{{ leader_host }}:{{ rqlite_http_port }}/status?pretty" + register: leader_status + changed_when: false + + - name: Display leader status + debug: + var: leader_status.stdout_lines + +# Deploy follower nodes after the leader is ready +- name: Deploy follower nodes + hosts: rqlite_followers + gather_facts: yes + become: yes + vars: + rqlite_version: "8.36.11" + rqlite_http_port: 4001 + rqlite_raft_port: 4002 + rqlite_data_dir: "/home/{{ ansible_user }}/data" + rqlite_download_url: "https://github.com/rqlite/rqlite/releases/download/v{{ rqlite_version }}/rqlite-v{{ rqlite_version }}-linux-amd64.tar.gz" + rqlite_extract_dir: "/opt/rqlite" + leader_host: "{{ hostvars['leader']['ansible_host'] }}" + + tasks: + - name: Install required packages + apt: + name: + - tar + - wget + - curl + state: present + update_cache: yes + when: ansible_os_family == "Debian" + + - name: Install required packages (RHEL/CentOS) + yum: + name: + - tar + - wget + - curl + state: present + when: ansible_os_family == "RedHat" + + - name: Create rqlite directories + file: + path: "{{ item }}" + state: directory + mode: '0755' + owner: "{{ ansible_user }}" + group: "{{ ansible_user }}" + with_items: + - "{{ rqlite_extract_dir }}" + - "{{ rqlite_data_dir }}" + + - name: Download and extract rqlite + unarchive: + src: "{{ rqlite_download_url }}" + dest: "{{ rqlite_extract_dir }}" + remote_src: yes + creates: "{{ rqlite_extract_dir }}/rqlited" + extra_opts: + - --strip-components=1 + owner: "{{ ansible_user }}" + group: "{{ ansible_user }}" + + - name: Copy rqlite binaries to /usr/local/bin + copy: + src: "{{ rqlite_extract_dir }}/{{ item }}" + dest: "/usr/local/bin/{{ item }}" + mode: '0755' + remote_src: yes + with_items: + - rqlited + - rqlite + + - name: Create follower service file + copy: + dest: /etc/systemd/system/rqlited.service + content: | + [Unit] + Description=rqlite distributed SQLite follower node + After=network.target + + [Service] + Type=simple + User={{ ansible_user }} + Group={{ ansible_user }} + WorkingDirectory=/home/{{ ansible_user }} + ExecStart=/usr/local/bin/rqlited -node-id {{ node_id }} -http-addr {{ ansible_host }}:{{ rqlite_http_port }} -raft-addr {{ ansible_host }}:{{ rqlite_raft_port }} -join {{ leader_host }}:{{ rqlite_raft_port }} {{ rqlite_data_dir }} + Restart=on-failure + RestartSec=5 + + [Install] + WantedBy=multi-user.target + + - name: Configure firewall (UFW) + ufw: + rule: allow + port: "{{ item }}" + proto: tcp + with_items: + - "{{ rqlite_http_port }}" + - "{{ rqlite_raft_port }}" + when: ansible_os_family == "Debian" + ignore_errors: yes # In case UFW isn't installed or enabled + + - name: Configure firewall (firewalld) + firewalld: + port: "{{ item }}/tcp" + permanent: yes + state: enabled + with_items: + - "{{ rqlite_http_port }}" + - "{{ rqlite_raft_port }}" + when: ansible_os_family == "RedHat" + ignore_errors: yes # In case firewalld isn't installed or enabled + + - name: Start and enable rqlite service on follower nodes + systemd: + name: rqlited + enabled: yes + daemon_reload: yes + state: started + +# Check cluster status after all nodes are deployed +- name: Verify cluster status + hosts: localhost + connection: local + gather_facts: no + vars: + leader_host: "{{ hostvars['leader']['ansible_host'] }}" + rqlite_http_port: 4001 + tasks: + - name: Wait for all nodes to be up + uri: + url: "http://{{ leader_host }}:{{ rqlite_http_port }}/readyz" + return_content: yes + register: cluster_ready + until: cluster_ready.status == 200 + retries: 6 + delay: 5 + + - name: Check cluster status + shell: "curl -s http://{{ leader_host }}:{{ rqlite_http_port }}/nodes?pretty" + register: cluster_status + changed_when: false + + - name: Display cluster status + debug: + var: cluster_status.stdout_lines diff --git a/ansible/rqlite/ansible/inventory.yml b/ansible/rqlite/ansible/inventory.yml new file mode 100644 index 0000000..8004ac1 --- /dev/null +++ b/ansible/rqlite/ansible/inventory.yml @@ -0,0 +1,5 @@ +--- +all: + hosts: + localhost: + ansible_connection: local # This forces local connection for localhost diff --git a/ansible/rqlite/ansible/rqlite-cluster.yml b/ansible/rqlite/ansible/rqlite-cluster.yml new file mode 100644 index 0000000..56950fa --- /dev/null +++ b/ansible/rqlite/ansible/rqlite-cluster.yml @@ -0,0 +1,157 @@ +--- +# Ansible playbook for deploying a 3-node rqlite cluster +# Filename: rqlite-cluster.yml + +- name: Deploy rqlite cluster + hosts: rqlite_nodes + become: yes + vars: + rqlite_version: "7.21.4" # Change to the latest version as needed + rqlite_download_url: "https://github.com/rqlite/rqlite/releases/download/v{{ rqlite_version }}/rqlite-v{{ rqlite_version }}-linux-amd64.tar.gz" + rqlite_extract_dir: "/opt/rqlite" + rqlite_data_dir: "/var/lib/rqlite" + rqlite_http_port: 4001 + rqlite_raft_port: 4002 + + tasks: + - name: Install required packages + apt: + name: + - tar + - wget + - curl + state: present + update_cache: yes + + - name: Create directories + file: + path: "{{ item }}" + state: directory + mode: '0755' + loop: + - "{{ rqlite_extract_dir }}" + - "{{ rqlite_data_dir }}" + + - name: Download and extract rqlite + unarchive: + src: "{{ rqlite_download_url }}" + dest: "{{ rqlite_extract_dir }}" + remote_src: yes + creates: "{{ rqlite_extract_dir }}/rqlited" + extra_opts: + - --strip-components=1 + + - name: Create symlinks to rqlite binaries + file: + src: "{{ rqlite_extract_dir }}/{{ item }}" + dest: "/usr/local/bin/{{ item }}" + state: link + loop: + - rqlited + - rqlite + + - name: Create rqlite systemd service + template: + src: rqlited.service.j2 + dest: /etc/systemd/system/rqlited.service + owner: root + group: root + mode: '0644' + + - name: Check if UFW is installed and enabled + command: ufw status + register: ufw_status + changed_when: false + failed_when: false + + - name: Configure firewall with UFW if available + ufw: + rule: allow + port: "{{ item }}" + proto: tcp + loop: + - "{{ rqlite_http_port }}" + - "{{ rqlite_raft_port }}" + when: "ufw_status.rc == 0 and 'Status: active' in ufw_status.stdout" + + - name: Ensure iptables allows required ports + iptables: + chain: INPUT + protocol: tcp + destination_port: "{{ item }}" + jump: ACCEPT + loop: + - "{{ rqlite_http_port }}" + - "{{ rqlite_raft_port }}" + when: "ufw_status.rc != 0 or 'Status: active' not in ufw_status.stdout" + ignore_errors: yes + + - name: Enable and start rqlited service on leader node + systemd: + name: rqlited + enabled: yes + daemon_reload: yes + state: started + when: inventory_hostname == groups['rqlite_nodes'][0] + + - name: Wait for leader node to be up + wait_for: + host: "{{ hostvars[groups['rqlite_nodes'][0]]['ansible_host'] }}" + port: "{{ rqlite_http_port }}" + timeout: 60 + when: inventory_hostname != groups['rqlite_nodes'][0] + + - name: Debug connection to leader + shell: "curl -v {{ hostvars[groups['rqlite_nodes'][0]]['ansible_host'] }}:{{ rqlite_http_port }}/status" + register: connectivity_test + failed_when: false + when: inventory_hostname != groups['rqlite_nodes'][0] + + - name: Display connectivity test results + debug: + var: connectivity_test + when: inventory_hostname != groups['rqlite_nodes'][0] + + - name: Enable and start rqlited service on follower nodes + systemd: + name: rqlited + enabled: yes + daemon_reload: yes + state: started + when: inventory_hostname != groups['rqlite_nodes'][0] + + - name: Check service status on all nodes + shell: systemctl status rqlited + register: service_status + failed_when: false + + - name: Display service status + debug: + var: service_status.stdout_lines + + - name: Check service logs + shell: journalctl -u rqlited -n 50 + register: service_logs + failed_when: false + + - name: Display service logs + debug: + var: service_logs.stdout_lines + + - name: Wait for all nodes to be up + wait_for: + host: "{{ ansible_host }}" + port: "{{ rqlite_http_port }}" + timeout: 60 + when: false + + - name: Check cluster status + shell: "curl -s http://{{ hostvars[groups['rqlite_nodes'][0]]['ansible_host'] }}:{{ rqlite_http_port }}/nodes?pretty" + register: cluster_status + changed_when: false + when: inventory_hostname == groups['rqlite_nodes'][0] + + - name: Display cluster status + debug: + var: cluster_status.stdout + when: inventory_hostname == groups['rqlite_nodes'][0] diff --git a/ansible/rqlite/ansible/rqlited.service.j2 b/ansible/rqlite/ansible/rqlited.service.j2 new file mode 100644 index 0000000..7002a6a --- /dev/null +++ b/ansible/rqlite/ansible/rqlited.service.j2 @@ -0,0 +1,21 @@ +[Unit] +Description=rqlite distributed SQLite database +After=network.target + +[Service] +Type=simple +User=root +Group=root +LimitNOFILE=65536 +Restart=always +RestartSec=5 +{% if inventory_hostname == groups['rqlite_nodes'][0] %} +# Leader node configuration - initial node doesn't join any other node +ExecStart=/usr/local/bin/rqlited -node-id 1 -http-addr 0.0.0.0:{{ rqlite_http_port }} -raft-addr 0.0.0.0:{{ rqlite_raft_port }} -http-adv-addr {{ ansible_host }}:{{ rqlite_http_port }} -raft-adv-addr {{ ansible_host }}:{{ rqlite_raft_port }} {{ rqlite_data_dir }} +{% else %} +# Follower node configuration - they join the leader +ExecStart=/usr/local/bin/rqlited -node-id {{ groups['rqlite_nodes'].index(inventory_hostname) + 1 }} -http-addr 0.0.0.0:{{ rqlite_http_port }} -raft-addr 0.0.0.0:{{ rqlite_raft_port }} -http-adv-addr {{ ansible_host }}:{{ rqlite_http_port }} -raft-adv-addr {{ ansible_host }}:{{ rqlite_raft_port }} -join {{ hostvars[groups['rqlite_nodes'][0]]['ansible_host'] }}:{{ rqlite_raft_port }} {{ rqlite_data_dir }} +{% endif %} + +[Install] +WantedBy=multi-user.target diff --git a/ansible/rqlite/ansible/teardown-rqlite.yml b/ansible/rqlite/ansible/teardown-rqlite.yml new file mode 100644 index 0000000..b5790a7 --- /dev/null +++ b/ansible/rqlite/ansible/teardown-rqlite.yml @@ -0,0 +1,155 @@ +--- +# Ansible playbook for tearing down an rqlite cluster +# Run with: ansible-playbook -i inventory.yml teardown-rqlite.yml -e "node_ips=10.203.146.136,10.203.146.91,10.203.146.232 leader_ip=10.203.146.232 ansible_user=ubuntu" + +- name: Process input parameters and set up host groups + hosts: localhost + connection: local + gather_facts: no + tasks: + - name: Validate required variables are provided + fail: + msg: "Required variables 'node_ips' and 'leader_ip' must be provided" + when: node_ips is not defined or leader_ip is not defined + + - name: Create array of IPs from comma-separated string + set_fact: + ip_list: "{{ node_ips.split(',') }}" + + - name: Verify leader_ip is in the node_ips list + fail: + msg: "Leader IP ({{ leader_ip }}) must be included in the list of node IPs" + when: leader_ip not in ip_list + + - name: Add leader node to inventory + add_host: + name: "leader" + ansible_host: "{{ leader_ip }}" + node_id: "{{ ip_list.index(leader_ip) + 1 }}" + groups: "rqlite_leader,rqlite_cluster" + + # Explicitly handle up to 2 follower nodes + - name: Add first follower node if it exists + add_host: + name: "follower1" + ansible_host: "{{ item }}" + node_id: "{{ ip_list.index(item) + 1 }}" + groups: "rqlite_followers,rqlite_cluster" + with_items: "{{ ip_list }}" + when: item != leader_ip + run_once: true + ignore_errors: yes + + - name: Add second follower node if it exists + add_host: + name: "follower2" + ansible_host: "{{ item }}" + node_id: "{{ ip_list.index(item) + 1 }}" + groups: "rqlite_followers,rqlite_cluster" + with_items: "{{ ip_list }}" + when: + - item != leader_ip + - item != (hostvars['follower1']['ansible_host'] | default('')) + run_once: true + ignore_errors: yes + + - name: Display hosts that will be cleaned up + debug: + msg: | + Preparing to remove rqlite from the following hosts: + - Leader: {{ hostvars['leader']['ansible_host'] }} + - Followers: {{ groups['rqlite_followers'] | map('extract', hostvars, ['ansible_host']) | join(', ') }} + + WARNING: This will completely remove rqlite and all its data from these servers. + To cancel, press Ctrl+C now. + + - name: Pause for confirmation + pause: + prompt: "Press Enter to continue with the teardown process, or Ctrl+C to abort" + seconds: 10 + +- name: Tear down rqlite on all nodes + hosts: rqlite_cluster + gather_facts: yes + become: yes + vars: + rqlite_data_dir: "/home/{{ ansible_user }}/data" + rqlite_extract_dir: "/opt/rqlite" + + tasks: + - name: Stop and disable rqlited service + systemd: + name: rqlited + state: stopped + enabled: no + ignore_errors: yes + + - name: Remove rqlited service file + file: + path: /etc/systemd/system/rqlited.service + state: absent + + - name: Run daemon-reload to update systemd + systemd: + daemon_reload: yes + + - name: Remove rqlite binaries + file: + path: "/usr/local/bin/{{ item }}" + state: absent + with_items: + - rqlited + - rqlite + + - name: Remove rqlite data directory + file: + path: "{{ rqlite_data_dir }}" + state: absent + + - name: Remove rqlite extract directory + file: + path: "{{ rqlite_extract_dir }}" + state: absent + + - name: Close firewall ports (UFW) + ufw: + rule: allow + port: "{{ item }}" + proto: tcp + delete: yes + with_items: + - 4001 # Default HTTP port + - 4002 # Default Raft port + when: ansible_os_family == "Debian" + ignore_errors: yes # In case UFW isn't installed + + - name: Close firewall ports (firewalld) + firewalld: + port: "{{ item }}/tcp" + permanent: yes + state: disabled + with_items: + - 4001 # Default HTTP port + - 4002 # Default Raft port + when: ansible_os_family == "RedHat" + ignore_errors: yes # In case firewalld isn't installed + +- name: Verify teardown is complete + hosts: localhost + connection: local + gather_facts: no + tasks: + - name: Display completion message + debug: + msg: | + Teardown Complete + + rqlite has been completely removed from: + - Leader: {{ hostvars['leader']['ansible_host'] }} + - Followers: {{ groups['rqlite_followers'] | map('extract', hostvars, ['ansible_host']) | join(', ') }} + + The following items have been removed: + - rqlite service + - rqlite binaries + - rqlite data directories + - Firewall rules for rqlite