##Common pre-setup
wget -O /var/lib/vz/template/iso/debian-12-generic-amd64.qcow2 https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2
apt update && apt install -y libguestfs-tools
virt-customize -a /var/lib/vz/template/iso/debian-12-generic-amd64.qcow2 --install qemu-guest-agent

##PVE1 steps
qm create 800 --name "debian-12-db-template" --memory 4096 --cores 2 --net0 virtio,bridge=vmbr0,tag=201
qm importdisk 800 /var/lib/vz/template/iso/debian-12-generic-amd64.qcow2 pve1-hdd01-01
qm set 800 --scsihw virtio-scsi-pci --scsi0 pve1-hdd01-01:vm-800-disk-0
qm set 800 --boot c --bootdisk scsi0
qm set 800 --ide2 pve1-hdd01-01:cloudinit
qm set 800 --serial0 socket --vga serial0
qm set 800 --agent enabled=1
qm resize 800 scsi0 32G
qm template 800
qm clone 800 151 --name db-node-02
qm set 151 --ipconfig0 ip=172.16.201.151/24,gw=172.16.201.1
qm start 151


##PVE2 steps
qm create 801 --name "debian-12-db-template" --memory 4096 --cores 2 --net0 virtio,bridge=vmbr0,tag=201
qm importdisk 801 /var/lib/vz/template/iso/debian-12-generic-amd64.qcow2 pve2-ssd01-01
qm set 801 --scsihw virtio-scsi-pci --scsi0 pve2-ssd01-01:vm-801-disk-0
qm set 801 --boot c --bootdisk scsi0
qm set 801 --ide2 pve2-ssd01-01:cloudinit
qm set 801 --serial0 socket --vga serial0
qm set 801 --agent enabled=1
qm resize 801 scsi0 32G
qm template 801
qm clone 801 152 --name db-node-01
qm set 152 --ipconfig0 ip=172.16.201.152/24,gw=172.16.201.1
qm start 152

##cloud-init setup on both
user:
password:
domain:
dns server:
local ssh public key (workstation)
--regenerate-- button at top
--start vm--

##Common Setup
apt update && apt install -y mariadb-server

##.151 config file
{/etc/mysql/mariadb.conf.d/50-galera.cnf}
[mysqld]
wsrep_on                 = ON
wsrep_provider           = /usr/lib/libgalera_smm.so
wsrep_cluster_name       = "mgdev1"
wsrep_cluster_address    = "gcomm://172.16.201.151,172.16.201.152,172.16.201.250"
wsrep_node_address       = "172.16.201.151"
binlog_format            = row
default_storage_engine   = InnoDB
innodb_autoinc_lock_mode = 2
bind-address             = 0.0.0.0

##.152 config file
{/etc/mysql/mariadb.conf.d/50-galera.cnf}
[mysqld]
wsrep_on                 = ON
wsrep_provider           = /usr/lib/libgalera_smm.so
wsrep_cluster_name       = "mgdev1"
wsrep_cluster_address    = "gcomm://172.16.201.151,172.16.201.152,172.16.201.250"
wsrep_node_address       = "172.16.201.152" 
binlog_format            = row
default_storage_engine   = InnoDB
innodb_autoinc_lock_mode = 2
bind-address             = 0.0.0.0
# Only needed on Node 1 for the very first launch
wsrep_new_cluster

##Deploy steps
1. run on primary node (with new cluster flag first) - "sudo systemctl enable --now mariadb"
2. run these to validate that it has joined the pi witness
-- systemctl status mariadb
-- mariadb -e "SHOW STATUS LIKE 'wsrep_cluster_size';"
3. Make sure pi has correct cluster and ip's
4. use "systemctl restart garb" and "systemctl status garb" to confirm status
5. once cluster has pi and primary node then comment out the "wsrep_new_cluster" line in the 50-galera.cnf on the primary node
6. run on the second node: "sudo systemctl enable --now mariadb" and use commands from step 3 to validate that there are 3 nodes

##Layer on Keepalived on both VMs
##Common keepalived install steps on both VMs
sudo apt update && sudo apt install -y keepalived

##.151 keepalived config
nano /etc/keepalived/keepalived.conf
#paste:
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 15
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 42
    }
    virtual_ipaddress {
        172.16.201.150/24
    }
}

##.152 keepalived config
nano /etc/keepalived/keepalived.conf
#paste:
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 15
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 42
    }
    virtual_ipaddress {
        172.16.201.150/24
    }
}

##keepalived start - both vms
sudo systemctl enable --now keepalived
sudo systemctl status keepalived

##test from anywhere else
ping 172.16.201.150