Workshop completo: aulas 08-10 com Talos, n8n e GitLab na Hetzner

Aula 08 - Cluster Kubernetes HA:
- Setup interativo com OpenTofu para Talos na Hetzner
- CCM, CSI Driver, Cluster Autoscaler, Metrics Server
- NGINX Ingress com LoadBalancer (HTTP/HTTPS/SSH)

Aula 09 - n8n na Hetzner:
- Deploy via Helm com PostgreSQL e Redis
- Suporte multi-tenant com add-client.sh
- Integração com Hetzner CSI para volumes persistentes

Aula 10 - GitLab na Hetzner:
- Setup agnóstico: CloudFlare (trusted proxies) ou Let's Encrypt
- Anti-affinity para distribuir webservice/sidekiq em nós diferentes
- Container Registry e SSH via TCP passthrough
- Documentação do erro 422 e solução com trustedCIDRsForXForwardedFor

Melhorias gerais:
- READMEs atualizados com arquitetura e troubleshooting
- Scripts cleanup.sh para todas as aulas
- CLAUDE.md atualizado com contexto do projeto
This commit is contained in:
Allyson de Paula
2025-12-31 17:57:02 -03:00
parent 50dc74c1d8
commit 07b7ee62d3
35 changed files with 4665 additions and 311 deletions

339
aula-10/README.md Normal file
View File

@@ -0,0 +1,339 @@
# Aula 10 - GitLab via Helm (Cluster Hetzner Cloud)
Deploy do GitLab em Kubernetes usando Helm chart oficial, com Container Registry, SSH e TLS configurável.
## Arquitetura
```
Hetzner LoadBalancer
┌───────────────────┼───────────────────┐
│ │ │
:80/:443 :22 │
│ │ │
▼ ▼ │
┌───────────────┐ ┌─────────────┐ │
│ NGINX Ingress │ │ TCP Service │ │
└───────┬───────┘ └──────┬──────┘ │
│ │ │
▼ ▼ │
┌─────────────────────────────────────────┐ │
│ GitLab (namespace: gitlab) │ │
│ ┌──────────┐ ┌────────┐ ┌─────────┐ │ │
│ │Webservice│ │Sidekiq │ │ Shell │◄─┼─────┘
│ │ (Rails) │ │ (Jobs) │ │ (SSH) │ │
│ └────┬─────┘ └───┬────┘ └─────────┘ │
│ │ │ │
│ ┌────▼────────────▼────┐ │
│ │ Gitaly │ │
│ │ (Git Storage) │ │
│ └──────────────────────┘ │
│ │
│ ┌──────────┐ ┌───────┐ ┌──────────┐ │
│ │PostgreSQL│ │ Redis │ │ MinIO │ │
│ │ (10Gi) │ │(10Gi) │ │ (10Gi) │ │
│ └──────────┘ └───────┘ └──────────┘ │
└─────────────────────────────────────────┘
```
## Pré-requisitos
1. **Cluster Talos na Hetzner** (aula-08) com:
- Hetzner CSI Driver (StorageClass: hcloud-volumes)
- NGINX Ingress Controller com LoadBalancer
2. **kubectl** instalado
3. **Helm** 3.x instalado
## Contexto do Cluster
Esta aula usa o cluster Kubernetes provisionado na **aula-08**. O `kubeconfig` foi gerado automaticamente pelo OpenTofu.
```bash
# Verificar se o cluster está acessível
export KUBECONFIG=$(pwd)/../aula-08/kubeconfig
kubectl cluster-info
```
## Instalação
```bash
cd aula-10
# Configurar acesso ao cluster (kubeconfig da aula-08)
export KUBECONFIG=$(pwd)/../aula-08/kubeconfig
# Executar setup interativo
chmod +x setup.sh
./setup.sh
```
O script é interativo e pergunta:
1. **Hostname do GitLab** (ex: `git.kube.quest`) - FQDN completo
2. **Usa CloudFlare?** (com proxy/CDN) - pode herdar da aula-09
3. **Ativar Let's Encrypt?** (se não usar CloudFlare)
### Opções de TLS
| Cenário | TLS Provider | Configuração |
|---------|--------------|--------------|
| **CloudFlare (proxy)** | CloudFlare Edge | Sem cert-manager, TLS na borda |
| **Outro DNS + Let's Encrypt** | cert-manager | HTTP-01 challenge automático |
| **Outro DNS + HTTP** | Nenhum | Apenas HTTP (dev/workshop) |
### CloudFlare e Trusted Proxies (Erro 422)
Quando você usa CloudFlare com proxy ativo (ícone laranja), o tráfego passa pelos servidores do CloudFlare antes de chegar ao GitLab:
```
Usuário → CloudFlare (TLS) → LoadBalancer → NGINX → GitLab
Adiciona headers:
X-Forwarded-For: IP-do-usuario
X-Forwarded-Proto: https
```
**Problema:** Por padrão, o GitLab (Workhorse) não confia nesses headers. Resultado:
- GitLab pensa que a requisição veio via HTTP (não HTTPS)
- CSRF token é gerado para HTTPS mas validado como HTTP
- **Erro 422: "The change you requested was rejected"**
**Solução:** O `setup.sh` configura automaticamente os IPs do CloudFlare como proxies confiáveis:
```yaml
gitlab.webservice.workhorse.trustedCIDRsForXForwardedFor:
- 173.245.48.0/20 # CloudFlare
- 103.21.244.0/22
- 103.22.200.0/22
# ... (todos os CIDRs do CloudFlare)
```
Com isso, o Workhorse confia nos headers `X-Forwarded-*` vindos do CloudFlare e o login funciona corretamente.
> **Lição:** Proxies reversos (CloudFlare, NGINX, HAProxy) adicionam headers para preservar informações do cliente original. O backend precisa ser configurado para confiar nesses headers, caso contrário terá problemas de protocolo e autenticação.
## Componentes Instalados
**Da aula-08 (infraestrutura):**
- Hetzner CSI Driver (StorageClass: hcloud-volumes)
- NGINX Ingress Controller com LoadBalancer
**Instalados por este script:**
- **cert-manager** (se Let's Encrypt ativado)
- **GitLab** com todos os componentes
| Componente | Memory Request | Memory Limit | Volume |
|------------|----------------|--------------|--------|
| Webservice | 2Gi | 2.5Gi | - |
| Sidekiq | 1.5Gi | 2Gi | - |
| Gitaly | 512Mi | 1Gi | 10Gi |
| PostgreSQL | 512Mi | 1Gi | 10Gi |
| Redis | 256Mi | 512Mi | 10Gi |
| MinIO | 128Mi | 256Mi | 10Gi |
| Shell | 64Mi | 128Mi | - |
| Toolbox | 256Mi | 512Mi | - |
| **Total** | ~5Gi | ~8Gi | 40Gi |
## Pod Anti-Affinity (Distribuicao Inteligente)
O GitLab configura **antiAffinity** para garantir que webservice e sidekiq rodem em nos diferentes:
```yaml
gitlab:
webservice:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: sidekiq
topologyKey: kubernetes.io/hostname
```
### Por que isso importa?
Sem antiAffinity, webservice (~2.5Gi) + sidekiq (~2Gi) = 4.5Gi no mesmo no CAX11 (4GB):
- **Resultado**: OOMKilled constantes, servico instavel
Com antiAffinity:
- Kubernetes detecta que nao pode agendar no mesmo no
- Pod fica **Pending**
- Cluster Autoscaler cria um novo no automaticamente
- Pods distribuidos = servico estavel
### Licao do Workshop
> "Kubernetes nao e so rodar containers - e **orquestracao inteligente**.
> Quando configuramos antiAffinity, o scheduler entendeu que precisava de mais
> recursos e o autoscaler provisionou automaticamente."
## Padrão de Domínios
| Serviço | Domínio |
|---------|---------|
| Web UI | {GITLAB_HOST} (ex: git.kube.quest) |
| Registry | registry.{DOMAIN} (ex: registry.kube.quest) |
| SSH | {GITLAB_HOST}:22 |
Exemplo com hostname `git.kube.quest`:
- https://git.kube.quest
- https://registry.kube.quest
- git@git.kube.quest
## Arquivo de Configuração
O `setup.sh` gera um arquivo `.env` (herda configuração de TLS da aula-09):
```bash
# aula-10/.env (gerado automaticamente)
GITLAB_HOST=git.kube.quest
REGISTRY_HOST=registry.kube.quest
DOMAIN=kube.quest
USE_CLOUDFLARE=true
USE_LETSENCRYPT=false
LETSENCRYPT_EMAIL=
```
## Acesso
### Web UI
```
URL: https://git.{domain}
Usuário: root
Senha: (ver abaixo)
```
### Obter senha do root
```bash
kubectl get secret gitlab-gitlab-initial-root-password \
-n gitlab \
-o jsonpath='{.data.password}' | base64 -d; echo
```
### SSH
```bash
# Clonar repositório via SSH
git clone git@git.kube.quest:grupo/projeto.git
# Configurar chave SSH no GitLab
# Settings → SSH Keys → Adicionar sua chave pública
```
### Container Registry
```bash
# Login no registry
docker login registry.kube.quest
# Push de imagem
docker tag minha-app:v1 registry.kube.quest/grupo/projeto:v1
docker push registry.kube.quest/grupo/projeto:v1
```
## Comandos Úteis
```bash
# Ver todos os pods
kubectl get pods -n gitlab
# Ver logs do webservice
kubectl logs -n gitlab -l app=webservice -f
# Ver logs do sidekiq
kubectl logs -n gitlab -l app=sidekiq -f
# Reiniciar componente
kubectl rollout restart deployment -n gitlab gitlab-webservice-default
# Rails console
kubectl exec -it -n gitlab \
$(kubectl get pod -n gitlab -l app=toolbox -o name | head -1) \
-- gitlab-rails console
# Backup
kubectl exec -it -n gitlab \
$(kubectl get pod -n gitlab -l app=toolbox -o name | head -1) \
-- backup-utility
# Ver certificados (se Let's Encrypt)
kubectl get certificate -n gitlab
# Desinstalar (apenas GitLab)
./cleanup.sh
```
**Nota:** O `cleanup.sh` remove apenas GitLab, clientes e cert-manager. A infraestrutura (CSI Driver, NGINX Ingress, LoadBalancer) é mantida pois pertence à aula-08.
## Configurações Adicionais
### Adicionar GitLab Runner
```bash
# Obter registration token
kubectl get secret gitlab-gitlab-runner-secret \
-n gitlab \
-o jsonpath='{.data.runner-registration-token}' | base64 -d; echo
# Instalar runner
helm install gitlab-runner gitlab/gitlab-runner \
--namespace gitlab \
--set gitlabUrl=https://git.kube.quest \
--set runnerRegistrationToken=<token>
```
## Troubleshooting
### Pods não iniciam
```bash
# Verificar eventos
kubectl get events -n gitlab --sort-by='.lastTimestamp'
# Verificar PVCs
kubectl get pvc -n gitlab
# Verificar se CSI driver está funcionando
kubectl get pods -n kube-system -l app=hcloud-csi
```
### SSH não conecta
```bash
# Verificar se porta 22 está no ConfigMap
kubectl get configmap tcp-services -n ingress-nginx -o yaml
# Verificar gitlab-shell pod
kubectl get pods -n gitlab -l app=gitlab-shell
```
### Registry não funciona
```bash
# Verificar pod do registry
kubectl get pods -n gitlab -l app=registry
kubectl logs -n gitlab -l app=registry
# Testar internamente
kubectl run test --rm -it --image=busybox -- wget -qO- http://gitlab-registry:5000/v2/
```
## Custos
| Recurso | Custo/mês |
|---------|-----------|
| Workers (2x CAX11 - antiAffinity) | ~$9.18 |
| Volumes (4x 10Gi = 40Gi) | ~$1.94 |
| LoadBalancer (compartilhado) | ~$1.80 (1/3) |
| **Total GitLab** | ~$12.92 |
**Nota:** O antiAffinity requer 2 workers minimos (webservice e sidekiq em nos separados).
O Cluster Autoscaler provisiona automaticamente quando necessario.
## Referências
- [GitLab Helm Chart](https://docs.gitlab.com/charts/)
- [GitLab Helm Chart Values](https://gitlab.com/gitlab-org/charts/gitlab/-/blob/master/values.yaml)
- [External Ingress](https://docs.gitlab.com/charts/advanced/external-ingress/)
- [NGINX TCP Services](https://kubernetes.github.io/ingress-nginx/user-guide/exposing-tcp-udp-services/)

184
aula-10/cleanup.sh Executable file
View File

@@ -0,0 +1,184 @@
#!/bin/bash
# =============================================================================
# Cleanup da Aula 10 - Remove GitLab
# =============================================================================
#
# Este script remove:
# - GitLab (Helm release)
# - PVCs (dados persistentes)
# - Secrets
# - Namespace gitlab
# - cert-manager (se instalado por esta aula)
# - Arquivo .env
#
# NÃO remove (gerenciados pela aula-08):
# - NGINX Ingress Controller
# - Hetzner CSI Driver
# - LoadBalancer
#
# ATENÇÃO: Todos os dados do GitLab serão perdidos!
#
# =============================================================================
set -e
# Cores para output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[OK]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERRO]${NC} $1"; }
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo ""
echo -e "${CYAN}============================================${NC}"
echo -e "${CYAN} Cleanup - Aula 10 (GitLab)${NC}"
echo -e "${CYAN}============================================${NC}"
echo ""
log_warn "ATENÇÃO: Isso vai remover os recursos da aula 10:"
echo " - GitLab e todos os componentes"
echo " - Volumes persistentes (PostgreSQL, Redis, MinIO, Gitaly)"
echo " - cert-manager (se instalado)"
echo " - Arquivo .env"
echo ""
echo "Mantido (aula-08):"
echo " - NGINX Ingress Controller"
echo " - Hetzner CSI Driver"
echo " - LoadBalancer"
echo ""
read -p "Continuar? (digite 'sim' para confirmar): " confirm
if [ "$confirm" != "sim" ]; then
log_info "Operação cancelada"
exit 0
fi
echo ""
# =============================================================================
# 1. REMOVER TCP SERVICES DO NGINX
# =============================================================================
log_info "=== Removendo configuração TCP do NGINX ==="
if kubectl get configmap tcp-services -n ingress-nginx &> /dev/null; then
kubectl patch configmap tcp-services \
-n ingress-nginx \
--type json \
-p '[{"op": "remove", "path": "/data/22"}]' 2>/dev/null || true
log_success "Configuração TCP removida"
else
log_info "ConfigMap tcp-services não encontrado"
fi
# =============================================================================
# 2. DESINSTALAR GITLAB
# =============================================================================
log_info "=== Removendo GitLab ==="
if helm status gitlab -n gitlab &> /dev/null; then
log_info "Desinstalando GitLab via Helm..."
helm uninstall gitlab -n gitlab --wait 2>/dev/null || true
log_success "GitLab removido"
else
log_info "GitLab não está instalado"
fi
# Remover PVCs
if kubectl get pvc -n gitlab &> /dev/null 2>&1; then
log_info "Removendo PVCs do namespace gitlab..."
kubectl delete pvc --all -n gitlab --wait=false 2>/dev/null || true
fi
# Aguardar PVs serem liberados
sleep 5
# Remover secrets restantes
log_info "Removendo secrets..."
kubectl delete secret --all -n gitlab 2>/dev/null || true
# Remover namespace
if kubectl get namespace gitlab &> /dev/null; then
log_info "Removendo namespace gitlab..."
kubectl delete namespace gitlab --wait=false 2>/dev/null || true
log_success "Namespace gitlab removido"
fi
# =============================================================================
# 3. REMOVER CERT-MANAGER (se instalado)
# =============================================================================
log_info "=== Verificando cert-manager ==="
if helm status cert-manager -n cert-manager &> /dev/null; then
log_info "Removendo cert-manager..."
# Remover ClusterIssuer primeiro
kubectl delete clusterissuer --all 2>/dev/null || true
# Remover helm release
helm uninstall cert-manager -n cert-manager --wait 2>/dev/null || true
# Remover CRDs
kubectl delete crd \
certificates.cert-manager.io \
certificaterequests.cert-manager.io \
challenges.acme.cert-manager.io \
clusterissuers.cert-manager.io \
issuers.cert-manager.io \
orders.acme.cert-manager.io \
2>/dev/null || true
# Remover namespace
kubectl delete namespace cert-manager --wait=false 2>/dev/null || true
log_success "cert-manager removido"
else
log_info "cert-manager não está instalado"
fi
# =============================================================================
# 4. REMOVER ARQUIVO .env
# =============================================================================
log_info "=== Removendo arquivo de configuração ==="
if [ -f "$SCRIPT_DIR/.env" ]; then
rm -f "$SCRIPT_DIR/.env"
log_success "Arquivo .env removido"
else
log_info "Arquivo .env não existe"
fi
# =============================================================================
# RESUMO
# =============================================================================
echo ""
echo -e "${CYAN}============================================${NC}"
echo -e "${GREEN} Cleanup Concluído!${NC}"
echo -e "${CYAN}============================================${NC}"
echo ""
echo "Removido:"
echo " - GitLab e todos os componentes"
echo " - cert-manager (se existia)"
echo " - Arquivo .env"
echo ""
echo "Mantido (aula-08):"
echo " - NGINX Ingress Controller"
echo " - Hetzner CSI Driver"
echo " - LoadBalancer"
echo " - Cluster Kubernetes"
echo ""
log_warn "Volumes Hetzner podem demorar alguns minutos para serem"
log_warn "completamente removidos. Verifique no painel da Hetzner."
echo ""

298
aula-10/gitlab-values.yaml Normal file
View File

@@ -0,0 +1,298 @@
# =============================================================================
# GitLab Helm Chart - Configuração Base para Hetzner CAX11
# =============================================================================
#
# Esta configuração:
# - Usa NGINX Ingress Controller externo (instalado na aula-08)
# - Define ~5GB de recursos distribuídos em 2 workers CAX11 (antiAffinity)
# - Desabilita componentes não essenciais para economizar recursos
# - Configura Registry para container images
#
# Valores dinâmicos (configurados via --set no setup.sh):
# - global.hosts.domain
# - global.hosts.gitlab.name
# - global.hosts.registry.name
# - global.hosts.minio.name
# - global.hosts.https
# - global.ingress.tls.enabled
# - global.ingress.configureCertmanager
#
# Cenário CloudFlare (proxy ativo):
# - global.ingress.tls.enabled=false
# - global.hosts.https=true
# - gitlab.webservice.workhorse.trustedCIDRsForXForwardedFor=[CloudFlare IPs]
#
# Cenário Let's Encrypt:
# - global.ingress.tls.enabled=true
# - global.hosts.https=true
# - global.ingress.configureCertmanager=true
# - global.ingress.annotations.cert-manager.io/cluster-issuer=letsencrypt-prod
#
# =============================================================================
global:
# Usar Ingress Controller externo
ingress:
class: nginx
# configureCertmanager é definido via --set no setup.sh (true para Let's Encrypt)
annotations:
nginx.ingress.kubernetes.io/proxy-body-size: "0"
nginx.ingress.kubernetes.io/proxy-read-timeout: "900"
nginx.ingress.kubernetes.io/proxy-connect-timeout: "900"
nginx.ingress.kubernetes.io/proxy-buffering: "off"
# cert-manager.io/cluster-issuer é adicionado via --set quando Let's Encrypt
# Timezone
time_zone: America/Sao_Paulo
# Pod Security - evitar warnings de PodSecurity
pod:
securityContext:
runAsNonRoot: true
seccompProfile:
type: RuntimeDefault
# Email (opcional - configurar depois)
# email:
# from: gitlab@kube.quest
# display_name: GitLab
# reply_to: noreply@kube.quest
# =============================================================================
# NGINX BUNDLED - DESABILITAR (usamos o externo)
# =============================================================================
nginx-ingress:
enabled: false
# =============================================================================
# CERT-MANAGER - DESABILITAR BUNDLED
# =============================================================================
# O cert-manager é instalado separadamente se Let's Encrypt for escolhido.
# global.ingress.configureCertmanager controla a integração.
# =============================================================================
# GITLAB COMPONENTS
# =============================================================================
gitlab:
# Webservice (Rails app - UI e API)
# NOTA: antiAffinity garante que webservice e sidekiq rodem em nós diferentes
# Isso evita OOM quando ambos competem por memória no mesmo nó CAX11 (4GB)
webservice:
minReplicas: 1
maxReplicas: 1
resources:
requests:
memory: 2Gi
cpu: 200m
limits:
memory: 2.5Gi
cpu: 1
workerProcesses: 1
puma:
threads:
min: 1
max: 2
# Anti-affinity: não rodar no mesmo nó que sidekiq
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: sidekiq
topologyKey: kubernetes.io/hostname
# Sidekiq (background jobs)
# Anti-affinity: não rodar no mesmo nó que webservice
sidekiq:
minReplicas: 1
maxReplicas: 1
resources:
requests:
memory: 1.5Gi
cpu: 100m
limits:
memory: 2Gi
cpu: 500m
# Desabilitar memory watchdog interno do GitLab (deixa o OOM killer do K8s gerenciar)
memoryKiller:
maxRss: 2000000000 # 2GB - maior que o limite para evitar kills prematuros
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: webservice
topologyKey: kubernetes.io/hostname
# Gitaly (Git storage)
gitaly:
resources:
requests:
memory: 512Mi
cpu: 100m
limits:
memory: 1Gi
cpu: 500m
persistence:
size: 10Gi # Mínimo Hetzner ($0.0484/GB)
storageClass: hcloud-volumes
# GitLab Shell (SSH)
gitlab-shell:
minReplicas: 1
maxReplicas: 1
service:
type: ClusterIP # TCP passthrough pelo NGINX
resources:
requests:
memory: 64Mi
cpu: 50m
limits:
memory: 128Mi
cpu: 100m
# Toolbox (backup, rake tasks)
toolbox:
enabled: true
resources:
requests:
memory: 256Mi
cpu: 50m
limits:
memory: 512Mi
cpu: 200m
# GitLab Exporter (métricas)
gitlab-exporter:
enabled: false # Economiza recursos
# Migrations
migrations:
resources:
requests:
memory: 256Mi
cpu: 100m
# KAS (Kubernetes Agent Server) - desabilitar
kas:
enabled: false
# =============================================================================
# POSTGRESQL
# =============================================================================
postgresql:
install: true
primary:
resources:
requests:
memory: 512Mi
cpu: 100m
limits:
memory: 1Gi
cpu: 500m
persistence:
size: 10Gi # Mínimo Hetzner ($0.0484/GB)
storageClass: hcloud-volumes
# =============================================================================
# REDIS
# =============================================================================
redis:
install: true
master:
resources:
requests:
memory: 256Mi
cpu: 50m
limits:
memory: 512Mi
cpu: 200m
persistence:
size: 10Gi # Mínimo Hetzner ($0.0484/GB)
storageClass: hcloud-volumes
# =============================================================================
# MINIO (Object Storage)
# =============================================================================
# NOTA: As imagens padrão do GitLab chart não suportam ARM64.
# Usamos as imagens oficiais multi-arch do MinIO.
minio:
install: true
image: minio/minio
imageTag: RELEASE.2024-06-13T22-53-53Z
minioMc:
image: minio/mc
tag: RELEASE.2024-06-12T14-34-03Z
resources:
requests:
memory: 128Mi
cpu: 50m
limits:
memory: 256Mi
cpu: 200m
persistence:
size: 10Gi
storageClass: hcloud-volumes
# Ou usar object storage externo (S3, etc):
# global:
# minio:
# enabled: false
# appConfig:
# object_store:
# enabled: true
# connection:
# secret: gitlab-object-storage
# key: connection
# =============================================================================
# REGISTRY (Container Registry)
# =============================================================================
registry:
enabled: true
hpa:
minReplicas: 1
maxReplicas: 1
resources:
requests:
memory: 128Mi
cpu: 50m
limits:
memory: 256Mi
cpu: 200m
# Storage usa MinIO bundled automaticamente quando minio.install=true
# =============================================================================
# COMPONENTES DESABILITADOS (economia de recursos)
# =============================================================================
# GitLab Runner - instalar separadamente se necessário
gitlab-runner:
install: false
# Prometheus - usar Victoria Metrics da aula-05
prometheus:
install: false
# Grafana
grafana:
install: false
# GitLab Pages
gitlab-pages:
enabled: false
# Praefect (Gitaly HA) - não necessário para instalação pequena
praefect:
enabled: false
# Spamcheck
spamcheck:
enabled: false
# =============================================================================
# UPGRADECHECK
# =============================================================================
upgradeCheck:
enabled: false

569
aula-10/setup.sh Executable file
View File

@@ -0,0 +1,569 @@
#!/bin/bash
# =============================================================================
# Setup da Aula 10 - GitLab via Helm (Hetzner Cloud)
# =============================================================================
#
# Este script instala e configura:
# 1. cert-manager (opcional, para Let's Encrypt)
# 2. GitLab com todos os componentes
#
# Pré-requisitos:
# - Kubernetes cluster Talos na Hetzner (aula-08) com:
# - Hetzner CSI Driver (StorageClass: hcloud-volumes)
# - NGINX Ingress Controller com LoadBalancer
# - kubectl configurado (KUBECONFIG=../aula-08/kubeconfig)
# - Helm 3.x instalado
#
# Uso:
# export KUBECONFIG=$(pwd)/../aula-08/kubeconfig
# chmod +x setup.sh
# ./setup.sh
#
# =============================================================================
set -e
# Cores para output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[OK]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERRO]${NC} $1"; }
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Variáveis de configuração
GITLAB_HOST=""
REGISTRY_HOST="" # Derivado automaticamente
DOMAIN="" # Extraído automaticamente do GITLAB_HOST
USE_CLOUDFLARE=""
USE_LETSENCRYPT=""
LETSENCRYPT_EMAIL=""
# =============================================================================
# FUNÇÕES DE CONFIGURAÇÃO
# =============================================================================
save_config() {
cat > "$SCRIPT_DIR/.env" <<EOF
# Configuração gerada pelo setup.sh
# $(date)
GITLAB_HOST=${GITLAB_HOST}
REGISTRY_HOST=${REGISTRY_HOST}
DOMAIN=${DOMAIN}
USE_CLOUDFLARE=${USE_CLOUDFLARE}
USE_LETSENCRYPT=${USE_LETSENCRYPT}
LETSENCRYPT_EMAIL=${LETSENCRYPT_EMAIL}
EOF
log_success "Configuração salva em .env"
}
load_config() {
# Tentar carregar .env local primeiro
if [[ -f "$SCRIPT_DIR/.env" ]]; then
source "$SCRIPT_DIR/.env"
return 0
fi
# Tentar herdar da aula-09 (apenas CloudFlare/Let's Encrypt)
if [[ -f "$SCRIPT_DIR/../aula-09/.env" ]]; then
source "$SCRIPT_DIR/../aula-09/.env"
log_info "Configuração de TLS herdada da aula-09"
# Limpar hosts (serão perguntados novamente)
GITLAB_HOST=""
REGISTRY_HOST=""
return 0
fi
return 1
}
# Extrai domínio base de um FQDN (ex: git.kube.quest → kube.quest)
extract_domain() {
local fqdn="$1"
echo "$fqdn" | sed 's/^[^.]*\.//'
}
collect_user_input() {
echo ""
echo -e "${CYAN}═══════════════════════════════════════════════════${NC}"
echo -e "${CYAN} GitLab no Kubernetes (Hetzner Cloud)${NC}"
echo -e "${CYAN}═══════════════════════════════════════════════════${NC}"
echo ""
# Carregar configuração existente
load_config
# Se já tem GITLAB_HOST configurado, oferecer usar
if [[ -n "$GITLAB_HOST" ]]; then
echo -e "Configuração existente encontrada:"
echo -e " GitLab: ${GREEN}${GITLAB_HOST}${NC}"
echo -e " Registry: ${GREEN}${REGISTRY_HOST}${NC}"
echo -e " CloudFlare: ${GREEN}${USE_CLOUDFLARE}${NC}"
echo -e " Let's Encrypt: ${GREEN}${USE_LETSENCRYPT}${NC}"
echo ""
echo -e "Deseja usar esta configuração?"
echo -e " 1) Sim, continuar com a configuração existente"
echo -e " 2) Não, reconfigurar"
echo -n "Escolha [1/2]: "
read -r choice
if [[ "$choice" == "1" ]]; then
return 0
fi
fi
# Coletar hostname do GitLab
echo ""
echo -n "Digite o hostname do GitLab (ex: git.kube.quest): "
read -r GITLAB_HOST
if [[ -z "$GITLAB_HOST" ]]; then
log_error "Hostname não pode ser vazio"
exit 1
fi
# Derivar Domain e Registry automaticamente
DOMAIN=$(extract_domain "$GITLAB_HOST")
REGISTRY_HOST="registry.${DOMAIN}"
log_info "Registry será: ${REGISTRY_HOST}"
# Se já tem configuração de TLS, oferecer usar
if [[ -n "$USE_CLOUDFLARE" ]]; then
echo ""
echo -e "Configuração de TLS encontrada:"
echo -e " CloudFlare: ${GREEN}${USE_CLOUDFLARE}${NC}"
echo -e " Let's Encrypt: ${GREEN}${USE_LETSENCRYPT}${NC}"
echo ""
echo -e "Deseja usar esta configuração de TLS?"
echo -e " 1) Sim"
echo -e " 2) Não, reconfigurar"
echo -n "Escolha [1/2]: "
read -r choice
if [[ "$choice" == "1" ]]; then
save_config
return 0
fi
fi
# Perguntar sobre CloudFlare
echo ""
echo "Você usa CloudFlare para DNS?"
echo " 1) Sim (com proxy/CDN ativado - ícone laranja)"
echo " 2) Não"
echo -n "Escolha [1/2]: "
read -r choice
if [[ "$choice" == "1" ]]; then
USE_CLOUDFLARE=true
USE_LETSENCRYPT=false
log_info "CloudFlare irá gerenciar TLS automaticamente na edge"
else
USE_CLOUDFLARE=false
# Perguntar sobre Let's Encrypt
echo ""
echo "Deseja ativar HTTPS com Let's Encrypt?"
echo " 1) Sim (recomendado para produção)"
echo " 2) Não (apenas HTTP)"
echo -n "Escolha [1/2]: "
read -r choice
if [[ "$choice" == "1" ]]; then
USE_LETSENCRYPT=true
echo ""
echo -n "Digite seu email para Let's Encrypt: "
read -r LETSENCRYPT_EMAIL
if [[ -z "$LETSENCRYPT_EMAIL" ]]; then
log_error "Email é obrigatório para Let's Encrypt"
exit 1
fi
else
USE_LETSENCRYPT=false
fi
fi
# Salvar configuração
save_config
}
install_cert_manager() {
log_info "Instalando cert-manager..."
helm repo add jetstack https://charts.jetstack.io 2>/dev/null || true
helm repo update jetstack
if helm status cert-manager -n cert-manager &> /dev/null; then
log_success "cert-manager já está instalado"
else
helm install cert-manager jetstack/cert-manager \
--namespace cert-manager \
--create-namespace \
--version v1.16.2 \
--set crds.enabled=true \
--wait \
--timeout 5m
log_success "cert-manager instalado"
fi
}
create_cluster_issuer() {
log_info "Criando ClusterIssuer para Let's Encrypt..."
cat <<EOF | kubectl apply -f -
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: letsencrypt-prod
spec:
acme:
server: https://acme-v02.api.letsencrypt.org/directory
email: ${LETSENCRYPT_EMAIL}
privateKeySecretRef:
name: letsencrypt-prod-key
solvers:
- http01:
ingress:
class: nginx
EOF
log_success "ClusterIssuer 'letsencrypt-prod' criado"
}
show_dns_instructions() {
# Obter IP do LoadBalancer
LB_IP=$(kubectl get svc -n ingress-nginx ingress-nginx-controller \
-o jsonpath='{.status.loadBalancer.ingress[0].ip}' 2>/dev/null || echo "<pendente>")
# Extrair nome do host (ex: git.kube.quest → git)
GITLAB_NAME=$(echo "$GITLAB_HOST" | cut -d. -f1)
REGISTRY_NAME=$(echo "$REGISTRY_HOST" | sed "s/\.${DOMAIN}$//" )
echo ""
echo -e "${CYAN}═══════════════════════════════════════════════════${NC}"
echo -e "${CYAN} Configure o DNS${NC}"
echo -e "${CYAN}═══════════════════════════════════════════════════${NC}"
if [[ "$USE_CLOUDFLARE" == "true" ]]; then
echo ""
echo "No painel do CloudFlare (https://dash.cloudflare.com):"
echo ""
echo -e " ${YELLOW}Tipo:${NC} A"
echo -e " ${YELLOW}Nome:${NC} ${GITLAB_NAME}"
echo -e " ${YELLOW}Conteúdo:${NC} ${GREEN}${LB_IP}${NC}"
echo -e " ${YELLOW}Proxy:${NC} ✓ (ícone laranja)"
echo ""
echo -e " ${YELLOW}Tipo:${NC} A"
echo -e " ${YELLOW}Nome:${NC} ${REGISTRY_NAME}"
echo -e " ${YELLOW}Conteúdo:${NC} ${GREEN}${LB_IP}${NC}"
echo -e " ${YELLOW}Proxy:${NC} ✓ (ícone laranja)"
echo ""
echo -e "${GREEN}O CloudFlare cuida do TLS automaticamente!${NC}"
else
echo ""
echo "No seu provedor DNS:"
echo ""
echo -e " ${YELLOW}Tipo:${NC} A"
echo -e " ${YELLOW}Nome:${NC} ${GITLAB_NAME}"
echo -e " ${YELLOW}Valor:${NC} ${GREEN}${LB_IP}${NC}"
echo ""
echo -e " ${YELLOW}Tipo:${NC} A"
echo -e " ${YELLOW}Nome:${NC} ${REGISTRY_NAME}"
echo -e " ${YELLOW}Valor:${NC} ${GREEN}${LB_IP}${NC}"
if [[ "$USE_LETSENCRYPT" == "true" ]]; then
echo ""
echo -e "${GREEN}Let's Encrypt irá emitir certificados automaticamente!${NC}"
echo -e "Aguarde ~2 minutos após configurar o DNS."
fi
fi
# Determinar protocolo
PROTOCOL="https"
if [[ "$USE_CLOUDFLARE" == "false" && "$USE_LETSENCRYPT" == "false" ]]; then
PROTOCOL="http"
fi
echo ""
echo -e "Acesse: ${GREEN}${PROTOCOL}://${GITLAB_HOST}${NC}"
}
# =============================================================================
# VERIFICAÇÕES INICIAIS
# =============================================================================
log_info "Verificando pré-requisitos..."
if ! command -v kubectl &> /dev/null; then
log_error "kubectl não encontrado"
exit 1
fi
log_success "kubectl encontrado"
if ! command -v helm &> /dev/null; then
log_error "Helm não encontrado"
exit 1
fi
log_success "Helm $(helm version --short) encontrado"
if ! kubectl cluster-info &> /dev/null; then
log_error "Não foi possível conectar ao cluster"
exit 1
fi
log_success "Conectado ao cluster Kubernetes"
# =============================================================================
# 1. VERIFICAR PRÉ-REQUISITOS DA AULA-08
# =============================================================================
log_info "=== Verificando infraestrutura (aula-08) ==="
# Verificar Hetzner CSI Driver
if ! kubectl get storageclass hcloud-volumes &> /dev/null; then
log_error "StorageClass hcloud-volumes não encontrado!"
log_error "Execute primeiro o setup.sh da aula-08 para instalar o CSI Driver."
exit 1
fi
log_success "Hetzner CSI Driver instalado (hcloud-volumes)"
# Verificar NGINX Ingress
if ! kubectl get ingressclass nginx &> /dev/null; then
log_error "NGINX Ingress não encontrado!"
log_error "Execute primeiro o setup.sh da aula-08 para instalar o Ingress Controller."
exit 1
fi
log_success "NGINX Ingress Controller instalado"
echo ""
# =============================================================================
# 2. COLETAR INPUT DO USUÁRIO
# =============================================================================
collect_user_input
echo ""
# =============================================================================
# 3. INSTALAR CERT-MANAGER (se Let's Encrypt)
# =============================================================================
if [[ "$USE_LETSENCRYPT" == "true" ]]; then
log_info "=== Configurando cert-manager ==="
install_cert_manager
create_cluster_issuer
echo ""
fi
# =============================================================================
# 4. ADICIONAR REPOSITÓRIO HELM
# =============================================================================
log_info "=== Preparando instalação do GitLab ==="
log_info "Adicionando repositório Helm do GitLab..."
helm repo add gitlab https://charts.gitlab.io/ 2>/dev/null || true
helm repo update
# =============================================================================
# 5. CRIAR NAMESPACE
# =============================================================================
log_info "Criando namespace gitlab..."
kubectl create namespace gitlab --dry-run=client -o yaml | kubectl apply -f -
echo ""
# =============================================================================
# 6. INSTALAR GITLAB VIA HELM
# =============================================================================
log_info "=== Instalando GitLab (isso pode levar 10-15 minutos) ==="
# Construir argumentos do Helm dinamicamente
HELM_ARGS=""
# Configurar hosts
HELM_ARGS="$HELM_ARGS --set global.hosts.domain=${DOMAIN}"
HELM_ARGS="$HELM_ARGS --set global.hosts.gitlab.name=${GITLAB_HOST}"
HELM_ARGS="$HELM_ARGS --set global.hosts.registry.name=${REGISTRY_HOST}"
HELM_ARGS="$HELM_ARGS --set global.hosts.minio.name=minio.${GITLAB_HOST}"
# Configurar TLS
if [[ "$USE_LETSENCRYPT" == "true" ]]; then
# Let's Encrypt: TLS gerenciado pelo cert-manager
HELM_ARGS="$HELM_ARGS --set global.ingress.configureCertmanager=true"
HELM_ARGS="$HELM_ARGS --set global.ingress.tls.enabled=true"
HELM_ARGS="$HELM_ARGS --set global.hosts.https=true"
HELM_ARGS="$HELM_ARGS --set certmanager-issuer.email=${LETSENCRYPT_EMAIL}"
# Adicionar annotation do ClusterIssuer
HELM_ARGS="$HELM_ARGS --set global.ingress.annotations.cert-manager\\.io/cluster-issuer=letsencrypt-prod"
elif [[ "$USE_CLOUDFLARE" == "true" ]]; then
# CloudFlare: TLS na edge, backend HTTP
# Workhorse precisa confiar nos IPs do CloudFlare para X-Forwarded-For
HELM_ARGS="$HELM_ARGS --set global.ingress.tls.enabled=false"
HELM_ARGS="$HELM_ARGS --set global.hosts.https=true"
# CloudFlare IPv4 CIDRs (https://www.cloudflare.com/ips-v4)
CLOUDFLARE_CIDRS='["173.245.48.0/20","103.21.244.0/22","103.22.200.0/22","103.31.4.0/22","141.101.64.0/18","108.162.192.0/18","190.93.240.0/20","188.114.96.0/20","197.234.240.0/22","198.41.128.0/17","162.158.0.0/15","104.16.0.0/13","104.24.0.0/14","172.64.0.0/13","131.0.72.0/22"]'
HELM_ARGS="$HELM_ARGS --set-json gitlab.webservice.workhorse.trustedCIDRsForXForwardedFor='${CLOUDFLARE_CIDRS}'"
else
# Apenas HTTP
HELM_ARGS="$HELM_ARGS --set global.ingress.tls.enabled=false"
HELM_ARGS="$HELM_ARGS --set global.hosts.https=false"
fi
# Verificar se já está instalado
if helm status gitlab -n gitlab &> /dev/null; then
log_warn "GitLab já está instalado. Atualizando..."
eval helm upgrade gitlab gitlab/gitlab \
--namespace gitlab \
-f "$SCRIPT_DIR/gitlab-values.yaml" \
$HELM_ARGS \
--timeout 15m \
--wait
log_success "GitLab atualizado com sucesso!"
else
log_info "Instalando GitLab..."
eval helm install gitlab gitlab/gitlab \
--namespace gitlab \
-f "$SCRIPT_DIR/gitlab-values.yaml" \
$HELM_ARGS \
--timeout 15m \
--wait
log_success "GitLab instalado com sucesso!"
fi
echo ""
# =============================================================================
# 7. CONFIGURAR TCP PASSTHROUGH PARA SSH
# =============================================================================
log_info "=== Configurando TCP passthrough para SSH ==="
# Verificar se ConfigMap existe
if kubectl get configmap tcp-services -n ingress-nginx &> /dev/null; then
kubectl patch configmap tcp-services \
-n ingress-nginx \
--type merge \
-p '{"data":{"22":"gitlab/gitlab-gitlab-shell:22"}}'
else
kubectl create configmap tcp-services \
-n ingress-nginx \
--from-literal="22=gitlab/gitlab-gitlab-shell:22"
fi
# Reiniciar NGINX para aplicar
kubectl rollout restart deployment ingress-nginx-controller -n ingress-nginx
log_success "TCP passthrough configurado (porta 22 → GitLab Shell)"
echo ""
# =============================================================================
# 8. OBTER SENHA INICIAL
# =============================================================================
log_info "Obtendo senha inicial do root..."
# Aguardar secret ser criado e obter senha (até 2 min)
ROOT_PASSWORD=""
for i in {1..60}; do
ROOT_PASSWORD=$(kubectl get secret gitlab-gitlab-initial-root-password \
-n gitlab \
-o jsonpath='{.data.password}' 2>/dev/null | base64 -d 2>/dev/null)
if [[ -n "$ROOT_PASSWORD" ]]; then
log_success "Senha obtida"
break
fi
sleep 2
done
# =============================================================================
# RESUMO FINAL
# =============================================================================
# Determinar protocolo
PROTOCOL="https"
if [[ "$USE_CLOUDFLARE" == "false" && "$USE_LETSENCRYPT" == "false" ]]; then
PROTOCOL="http"
fi
echo ""
echo -e "${CYAN}═══════════════════════════════════════════════════${NC}"
echo -e "${GREEN} GitLab Instalado com Sucesso!${NC}"
echo -e "${CYAN}═══════════════════════════════════════════════════${NC}"
echo ""
echo "Componentes instalados:"
if [[ "$USE_LETSENCRYPT" == "true" ]]; then
echo " - cert-manager (ClusterIssuer: letsencrypt)"
fi
echo " - GitLab (namespace: gitlab)"
echo " - Webservice (UI + API)"
echo " - Sidekiq (background jobs)"
echo " - Gitaly (Git storage)"
echo " - GitLab Shell (SSH)"
echo " - PostgreSQL"
echo " - Redis"
echo " - MinIO (object storage)"
echo " - Container Registry"
echo ""
echo "Configuração:"
echo " GitLab: ${GITLAB_HOST}"
echo " Registry: ${REGISTRY_HOST}"
echo " Domínio: ${DOMAIN}"
echo " CloudFlare: ${USE_CLOUDFLARE}"
echo " Let's Encrypt: ${USE_LETSENCRYPT}"
echo ""
echo "URLs:"
echo " Web: ${PROTOCOL}://${GITLAB_HOST}"
echo " Registry: ${PROTOCOL}://${REGISTRY_HOST}"
echo " SSH: git@${GITLAB_HOST} (porta 22)"
echo ""
echo "Credenciais:"
echo " Usuário: root"
if [ -n "$ROOT_PASSWORD" ]; then
echo " Senha: $ROOT_PASSWORD"
else
echo " Senha: (execute o comando abaixo)"
echo ""
echo " kubectl get secret gitlab-gitlab-initial-root-password -n gitlab \\"
echo " -o jsonpath='{.data.password}' | base64 -d; echo"
fi
# Mostrar instruções de DNS
show_dns_instructions
echo ""
echo "Comandos úteis:"
echo " # Ver pods"
echo " kubectl get pods -n gitlab"
echo ""
echo " # Ver logs"
echo " kubectl logs -n gitlab -l app=webservice -f"
echo ""
echo " # Reiniciar componente"
echo " kubectl rollout restart deployment -n gitlab gitlab-webservice-default"
echo ""
if [[ "$USE_LETSENCRYPT" == "true" ]]; then
echo " # Ver status dos certificados"
echo " kubectl get certificate -n gitlab"
echo ""
fi
echo " # Desinstalar"
echo " ./cleanup.sh"
echo ""
echo -e "${CYAN}═══════════════════════════════════════════════════${NC}"
echo ""
# Mostrar status dos pods
log_info "Status dos pods:"
kubectl get pods -n gitlab