create wireguard mesh and deploy surrealdb
This commit is contained in:
parent
ae3999d4f7
commit
3f35d7dc9b
@ -3,5 +3,6 @@
|
||||
This repository has various deployment examples of real world software to the DeTEE network. The examples currently include:
|
||||
- [Gitea on DeTEE](https://gitea.detee.cloud/general/examples/src/branch/master/gitea/deploy_gitea.sh) - A small bash script that deploys a Gitea server (just like this one) to a VM on DeTEE
|
||||
- [Ansible Postgres](https://gitea.detee.cloud/general/examples/src/branch/master/ansible-postgres) - Deploy a Postgres DB and a read replica via Ansible to two DeTEE VMs.
|
||||
- [Wireguard Overlay](https://gitea.detee.cloud/general/examples/src/branch/master/wireguard-bastion) - Hide resources behind VPN, by leveraging VM deployments on DeTEE.
|
||||
- [Wireguard DMZ](https://gitea.detee.cloud/general/examples/src/branch/master/wireguard-bastion) - Hide resources behind WireGuard VPN, by leveraging VM deployments on DeTEE.
|
||||
- [Overlay Network](https://gitea.detee.cloud/general/examples/src/branch/master/overlay-network) - Automated deployment of an encrypted network overlay (full-mesh between VMs).
|
||||
- [Kubernetes (k3s)](https://gitea.detee.cloud/general/examples/src/branch/master/kubernetes) - Use k3s to deploy 5 Kubernetes nodes to DeTEE VMs, forming a small cluster.
|
||||
|
15
overlay-network/README.md
Normal file
15
overlay-network/README.md
Normal file
@ -0,0 +1,15 @@
|
||||
# Overlay Network
|
||||
|
||||
These scripts allow you to create an overlay network on top of DeTEE VMs. These
|
||||
VMs do not need a public IP, however they require an extra port to be
|
||||
forwarded. Every VM in the network will get an IP in the subnet `10.254.254.0/24`.
|
||||
|
||||
The VMs will be connected in a full-mesh topology, meaning each VM can
|
||||
communicate with each other VM directly. Here is a graphical representation of a
|
||||
full mesh from wikipedia: https://en.wikipedia.org/wiki/File:FullMeshNetwork.svg
|
||||
|
||||
To create the VMs, run `./create_vms.sh`.
|
||||
|
||||
To deploy the network overlay, run `./setup_wg_mesh.sh`. This will create an
|
||||
overlay on top of all the VMs created previously using the `./create_vms.sh`
|
||||
script, assigning IPs in alphabetical order.
|
14
overlay-network/create_vms.sh
Executable file
14
overlay-network/create_vms.sh
Executable file
@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
||||
cd $script_dir
|
||||
set -e
|
||||
export FORMAT=YAML
|
||||
mkdir -p tmp/vms
|
||||
|
||||
for vm_config in vm_configs/*; do
|
||||
vm_name=$(echo $vm_config | cut -d '/' -f2 | cut -d '.' -f1)
|
||||
detee-cli vm deploy --from-yaml $vm_config > tmp/vms/${vm_name}_install.yaml &&
|
||||
echo "The VM $vm_name got created." &
|
||||
done
|
||||
|
||||
wait
|
92
overlay-network/setup_wg_mesh.sh
Executable file
92
overlay-network/setup_wg_mesh.sh
Executable file
@ -0,0 +1,92 @@
|
||||
#!/bin/bash
|
||||
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
||||
cd $script_dir
|
||||
set -e
|
||||
export FORMAT=YAML
|
||||
mkdir -p tmp/wg
|
||||
mkdir -p tmp/logs
|
||||
rm tmp/vms/*inspect.yaml || true
|
||||
vms=()
|
||||
|
||||
# inspect VMs
|
||||
for vm_config in $(grep -r uuid: tmp/vms/ | awk '{ print $2}'); do
|
||||
vm_id=$(echo $vm_config | cut -d '/' -f2 | cut -d '.' -f1)
|
||||
detee-cli vm inspect $vm_id > tmp/vms/${vm_id}_inspect.yaml
|
||||
vm_name=$(grep 'hostname: ' tmp/vms/${vm_id}_inspect.yaml |
|
||||
awk '{ print $2 }')
|
||||
mv tmp/vms/${vm_id}_inspect.yaml tmp/vms/${vm_name}_inspect.yaml
|
||||
done
|
||||
|
||||
# define VM object
|
||||
vm_count=0
|
||||
new_vm() {
|
||||
(( vm_count++ )) || true
|
||||
local vm_name="$1"
|
||||
local vm_id="vm$vm_count"
|
||||
|
||||
local vm_install_data="tmp/vms/${vm_name}_install.yaml"
|
||||
local vm_inspect_data="tmp/vms/${vm_name}_inspect.yaml"
|
||||
|
||||
vm_node_ip=$(grep 'ip: ' $vm_install_data | awk '{ print $2 }')
|
||||
vm_port=$(grep exposed_ports -A 1 $vm_inspect_data | tail -1 | grep -oE "[0-9]*")
|
||||
wg_privkey=$(wg genkey)
|
||||
wg_pubkey=$(echo $wg_privkey | wg pubkey)
|
||||
|
||||
declare -gA "$vm_id"
|
||||
eval "$vm_id[id]=$vm_count"
|
||||
eval "$vm_id[name]=$vm_name"
|
||||
eval "$vm_id[port]=$vm_port"
|
||||
eval "$vm_id[node_ip]=$vm_node_ip"
|
||||
eval "$vm_id[private_ip]=10.254.254.$vm_count"
|
||||
eval "$vm_id[wg_priv]=$wg_privkey"
|
||||
eval "$vm_id[wg_pub]=$wg_pubkey"
|
||||
|
||||
vms+=("$vm_id")
|
||||
}
|
||||
|
||||
# loops over all VMs
|
||||
for vm_install_file in tmp/vms/*_install.yaml; do
|
||||
vm_name=$(echo $vm_install_file | cut -d '/' -f3 | cut -d '_' -f1)
|
||||
new_vm $vm_name
|
||||
done
|
||||
|
||||
# loops over all VMs in array
|
||||
for main_vm_loop in "${vms[@]}"; do
|
||||
declare -n main_vm_ref="$main_vm_loop"
|
||||
wg_file="tmp/wg/${main_vm_ref[name]}.ini"
|
||||
{
|
||||
echo "[Interface]"
|
||||
echo "Address = "${main_vm_ref[private_ip]}" "
|
||||
echo "PrivateKey = "${main_vm_ref[wg_priv]}" "
|
||||
echo "ListenPort = 22"
|
||||
} > ${wg_file}
|
||||
|
||||
ssh="ssh -p ${main_vm_ref[port]} root@${main_vm_ref[node_ip]}"
|
||||
$ssh sed -i '/10.254.254./d' /etc/hosts
|
||||
|
||||
for inner_vm_loop in "${vms[@]}"; do
|
||||
declare -n inner_vm_ref="$inner_vm_loop"
|
||||
[[ "${inner_vm_ref[id]}" == "${main_vm_ref[id]}" ]] && continue
|
||||
echo ${inner_vm_ref[private_ip]} ${inner_vm_ref[name]} | $ssh tee -a /etc/hosts > /dev/null
|
||||
{
|
||||
echo
|
||||
echo "[Peer]"
|
||||
echo "PublicKey = ${inner_vm_ref[wg_pub]}"
|
||||
echo "Endpoint = ${inner_vm_ref[node_ip]}:${inner_vm_ref[port]}"
|
||||
echo "AllowedIPs = ${inner_vm_ref[private_ip]}"
|
||||
echo "PersistentKeepalive = 25"
|
||||
} >> ${wg_file}
|
||||
done
|
||||
echo WireGuard config written to ${wg_file}
|
||||
|
||||
$ssh pacman -Syu --noconfirm > tmp/logs/${main_vm_ref[name]}.log 2>&1
|
||||
$ssh pacman -S wireguard-tools --needed --noconfirm >> tmp/logs/${main_vm_ref[name]}.log 2>&1
|
||||
echo Packages installed for ${main_vm_ref[name]}
|
||||
|
||||
# TODO: make this reboot persistant
|
||||
$ssh sysctl -w net.ipv4.conf.all.forwarding=1 > /dev/null
|
||||
cat ${wg_file} | $ssh tee /etc/wireguard/brain.conf > /dev/null
|
||||
$ssh wg-quick down brain >> tmp/logs/${main_vm_ref[name]}.log 2>&1 || true
|
||||
$ssh wg-quick up brain >> tmp/logs/${main_vm_ref[name]}.log 2>&1 || true
|
||||
echo WireGuard started on ${main_vm_ref[name]}
|
||||
done
|
10
overlay-network/vm_configs/template-1.yaml
Normal file
10
overlay-network/vm_configs/template-1.yaml
Normal file
@ -0,0 +1,10 @@
|
||||
hostname: template-1
|
||||
hours: 2
|
||||
price: 20000
|
||||
location:
|
||||
country: "FR"
|
||||
ipv4: !PublishPorts [ ]
|
||||
public_ipv6: false
|
||||
vcpus: 4
|
||||
memory_mb: 8000
|
||||
disk_size_gb: 60
|
10
overlay-network/vm_configs/template-2.yaml
Normal file
10
overlay-network/vm_configs/template-2.yaml
Normal file
@ -0,0 +1,10 @@
|
||||
hostname: template-2
|
||||
hours: 2
|
||||
price: 20000
|
||||
location:
|
||||
country: "GB"
|
||||
ipv4: !PublishPorts [ ]
|
||||
public_ipv6: false
|
||||
vcpus: 4
|
||||
memory_mb: 8000
|
||||
disk_size_gb: 60
|
10
overlay-network/vm_configs/template-3.yaml
Normal file
10
overlay-network/vm_configs/template-3.yaml
Normal file
@ -0,0 +1,10 @@
|
||||
hostname: template-3
|
||||
hours: 2
|
||||
price: 20000
|
||||
location:
|
||||
country: "US"
|
||||
ipv4: !PublishPorts [ ]
|
||||
public_ipv6: false
|
||||
vcpus: 4
|
||||
memory_mb: 8000
|
||||
disk_size_gb: 60
|
10
overlay-network/vm_configs/template-n.yaml
Normal file
10
overlay-network/vm_configs/template-n.yaml
Normal file
@ -0,0 +1,10 @@
|
||||
hostname: template-n
|
||||
hours: 2
|
||||
price: 20000
|
||||
location:
|
||||
country: "US"
|
||||
ipv4: !PublishPorts [ ]
|
||||
public_ipv6: false
|
||||
vcpus: 4
|
||||
memory_mb: 8000
|
||||
disk_size_gb: 60
|
10
overlay-network/vm_configs/template-x.yaml
Normal file
10
overlay-network/vm_configs/template-x.yaml
Normal file
@ -0,0 +1,10 @@
|
||||
hostname: template-x
|
||||
hours: 2
|
||||
price: 20000
|
||||
location:
|
||||
country: "FR"
|
||||
ipv4: !PublishPorts [ ]
|
||||
public_ipv6: false
|
||||
vcpus: 4
|
||||
memory_mb: 8000
|
||||
disk_size_gb: 60
|
6
surrealdb_tikv_prod/README.md
Normal file
6
surrealdb_tikv_prod/README.md
Normal file
@ -0,0 +1,6 @@
|
||||
# Production SurrealDB setup with TiKV
|
||||
|
||||
This repo uses the [WireGuard
|
||||
Overlay](https://gitea.detee.cloud/general/examples/src/branch/master/wireguard-bastion)
|
||||
under the hood to protect the DataBase nodes, as TLS is not fully supported for
|
||||
SurrealDB + TiKV.
|
43
surrealdb_tikv_prod/deploy_db.sh
Executable file
43
surrealdb_tikv_prod/deploy_db.sh
Executable file
@ -0,0 +1,43 @@
|
||||
#!/bin/bash
|
||||
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
||||
cd $script_dir
|
||||
set -e
|
||||
|
||||
ssh_command() {
|
||||
vm_name="$1"
|
||||
vm_file="tmp/overlay-network/tmp/vms/${vm_name}_install.yaml"
|
||||
ip="$(grep 'ip: ' $vm_file | awk '{ print $NF }')"
|
||||
port="$(grep 'port: ' $vm_file | cut -d "'" -f2)"
|
||||
echo ssh -p $port root@$ip
|
||||
}
|
||||
export ssh_command
|
||||
|
||||
ssh_prod_brain1="$(ssh_command prod-brain-1)"
|
||||
ssh_prod_brain2="$(ssh_command prod-brain-2)"
|
||||
ssh_prod_brain3="$(ssh_command prod-brain-3)"
|
||||
ssh_prod_mon="$(ssh_command prod-brain-mon)"
|
||||
ssh_staging_brain1="$(ssh_command staging-brain-1)"
|
||||
ssh_staging_brain2="$(ssh_command staging-brain-2)"
|
||||
ssh_staging_brain3="$(ssh_command staging-brain-3)"
|
||||
ssh_staging_mon="$(ssh_command staging-brain-mon)"
|
||||
export ssh_bastion="$(ssh_command bastion-brain)"
|
||||
|
||||
# $ssh_bastion 'ssh-keygen -t ed25519 -f /root/.ssh/id_ed25519 -N ""'
|
||||
# bastion_pubkey=$($ssh_bastion cat /root/.ssh/id_ed25519.pub)
|
||||
# echo $bastion_pubkey | $ssh_prod_brain1 tee -a /root/.ssh/authorized_keys
|
||||
# echo $bastion_pubkey | $ssh_prod_brain2 tee -a /root/.ssh/authorized_keys
|
||||
# echo $bastion_pubkey | $ssh_prod_brain3 tee -a /root/.ssh/authorized_keys
|
||||
# echo $bastion_pubkey | $ssh_prod_mon tee -a /root/.ssh/authorized_keys
|
||||
# echo $bastion_pubkey | $ssh_staging_brain1 tee -a /root/.ssh/authorized_keys
|
||||
# echo $bastion_pubkey | $ssh_staging_brain2 tee -a /root/.ssh/authorized_keys
|
||||
# echo $bastion_pubkey | $ssh_staging_brain3 tee -a /root/.ssh/authorized_keys
|
||||
# echo $bastion_pubkey | $ssh_staging_mon tee -a /root/.ssh/authorized_keys
|
||||
|
||||
$ssh_bastion curl -o /root/prepare.sh \
|
||||
https://gitea.detee.cloud/general/examples/raw/branch/surreal_brain/surrealdb_tikv_prod/prepare_bastion.sh
|
||||
$ssh_bastion curl -o /root/prod_cluster.yaml \
|
||||
https://gitea.detee.cloud/general/examples/raw/branch/surreal_brain/surrealdb_tikv_prod/prod_cluster.yaml
|
||||
$ssh_bastion curl -o /root/staging_cluster.yaml \
|
||||
https://gitea.detee.cloud/general/examples/raw/branch/surreal_brain/surrealdb_tikv_prod/staging_cluster.yaml
|
||||
$ssh_bastion chmod +x /root/prepare.sh
|
||||
$ssh_bastion /root/prepare.sh
|
46
surrealdb_tikv_prod/deploy_nodes.sh
Executable file
46
surrealdb_tikv_prod/deploy_nodes.sh
Executable file
@ -0,0 +1,46 @@
|
||||
#!/bin/bash
|
||||
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
||||
cd $script_dir
|
||||
set -e
|
||||
export FORMAT=YAML
|
||||
mkdir -p tmp/
|
||||
|
||||
rm -rf tmp/overlay-network
|
||||
cp -r ../overlay-network tmp/
|
||||
rm -rf tmp/overlay-network/tmp
|
||||
cd tmp/overlay-network/vm_configs
|
||||
find . -maxdepth 1 -type f ! -name 'template-n.yaml' -exec rm -- '{}' +
|
||||
|
||||
setup_vm () {
|
||||
vm_name="$1"
|
||||
cp template-n.yaml $vm_name.yaml
|
||||
sed -i "s/template-n/$vm_name/" $vm_name.yaml
|
||||
sed -i '/PublishPorts/d' $vm_name.yaml
|
||||
echo "ipv4: !PublishPorts [ 31337 ]" >> $vm_name.yaml
|
||||
sed -i '/hours:/d' $vm_name.yaml
|
||||
echo "hours: 800" >> $vm_name.yaml
|
||||
country_options=(GB FR CA US)
|
||||
country=${country_options[RANDOM % ${#country_options[@]}]}
|
||||
[[ "$vm_name" == "bastion-brain" ]] && country="FR"
|
||||
sed -i '/location:/d' $vm_name.yaml
|
||||
sed -i '/country:/d' $vm_name.yaml
|
||||
echo "location:" >> $vm_name.yaml
|
||||
echo " country: \"$country\"" >> $vm_name.yaml
|
||||
}
|
||||
|
||||
setup_vm staging-brain-1
|
||||
setup_vm staging-brain-2
|
||||
setup_vm staging-brain-3
|
||||
setup_vm staging-brain-mon
|
||||
setup_vm prod-brain-1
|
||||
setup_vm prod-brain-2
|
||||
setup_vm prod-brain-3
|
||||
setup_vm prod-brain-mon
|
||||
setup_vm bastion-brain
|
||||
rm template-n.yaml
|
||||
|
||||
cd ../
|
||||
echo CREATING VMS!
|
||||
./create_vms.sh
|
||||
sleep 10
|
||||
./setup_wg_mesh.sh
|
33
surrealdb_tikv_prod/prepare_bastion.sh
Normal file
33
surrealdb_tikv_prod/prepare_bastion.sh
Normal file
@ -0,0 +1,33 @@
|
||||
#!/bin/bash
|
||||
curl -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
|
||||
|
||||
ssh prod-brain-1 -o StrictHostKeyChecking=false echo Hello!
|
||||
ssh prod-brain-2 -o StrictHostKeyChecking=false echo Hello!
|
||||
ssh prod-brain-3 -o StrictHostKeyChecking=false echo Hello!
|
||||
ssh prod-brain-mon -o StrictHostKeyChecking=false echo Hello!
|
||||
ssh staging-brain-1 -o StrictHostKeyChecking=false echo Hello!
|
||||
ssh staging-brain-2 -o StrictHostKeyChecking=false echo Hello!
|
||||
ssh staging-brain-3 -o StrictHostKeyChecking=false echo Hello!
|
||||
ssh staging-brain-mon -o StrictHostKeyChecking=false echo Hello!
|
||||
|
||||
mkdir -p /root/brain_logs/
|
||||
|
||||
ssh prod-brain-1 pacman -S sudo --noconfirm >> /root/brain_logs/prod-brain-1
|
||||
ssh prod-brain-2 pacman -S sudo --noconfirm >> /root/brain_logs/prod-brain-2
|
||||
ssh prod-brain-3 pacman -S sudo --noconfirm >> /root/brain_logs/prod-brain-3
|
||||
ssh prod-brain-mon pacman -S sudo --noconfirm >> /root/brain_logs/prod-brain-mon
|
||||
ssh staging-brain-1 pacman -S sudo --noconfirm >> /root/brain_logs/staging-brain-1
|
||||
ssh staging-brain-2 pacman -S sudo --noconfirm >> /root/brain_logs/staging-brain-2
|
||||
ssh staging-brain-3 pacman -S sudo --noconfirm >> /root/brain_logs/staging-brain-3
|
||||
ssh staging-brain-mon pacman -S sudo --noconfirm >> /root/brain_logs/straging-brain-mon
|
||||
|
||||
/root/.tiup/bin/tiup cluster deploy \
|
||||
staging-brain v8.5.1 /root/staging_cluster.yaml \
|
||||
--user root -i ~/.ssh/id_ed25519
|
||||
|
||||
/root/.tiup/bin/tiup cluster deploy \
|
||||
prod-brain v8.5.1 /root/prod_cluster.yaml \
|
||||
--user root -i ~/.ssh/id_ed25519
|
||||
|
||||
/root/.tiup/bin/tiup cluster start staging-brain --init
|
||||
/root/.tiup/bin/tiup cluster start prod-brain --init
|
24
surrealdb_tikv_prod/prod_cluster.yaml
Normal file
24
surrealdb_tikv_prod/prod_cluster.yaml
Normal file
@ -0,0 +1,24 @@
|
||||
global:
|
||||
user: "brain"
|
||||
ssh_port: 22
|
||||
deploy_dir: "/opt/brain_deployment"
|
||||
data_dir: "/opt/brain_data"
|
||||
|
||||
pd_servers:
|
||||
- host: prod-brain-1
|
||||
- host: prod-brain-2
|
||||
- host: prod-brain-3
|
||||
|
||||
tikv_servers:
|
||||
- host: prod-brain-1
|
||||
- host: prod-brain-2
|
||||
- host: prod-brain-3
|
||||
|
||||
monitoring_servers:
|
||||
- host: prod-brain-mon
|
||||
|
||||
grafana_servers:
|
||||
- host: prod-brain-mon
|
||||
|
||||
alertmanager_servers:
|
||||
- host: prod-brain-mon
|
24
surrealdb_tikv_prod/staging_cluster.yaml
Normal file
24
surrealdb_tikv_prod/staging_cluster.yaml
Normal file
@ -0,0 +1,24 @@
|
||||
global:
|
||||
user: "brain"
|
||||
ssh_port: 22
|
||||
deploy_dir: "/opt/brain_deployment"
|
||||
data_dir: "/opt/brain_data"
|
||||
|
||||
pd_servers:
|
||||
- host: staging-brain-1
|
||||
- host: staging-brain-2
|
||||
- host: staging-brain-3
|
||||
|
||||
tikv_servers:
|
||||
- host: staging-brain-1
|
||||
- host: staging-brain-2
|
||||
- host: staging-brain-3
|
||||
|
||||
monitoring_servers:
|
||||
- host: staging-brain-mon
|
||||
|
||||
grafana_servers:
|
||||
- host: staging-brain-mon
|
||||
|
||||
alertmanager_servers:
|
||||
- host: staging-brain-mon
|
Loading…
Reference in New Issue
Block a user