Infraestrutura Completa em GKE com Terraform, Ansible e GitHub Actions
- Visão Geral
- Tecnologias Utilizadas
- Arquitetura
- Pré-requisitos
- Como Executar
- Estrutura do Projeto
- Testes e Validação
- Segurança
- CI/CD
Este repositório contém uma infraestrutura completa de DevOps implementada na Google Cloud Platform (GKE), demonstrando:
- ✅ Infraestrutura como Código com Terraform
- ✅ Automação de Configuração com Ansible
- ✅ Kubernetes de Alta Disponibilidade (2 nodes)
- ✅ HTTPS Automático com Let's Encrypt
- ✅ Monitoramento Completo (Prometheus + Grafana + Alertmanager)
- ✅ Logs Centralizados (Loki + Promtail)
- ✅ Gerenciamento de Secrets (HashiCorp Vault)
- ✅ CI/CD Automatizado (GitHub Actions)
- ✅ Segurança (Network Policies + RBAC)
- ✅ Auto-scaling (HPA)
- Google Cloud Platform
- Terraform v1.6+
- Ansible v2.15+
- GKE (Google Kubernetes Engine)
- Helm
- Ingress Nginx
- Cert-Manager (Let's Encrypt)
- Prometheus
- Grafana
- Loki + Promtail
- HashiCorp Vault
- Network Policies
- RBAC (Role-Based Access Control)
- GitHub Actions
┌─────────────────────────────────────────────────────────────────┐
│ INTERNET / USERS │
└────────────────────────────┬────────────────────────────────────┘
│ HTTPS (443)
▼
┌─────────────────────────────────────────────────────────────────┐
│ INGRESS NGINX CONTROLLER │
│ (Load Balancer + SSL Termination) │
│ Cert-Manager (Let's Encrypt) │
└────────────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ GOOGLE KUBERNETES ENGINE │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ APPLICATION (WhoAmI - 2+ replicas) │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │ Pod 1 │ │ Pod 2 │ │ Pod 3 │ HPA │ │
│ │ └──────────┘ └──────────┘ └──────────┘ │ │
│ └──────────────────────────────────────────────────────────┘ │
│ │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ OBSERVABILITY STACK │ │
│ │ ┌────────────┐ ┌──────────┐ ┌─────────────────┐ │ │
│ │ │ Prometheus │ │ Grafana │ │ Alertmanager │ │ │
│ │ └────────────┘ └──────────┘ └─────────────────┘ │ │
│ │ ┌────────────┐ ┌──────────┐ │ │
│ │ │ Loki │ │ Promtail │ │ │
│ │ └────────────┘ └──────────┘ │ │
│ └──────────────────────────────────────────────────────────┘ │
│ │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ SECURITY & SECRETS │ │
│ │ ┌────────────┐ ┌──────────────────┐ │ │
│ │ │ Vault │ │ Network Policies │ │ │
│ │ └────────────┘ └──────────────────┘ │ │
│ └──────────────────────────────────────────────────────────┘ │
│ │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ NODES (2x) │ │
│ │ ┌─────────────────┐ ┌─────────────────┐ │ │
│ │ │ Node 1 (e2) │ │ Node 2 (e2) │ │ │
│ │ │ 2 vCPU + 4GB │ │ 2 vCPU + 4GB │ │ │
│ │ └─────────────────┘ └─────────────────┘ │ │
│ └──────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
▲
│
┌─────────┴──────────┐
│ TERRAFORM (IaC) │
│ ANSIBLE (Config) │
│ GITHUB ACTIONS │
└─────────────────────┘
| Requisito | Status | Tecnologia | Observação |
|---|---|---|---|
| Kubernetes HA | ✅ | GKE (2 nodes) | Alta disponibilidade com múltiplos nodes |
| HTTPS Automático | ✅ | Cert-Manager + Let's Encrypt | Certificado válido e renovação automática |
| Monitoramento | ✅ | Prometheus + Grafana + Alertmanager | Dashboards e alertas configurados |
| Logs Centralizados | ✅ | Loki + Promtail | Agregação de logs dos pods |
| CI/CD | ✅ | GitHub Actions | Deploy automático a cada push |
| Vault | ✅ | HashiCorp Vault | Gerenciamento de secrets |
| Segurança | ✅ | Network Policies + RBAC | Isolamento de rede e controle de acesso |
| Escalabilidade | ✅ | HPA (Horizontal Pod Autoscaler) | Auto-scaling baseado em CPU |
| IaC | ✅ | Terraform | Infraestrutura reproduzível |
| Automação | ✅ | Ansible | Configuração automatizada |
# Terraform
terraform --version # >= 1.6.0
# Google Cloud SDK
gcloud --version
# Kubectl
kubectl version --client
# Ansible
ansible --version # >= 2.15
# Helm (opcional)
helm version- Conta Google Cloud ativa
- Projeto GCP criado
- APIs habilitadas:
- Kubernetes Engine API
- Compute Engine API
- Service Account com permissões:
roles/container.adminroles/compute.admin
# 1. Clonar repositório
git clone https://github.com/wlazevedo/DevOps_Desafio.git
cd DevOps_Desafio
# 2. Configurar credenciais GCP
gcloud auth login
gcloud config set project SEU-PROJECT-ID
# 3. Provisionar infraestrutura
cd terraform
terraform init
terraform apply -auto-approve
# 4. Configurar kubectl
gcloud container clusters get-credentials devops-challenge-cluster \
--zone us-central1-a \
--project SEU-PROJECT-ID
# 5. Verificar
kubectl get nodes
kubectl get pods -A
# 6. Acessar aplicação
curl https://SEU-DOMINIO# Após terraform apply é criado os recursos na GCP e o terraform também roda o playbook com os manifests (path e execução do playbook.yaml dentro do main.tf). Caso queira forçar algum manifest não executado, rode os comandos a seguir:
cd ../ansible
ansible-playbook -i inventory/hosts playbook.yaml# Configurar secrets no GitHub:
# - GCP_SA_JSON (Service Account JSON)
# Fazer push para main
git add .
git commit -m "deploy: initial setup"
git push origin main
# Pipeline executa automaticamentedevops-challenge/
├── README.md # Este arquivo
├── .gitignore # Arquivos ignorados
├── .github/
│ └── workflows/
│ └── deploy.yml # Pipeline CI/CD
├── terraform/
│ ├── main.tf # Configuração principal
│ ├── variables.tf # Variáveis
│ ├── outputs.tf # Outputs
│ └── providers.tf # Providers GCP
├── ansible/
│ ├── playbook.yaml # Playbook principal
│ ├── inventory/
│ │ └── hosts # Inventário
│ └── roles/ # Roles customizadas
├── k8s/
│ ├── namespace.yaml # Namespace da aplicação
│ ├── deployment.yaml # Deployment WhoAmI
│ ├── service.yaml # Service
│ ├── ingress.yaml # Ingress + TLS
│ └── hpa.yaml # Horizontal Pod Autoscaler
├── monitoring/
│ ├── dashboards/ # Dashboards Grafana
│ ├── alerts/ # Regras de alerta
│ └── loki/ # Configuração Loki
├── logging/
│ └── promtail-config.yaml # Configuração Promtail
├── security/
│ ├── network-policy.yaml # Network Policies
│ └── vault/ # Configurações Vault
├── health/
│ └── health-check.sh # Script de health check
└── docs/
├── arquitetura.md # Documentação da arquitetura
├── setup-guide.md # Guia de instalação
└── troubleshooting.md # Solução de problemas
# Verificar pods
kubectl get pods -n devops-challenge
# Resultado esperado: 2+ pods Running
# whoami-xxx 1/1 Running# Testar certificado SSL
curl -v https://devops-walax.ddns.net
# Resultado esperado:
# ✓ SSL certificate verify ok
# ✓ HTTP/2 200# Ver hostnames diferentes
for i in {1..10}; do
curl -s https://devops-walax.ddns.net | grep Hostname
done
# Resultado esperado: Hostnames alternando entre pods# Ver estado do HPA
kubectl get hpa -n devops-challenge
# Gerar carga
kubectl run load-generator --image=busybox --rm -it -- /bin/sh
while true; do wget -q -O- http://whoami.devops-challenge.svc.cluster.local; done
# Observar escalando
watch kubectl get hpa,pods -n devops-challenge# Deletar um pod
kubectl delete pod -n devops-challenge -l app=whoami --force
# Verificar recriação automática
kubectl get pods -n devops-challenge -w
# Resultado esperado: Pod recriado em ~10s# Port-forward
kubectl port-forward -n monitoring svc/kube-prometheus-stack-grafana 3000:80
# Abrir navegador: http://localhost:3000
# User: admin
# Password: prom-operator- Kubernetes / Compute Resources / Cluster: Visão geral do cluster
- Kubernetes / Compute Resources / Namespace (Pods): Métricas por namespace
- Kubernetes / Networking / Cluster: Tráfego de rede
- Node Exporter / Nodes: Métricas de hardware dos nodes
- Nginx Ingress Controller: Métricas do Ingress
kubectl port-forward -n monitoring svc/kube-prometheus-stack-prometheus 9090:9090
# Abrir: http://localhost:9090# No Grafana → Explore → Datasource: Loki
# Queries úteis:
{namespace="devops-challenge"}
{namespace="devops-challenge"} |= "error"
{namespace="devops-challenge", app="whoami"}# Ver policies aplicadas
kubectl get networkpolicy -n devops-challenge
# Política: Apenas Ingress e Monitoring podem acessar aplicação# Ver roles e bindings
kubectl get roles,rolebindings -n devops-challenge# Acessar Vault
kubectl port-forward -n vault svc/vault 8200:8200
# Abrir: http://localhost:8200
# Token root: ver em kubectl logs vault-0 -n vault# Ver secrets (sem expor valores)
kubectl get secrets -n devops-challengeO pipeline é disparado automaticamente a cada push na branch main.
Etapas:
- Checkout do código
- Autenticação no GCP
- Configuração do kubectl
- Aplicação de dashboards e alertas
- Aplica secret simples no vault
- Health checks
- Verificação de status
Ver pipeline:
GitHub → Actions → Ver último workflow
Testar pipeline:
Run workflow - workflow dispatch - Kubernetes Documentation
- Google Cloud Documentation
- Terraform Registry
- Prometheus Community
- Grafana Labs
- Vault HashiCorp Dev Mode
- Cert-manager
- Kube-prometheus-stack
- Promtail + Loki