Skip to content

Commit 055358d

Browse files
committed
Merge branch 'main' into fix/webServerHealthChecks
2 parents bc7a53f + f3641e6 commit 055358d

File tree

109 files changed

+578
-359
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+578
-359
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Regra aplicada à tag `http-server` da rede `hivetown-external`, que permite à
5656
<summary>Linha de comandos equivalente</summary>
5757

5858
```bash
59-
gcloud compute --project=hivetown firewall-rules create hivetown-external-allow-http --direction=INGRESS --priority=1000 --network=hivetown-external --action=ALLOW --rules=tcp:80,tcp:433 --source-ranges=0.0.0.0/0 --target-tags=http-server
59+
gcloud compute --project=hivetown firewall-rules create hivetown-external-allow-http --direction=INGRESS --priority=1000 --network=hivetown-external --action=ALLOW --rules=tcp:80,tcp:443 --source-ranges=0.0.0.0/0 --target-tags=http-server
6060
```
6161
</details>
6262

@@ -74,9 +74,7 @@ gcloud compute --project=hivetown firewall-rules create hivetown-allow-ssh --des
7474
</details>
7575

7676
#### VRRP
77-
Regra aplicada à tag `vrrp` (origem e destino), que permite troca de mensagens do Keepalived para healthchecks.
78-
79-
Para isto foi permitido o protocolo 112 (código IANA para o VRRP).
77+
Ver [Keepalived Loadbalancers](../load-balancers/keepalived/README.md) e [Keepalived Bases de Dados](../databases/keepalived/README.md)
8078

8179
<details>
8280
<summary>Linha de comandos equivalente</summary>

databases/backup/.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
MYSQL_USER=hivetownbackup
2+
MYSQL_PASSWORD=hello
3+
DATABASE=hivetown
4+
BACKUP_DIR=backups
5+
MYSQL_HOST=10.0.128.3
6+
MYSQL_PORT=3306

databases/backup/backup.sh

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
#!/bin/bash
2+
# Load env
3+
set -a
4+
. /home/romul/.env
5+
set +a
26

3-
# Define as variáveis de ambiente necessárias para o backup
4-
MYSQL_USER=mybackupuser
5-
MYSQL_PASSWORD=mybackuppassword
6-
DATABASE=hivetown
7-
BACKUP_DIR=backups
8-
MYSQL_HOST=10.164.0.6
9-
MYSQL_PORT=3306
10-
11-
7+
backupDate=$(date +%Y-%m-%d_%H-%M-%S)
128

139
# Cria o nome do arquivo de backup com a data atual
14-
BACKUP_FILENAME=${DATABASE}_$(date +%Y-%m-%d_%H-%M-%S).sql
10+
BACKUP_FILENAME=${DATABASE}_$backupDate.sql
1511

1612
# Executa o mysqldump para fazer o backup dos bancos de dados no servidor master
17-
mysqldump -h $MYSQL_HOST -P $MYSQL_PORT -u $MYSQL_USER -p$MYSQL_PASSWORD $DATABASE > $BACKUP_DIR/$BACKUP_FILENAME
13+
mysqldump -h $MYSQL_HOST -P $MYSQL_PORT -u $MYSQL_USER -p$MYSQL_PASSWORD $DATABASE > $BACKUP_DIR/$BACKUP_FILENAME
1814

1915
# Exibe uma mensagem de sucesso
20-
echo "Backup concluído em $(date +%Y-%m-%d_%H:%M:%S)"
16+
echo "Backup concluído em $backupDate"

databases/db1/.env.example

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
MYSQL_USER=hivetownbackup
2+
MYSQL_PASSWORD=mybackuppassword
3+
DATABASE=hivetown
4+
5+
INSTANCE_NAME=vm-database-1
6+
INSTANCE_ZONE=europe-west4-a
7+
PEER_INSTANCE_NAME=vm-database-2
8+
PEER_INSTANCE_ZONE=europe-west4-b
9+
ALIAS_IP=10.0.128.10
10+
11+
BACKUP_USER=romul
12+
BACKUP_IP=10.0.112.2
13+
BACKUP_DIR="/home/romul/backups"
14+
BACKUP_DESTINATION="/home/romul/"

databases/db1/backup.sh

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
#!/bin/bash
2-
3-
# Define as variáveis de ambiente necessárias para o backup
4-
MYSQL_USER=mybackupuser
5-
MYSQL_PASSWORD=mybackuppassword
6-
DATABASE=hivetown
7-
2+
# Load env
3+
set -a
4+
. /home/romul/.env
5+
set +a
86

97
# Executa o mysqldump para fazer o backup dos bancos de dados no servidor master
10-
docker exec mysql-db-1 mysqldump -u $MYSQL_USER -p$MYSQL_PASSWORD $DATABASE > MasterDown.sql
8+
docker exec mysql-hivetown mysqldump -u $MYSQL_USER -p$MYSQL_PASSWORD $DATABASE > MasterDown.sql
119

1210
# Define o nome do arquivo SQL
1311
sql_file="MasterDown.sql"

databases/db1/bash.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/bash
22

3-
docker exec -it mysql-db-1 bash
3+
docker exec -it mysql-hivetown bash

databases/db1/execute.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/bash
22

3-
docker exec -i mysql-db-1 mysql -uroot -phello < dummy.sql
3+
docker exec -i mysql-hivetown mysql < dummy.sql

databases/db1/init_master.sh

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#!/bin/bash
2-
gcloud compute instances network-interfaces update vm-database-2 --zone europe-west4-b --aliases ""
3-
gcloud compute instances network-interfaces update vm-database-1 --zone europe-west4-a --aliases 10.0.128.10
2+
# Load env
3+
set -a
4+
. .env
5+
set +a
6+
7+
gcloud compute instances network-interfaces update $PEER_INSTANCE_NAME --zone $PEER_INSTANCE_ZONE --aliases ""
8+
gcloud compute instances network-interfaces update $INSTANCE_NAME --zone $INSTANCE_ZONE --aliases $ALIAS_IP
49

510
echo "A construir..."
611
source ./master/build.sh
@@ -10,10 +15,8 @@ source ./master/run.sh
1015
echo ""
1116

1217
sleep 3
13-
echo "Estado do mysql-db-1: "
18+
echo "Estado do mysql-hivetown: "
1419
docker ps -a
1520

16-
cp /home/romul/keepalived/keepalivedMASTER.conf /home/romul/keepalived/keepalived.conf &&
17-
sudo cp -R /home/romul/keepalived/keepalived.conf /etc/keepalived/ &&
18-
sudo cp -R /home/romul/keepalived/takeover.sh /etc/keepalived/ &&
19-
sudo systemctl restart keepalived
21+
export STATE=MASTER
22+
sudo bash /home/romul/keepalived/plug.sh

databases/db1/keepalived/.env.example

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Keepalived
2+
# Config
3+
UNICAST_SRC_IP=10.0.128.4
4+
UNICAST_PEER=10.0.128.3
5+
AUTH_PASS=123456
6+
STATE=MASTER
7+
8+
# Takeover
9+
INSTANCE_NAME=vm-database-1
10+
INSTANCE_ZONE=europe-west4-a
11+
PEER_INSTANCE_NAME=vm-database-2
12+
PEER_INSTANCE_ZONE=europe-west4-b
13+
ALIAS_IP=10.0.128.10

databases/db1/keepalived/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
keepalived.master.conf
2+
keepalived.backup.conf
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
# Load env
3+
set -a
4+
. .env
5+
set +a
6+
7+
export STATE=MASTER
8+
export PRIORITY=100
9+
envsubst < keepalived.template.conf > keepalived.master.conf
10+
11+
export STATE=BACKUP
12+
export PRIORITY=50
13+
envsubst < keepalived.template.conf > keepalived.backup.conf
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
vrrp_instance floating_ip {
22
interface ens4
3-
state MASTER
4-
unicast_src_ip 10.0.128.4
3+
state ${STATE}
4+
unicast_src_ip ${UNICAST_SRC_IP}
55
unicast_peer {
6-
10.0.128.3
6+
${UNICAST_PEER}
77
}
88
virtual_router_id 50
9-
priority 100
9+
priority ${PRIORITY}
1010
advert_int 1
1111
authentication {
1212
auth_type PASS
13-
auth_pass 123456
13+
auth_pass ${AUTH_PASS}
1414
}
1515
notify_master "bash /etc/keepalived/takeover.sh"
1616
}

databases/db1/keepalived/keepalivedSLAVE.conf

Lines changed: 0 additions & 16 deletions
This file was deleted.

databases/db1/keepalived/plug.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
# Define o nome do container
4+
container_name="mysql-hivetown"
5+
6+
# Choose which keepalived.conf to copy
7+
toCopy="keepalived.backup.conf"
8+
if docker ps -a | grep -q $container_name; then
9+
10+
# Obtém o nome da imagem do contêiner
11+
image=$(docker inspect -f '{{.Config.Image}}' $container_name)
12+
13+
if [ "$image" == "mysql-master-image" ]; then
14+
toCopy="keepalived.master.conf"
15+
fi
16+
fi
17+
18+
# Copy needed files to /etc/keepalived
19+
sudo cp /home/romul/keepalived/$toCopy /etc/keepalived/keepalived.conf
20+
sudo cp /home/romul/keepalived/.env /etc/keepalived/.env
21+
sudo cp /home/romul/keepalived/takeover.sh /etc/keepalived/takeover.sh
22+
23+
# Restart keepalived
24+
sudo systemctl restart keepalived

databases/db1/keepalived/takeover.sh

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#!/bin/bash
2+
# Load env
3+
set -a
4+
. /etc/keepalived/.env
5+
set +a
26

37
# Define o nome do container
4-
container_name="mysql-db-1"
8+
container_name="mysql-hivetown"
59

610
if docker ps -a | grep -q $container_name; then
711

@@ -15,15 +19,14 @@ if docker ps -a | grep -q $container_name; then
1519
if [ "$state" = "BACKUP" ]; then
1620
echo "A INICIAR O MASTER!"
1721

18-
gcloud compute instances network-interfaces update vm-database-2 --zone europe-west4-b --aliases "" >> /home/romul/log.txt 2>&1
19-
gcloud compute instances network-interfaces update vm-database-1 --zone europe-west4-a --aliases 10.0.128.10
20-
22+
gcloud compute instances network-interfaces update $PEER_INSTANCE_NAME --zone $PEER_INSTANCE_ZONE --aliases "" >> /home/romul/log.txt 2>&1
23+
gcloud compute instances network-interfaces update $INSTANCE_NAME --zone $INSTANCE_ZONE --aliases $ALIAS_IP
2124

2225
# Mudança da configuração do keepalived
23-
cp /home/romul/keepalived/keepalivedMASTER.conf /home/romul/keepalived/keepalived.conf
24-
sudo cp -R /home/romul/keepalived/keepalived.conf /etc/keepalived/
26+
sudo cp /home/romul/keepalived/keepalived.master.conf /etc/keepalived/keepalived.conf
2527
sudo systemctl restart keepalived
2628
fi
29+
2730
# Mudança da configuração do mysql para master
2831
source /home/romul/newMaster.sh
2932
fi

databases/db1/master/.env.example

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
MYSQL_ROOT_PASSWORD=hello
2+
MYSQL_SERVER_ID=1
3+
MYSQL_LOG_BIN=mysql-bin
4+
MYSQL_BINLOG_DO_DB=hivetown
5+
6+
# Init master
7+
MYSQL_REPLICATOR_USER=hivetownreplicator
8+
MYSQL_REPLICATOR_PASSWORD=hello
9+
MYSQL_BACKUP_USER=hivetownbackup
10+
MYSQL_BACKUP_PASSWORD=mybackuppassword

databases/db1/master/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
init_master.sql
2+
my.cnf

databases/db1/master/Dockerfile

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
FROM mysql:latest
22

3-
# Configuração do servidor MySQL master
4-
ENV MYSQL_ROOT_PASSWORD=hello
5-
ENV MYSQL_SERVER_ID=1
6-
ENV MYSQL_LOG_BIN=mysql-bin
7-
ENV MYSQL_BINLOG_DO_DB=hivetown
8-
93
# Copia o arquivo my.cnf para o container
104
COPY my.cnf /etc/mysql/conf.d/my.cnf
115

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
# Load env
3+
set -a
4+
. .env
5+
set +a
6+
7+
envsubst < init_master.template.sql > init_master.sql
8+
envsubst < my.template.cnf > my.cnf

databases/db1/master/init_master.sql

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE USER '${MYSQL_REPLICATOR_USER}'@'%' IDENTIFIED WITH mysql_native_password BY '${MYSQL_REPLICATOR_PASSWORD}'; -- Cria um user para funcionar como replica
2+
GRANT REPLICATION SLAVE ON *.* TO '${MYSQL_REPLICATOR_USER}'@'%'; -- Concede privilégios de replicação ao user
3+
FLUSH PRIVILEGES; -- Recarrrega as tabelas de privilégios
4+
5+
CREATE USER '${MYSQL_BACKUP_USER}'@'%' IDENTIFIED WITH mysql_native_password BY '${MYSQL_BACKUP_PASSWORD}'; -- Cria um user para funcionar como backup
6+
GRANT ALL PRIVILEGES ON hivetown.* TO '${MYSQL_BACKUP_USER}'@'%';
7+
GRANT PROCESS ON *.* TO '${MYSQL_BACKUP_USER}'@'%';
8+
FLUSH PRIVILEGES; -- Recarrega as tabelas de privilégios

databases/db1/master/my.cnf

Lines changed: 0 additions & 4 deletions
This file was deleted.

databases/db1/master/my.template.cnf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[mysqld]
2+
server-id=${MYSQL_SERVER_ID}
3+
log-bin=${MYSQL_LOG_BIN}
4+
binlog-do-db=${MYSQL_BINLOG_DO_DB}

databases/db1/master/run.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
#!/bin/bash
2-
3-
docker run --name mysql-db-1 -e MYSQL_ROOT_PASSWORD=hello -d -p 3306:3306 mysql-master-image
2+
docker run --name mysql-hivetown --env-file=/home/romul/master/.env -d -p 3306:3306 mysql-master-image

databases/db1/master/status.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
#!/bin/bash
2-
3-
docker exec -it mysql-db-1 mysql -uroot -phello -e "SHOW MASTER STATUS\G"
2+
docker exec -it mysql-hivetown mysql -e "SHOW MASTER STATUS\G"

databases/db1/newMaster.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ echo "Aguardar 5 segundos para atualizar a base de dados com o último backup...
2323
sleep 5
2424

2525
# Executa o ficheiro sql para atualizar o master
26-
while ! docker exec -i mysql-db-1 mysql -uroot -phello -e "CREATE DATABASE hivetown;"; do
26+
while ! docker exec -i mysql-hivetown mysql -e "CREATE DATABASE hivetown;"; do
2727
sleep 2
2828
echo "A tentar novamente..."
2929
done
30-
docker exec -i mysql-db-1 mysql -uroot -phello hivetown < MasterDown.sql;
30+
docker exec -i mysql-hivetown mysql hivetown < MasterDown.sql;
3131

3232

3333

databases/db1/newSlave.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ source /home/romul/slave/run.sh
1616
echo ""
1717

1818
sleep 3
19-
echo "Estado do mysql-db-1: "
19+
echo "Estado do mysql-hivetown: "
2020
docker ps -a
2121

2222
echo ""

databases/db1/remove.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
#!/bin/bash
2-
3-
docker rm mysql-db-1
2+
docker rm mysql-hivetown

0 commit comments

Comments
 (0)