217 lines
4.7 KiB
Markdown
217 lines
4.7 KiB
Markdown
# Despliegue en Google Cloud
|
|
|
|
Esta guía explica cómo desplegar SnatchGame en Google Cloud Platform usando una VM de Compute Engine.
|
|
|
|
## Requisitos Previos
|
|
|
|
1. **Cuenta de Google Cloud** con billing habilitado
|
|
2. **gcloud CLI** instalado y configurado
|
|
3. **Proyecto de Google Cloud** creado
|
|
4. **Dominio** configurado en Cloudflare
|
|
|
|
## Paso 1: Configurar Google Cloud
|
|
|
|
### Instalar gcloud CLI
|
|
```bash
|
|
# En tu máquina local
|
|
curl https://sdk.cloud.google.com | bash
|
|
exec -l $SHELL
|
|
gcloud init
|
|
```
|
|
|
|
### Configurar proyecto
|
|
```bash
|
|
# Listar proyectos disponibles
|
|
gcloud projects list
|
|
|
|
# Configurar proyecto activo
|
|
gcloud config set project TU_PROJECT_ID
|
|
|
|
# Habilitar APIs necesarias
|
|
gcloud services enable compute.googleapis.com
|
|
```
|
|
|
|
## Paso 2: Crear la VM
|
|
|
|
### Editar configuración
|
|
Edita `deploy/gcloud/create-vm.sh` y cambia:
|
|
```bash
|
|
PROJECT_ID="tu-proyecto-gcloud" # Por tu project ID real
|
|
```
|
|
|
|
### Ejecutar creación
|
|
```bash
|
|
cd deploy/gcloud
|
|
chmod +x *.sh
|
|
./create-vm.sh
|
|
```
|
|
|
|
Esto creará:
|
|
- VM `snatchgame-vm` con Ubuntu 20.04
|
|
- Tipo `e2-micro` (1 vCPU, 1GB RAM)
|
|
- Disco de 20GB
|
|
- Nginx configurado automáticamente
|
|
- Docker instalado
|
|
- Tu aplicación desplegada
|
|
|
|
## Paso 3: Configurar DNS en Cloudflare
|
|
|
|
Una vez creada la VM, obtendrás una IP externa. Configura en Cloudflare:
|
|
|
|
```
|
|
Type: A
|
|
Name: snatchgame
|
|
Content: IP_EXTERNA_DE_LA_VM
|
|
Proxy status: Proxied (naranja)
|
|
TTL: Auto
|
|
```
|
|
|
|
## Paso 4: Configurar SSL en Cloudflare
|
|
|
|
En el dashboard de Cloudflare:
|
|
|
|
1. **SSL/TLS → Overview**
|
|
- Encryption mode: `Flexible` o `Full`
|
|
|
|
2. **SSL/TLS → Edge Certificates**
|
|
- Always Use HTTPS: `On`
|
|
- HTTP Strict Transport Security: `On`
|
|
|
|
## Paso 5: Verificar Deployment
|
|
|
|
### Verificar VM
|
|
```bash
|
|
# Conectarse a la VM
|
|
gcloud compute ssh snatchgame-vm --zone=us-central1-a
|
|
|
|
# Verificar servicios
|
|
sudo systemctl status nginx
|
|
sudo systemctl status docker
|
|
docker ps
|
|
|
|
# Ver logs
|
|
docker logs snatchgame
|
|
```
|
|
|
|
### Verificar URLs
|
|
- `http://snatchgame.nucleoriofrio.com` - Frontend
|
|
- `http://snatchgame.nucleoriofrio.com/colyseus` - Monitor
|
|
|
|
## Paso 6: Configurar Deployment Automático (Opcional)
|
|
|
|
### Crear Service Account
|
|
```bash
|
|
# Crear service account
|
|
gcloud iam service-accounts create gitea-deployer \
|
|
--display-name="Gitea Deployer"
|
|
|
|
# Asignar permisos
|
|
gcloud projects add-iam-policy-binding TU_PROJECT_ID \
|
|
--member="serviceAccount:gitea-deployer@TU_PROJECT_ID.iam.gserviceaccount.com" \
|
|
--role="roles/compute.instanceAdmin"
|
|
|
|
# Crear key
|
|
gcloud iam service-accounts keys create key.json \
|
|
--iam-account=gitea-deployer@TU_PROJECT_ID.iam.gserviceaccount.com
|
|
```
|
|
|
|
### Configurar secrets en Gitea
|
|
En tu repositorio de Gitea, agrega estos secrets:
|
|
- `GCLOUD_SERVICE_KEY`: Contenido del archivo `key.json`
|
|
- `GCLOUD_PROJECT_ID`: Tu project ID
|
|
|
|
### Activar workflow
|
|
El archivo `.gitea/workflows/deploy-gcloud.yml` se ejecutará automáticamente cuando pushees a la rama `production`.
|
|
|
|
## Actualización Manual
|
|
|
|
Para actualizar manualmente:
|
|
```bash
|
|
cd deploy/gcloud
|
|
./update-deployment.sh
|
|
```
|
|
|
|
## Monitoreo y Logs
|
|
|
|
### Ver logs de la aplicación
|
|
```bash
|
|
gcloud compute ssh snatchgame-vm --zone=us-central1-a
|
|
docker logs -f snatchgame
|
|
```
|
|
|
|
### Ver logs de nginx
|
|
```bash
|
|
gcloud compute ssh snatchgame-vm --zone=us-central1-a
|
|
sudo tail -f /var/log/nginx/access.log
|
|
sudo tail -f /var/log/nginx/error.log
|
|
```
|
|
|
|
### Monitoreo de recursos
|
|
```bash
|
|
# Conectarse a la VM
|
|
gcloud compute ssh snatchgame-vm --zone=us-central1-a
|
|
|
|
# Ver uso de recursos
|
|
htop
|
|
docker stats
|
|
```
|
|
|
|
## Costos Estimados
|
|
|
|
- **VM e2-micro**: ~$7/mes
|
|
- **Disco 20GB**: ~$0.80/mes
|
|
- **Tráfico de red**: ~$0.12/GB
|
|
- **Total estimado**: ~$8-10/mes
|
|
|
|
## Troubleshooting
|
|
|
|
### VM no responde
|
|
```bash
|
|
# Reiniciar VM
|
|
gcloud compute instances reset snatchgame-vm --zone=us-central1-a
|
|
|
|
# Verificar startup script
|
|
gcloud compute ssh snatchgame-vm --zone=us-central1-a
|
|
sudo tail -f /var/log/syslog
|
|
```
|
|
|
|
### Aplicación no funciona
|
|
```bash
|
|
# Conectarse a VM
|
|
gcloud compute ssh snatchgame-vm --zone=us-central1-a
|
|
|
|
# Verificar contenedor
|
|
docker ps
|
|
docker logs snatchgame
|
|
|
|
# Redesplegar
|
|
sudo /opt/deploy-snatchgame.sh
|
|
```
|
|
|
|
### DNS no resuelve
|
|
- Verificar configuración en Cloudflare
|
|
- Esperar propagación DNS (puede tomar hasta 24h)
|
|
- Usar herramientas como `dig` o `nslookup`
|
|
|
|
## Scaling y Optimización
|
|
|
|
### Aumentar recursos de VM
|
|
```bash
|
|
# Parar VM
|
|
gcloud compute instances stop snatchgame-vm --zone=us-central1-a
|
|
|
|
# Cambiar tipo de máquina
|
|
gcloud compute instances set-machine-type snatchgame-vm \
|
|
--machine-type=e2-small --zone=us-central1-a
|
|
|
|
# Iniciar VM
|
|
gcloud compute instances start snatchgame-vm --zone=us-central1-a
|
|
```
|
|
|
|
### Backup automático
|
|
```bash
|
|
# Crear snapshot del disco
|
|
gcloud compute disks snapshot snatchgame-vm \
|
|
--zone=us-central1-a \
|
|
--snapshot-names=snatchgame-backup-$(date +%Y%m%d)
|
|
``` |