- Aula 10: Gitea + Registry + Actions + Runner (substituiu GitLab) - gitea-values.yaml: PostgreSQL standalone, Valkey standalone, ~800Mi RAM - setup.sh/cleanup.sh: namespace gitea, Helm gitea-charts/gitea + actions - README.md: documentação completa com de→para (GitLab/Harbor/Tekton vs Gitea) - Aula 11: ArgoCD (GitOps) — removido GitLab Runner (runner vive na aula-10) - setup.sh: só ArgoCD, integração SSH com Gitea - node-bugado/.gitea/workflows/ci.yml: pipeline convertida - Aula 13: Container Factory — atualizado para Gitea - setup.sh/cleanup.sh: referências GitLab → Gitea - pipelines/postgresql/ci.yml: Gitea Actions workflow - README.md: conexão com act_runner explicada - CLAUDE.md: tabela de aulas atualizada
276 lines
8.6 KiB
Markdown
276 lines
8.6 KiB
Markdown
# Aula 11 - ArgoCD (GitOps)
|
|
|
|
Deploy do ArgoCD para CD declarativo com GitOps, integrado ao Gitea (aula-10).
|
|
|
|
## Arquitetura
|
|
|
|
```
|
|
┌─────────────────────────────────────────────────────────────┐
|
|
│ Gitea (aula-10) │
|
|
│ ┌─────────────┐ ┌──────────────┐ ┌───────────────┐ │
|
|
│ │ git push │───►│ Gitea Actions│───►│ Registry │ │
|
|
│ │ (código) │ │ (act_runner) │ │ (imagem:tag) │ │
|
|
│ └─────────────┘ └──────┬───────┘ └───────────────┘ │
|
|
└────────────────────────────┼────────────────────────────────┘
|
|
│ atualiza manifests
|
|
▼
|
|
┌─────────────────────────────────────────────────────────────┐
|
|
│ Repositório GitOps │
|
|
│ apps/node-bugado/ │
|
|
│ ├── deployment.yaml (image: gitea.../node-bugado:sha) │
|
|
│ ├── service.yaml │
|
|
│ └── configmap.yaml │
|
|
└─────────────────────────────────────────────────────────────┘
|
|
│
|
|
│ sync automático
|
|
▼
|
|
┌─────────────────────────────────────────────────────────────┐
|
|
│ ArgoCD │
|
|
│ ┌─────────────┐ ┌──────────────┐ ┌───────────────┐ │
|
|
│ │ Application │───►│ Sync │───►│ Kubernetes │ │
|
|
│ │ CRD │ │ Controller │ │ (deploy) │ │
|
|
│ └─────────────┘ └──────────────┘ └───────────────┘ │
|
|
└─────────────────────────────────────────────────────────────┘
|
|
```
|
|
|
|
## Pré-requisitos
|
|
|
|
1. **Cluster Talos na Hetzner** (aula-08) com:
|
|
- NGINX Ingress Controller com LoadBalancer
|
|
- Hetzner CSI Driver
|
|
2. **Gitea instalado** (aula-10)
|
|
3. **kubectl** e **helm** instalados
|
|
|
|
## Contexto do Cluster
|
|
|
|
```bash
|
|
# Usar kubeconfig da aula-08
|
|
export KUBECONFIG=$(pwd)/../aula-08/kubeconfig
|
|
kubectl cluster-info
|
|
```
|
|
|
|
## Instalação
|
|
|
|
```bash
|
|
cd aula-11
|
|
|
|
# Executar setup interativo
|
|
chmod +x setup.sh
|
|
./setup.sh
|
|
```
|
|
|
|
O script instala:
|
|
1. **ArgoCD** - GitOps CD para Kubernetes
|
|
2. Configura integração SSH com Gitea
|
|
|
|
## Componentes Instalados
|
|
|
|
| Componente | Namespace | Recursos | Função |
|
|
|------------|-----------|----------|--------|
|
|
| ArgoCD Server | argocd | 256Mi/512Mi | UI + API |
|
|
| ArgoCD Repo Server | argocd | 256Mi/512Mi | Git clone/sync |
|
|
| ArgoCD Controller | argocd | 256Mi/512Mi | Reconciliation |
|
|
| ArgoCD Redis | argocd | 64Mi/128Mi | Cache |
|
|
| **Total** | | **~832Mi** | |
|
|
|
|
## Acesso
|
|
|
|
### ArgoCD
|
|
|
|
```
|
|
URL: https://argocd.{domain}
|
|
Username: admin
|
|
Senha: kubectl get secret argocd-initial-admin-secret -n argocd \
|
|
-o jsonpath='{.data.password}' | base64 -d
|
|
```
|
|
|
|
## Configurar Pipeline GitOps
|
|
|
|
### 1. Criar Repositório GitOps no Gitea
|
|
|
|
Crie um repositório `gitops-demo` com a estrutura:
|
|
|
|
```
|
|
gitops-demo/
|
|
└── apps/
|
|
└── node-bugado/
|
|
├── configmap.yaml
|
|
├── deployment.yaml
|
|
├── service.yaml
|
|
└── ingress.yaml
|
|
```
|
|
|
|
Use os arquivos de exemplo em `node-bugado/k8s/`.
|
|
|
|
### 2. Configurar Deploy Key
|
|
|
|
```bash
|
|
# Gerar par de chaves
|
|
ssh-keygen -t ed25519 -f argocd-deploy-key -N ''
|
|
|
|
# Adicionar chave pública no Gitea:
|
|
# Repositório → Settings → Deploy Keys
|
|
# Marcar "Enable Write Access" (se o CI precisa push)
|
|
```
|
|
|
|
### 3. Conectar Repositório no ArgoCD
|
|
|
|
Via UI:
|
|
1. Acesse https://argocd.{domain}
|
|
2. Settings → Repositories → Connect Repo
|
|
3. Method: SSH
|
|
4. URL: `git@gitea.{domain}:{usuario}/gitops-demo.git`
|
|
5. SSH private key: (conteúdo de argocd-deploy-key)
|
|
|
|
Ou via CLI:
|
|
```bash
|
|
argocd repo add git@gitea.kube.quest:usuario/gitops-demo.git \
|
|
--ssh-private-key-path argocd-deploy-key
|
|
```
|
|
|
|
### 4. Criar ArgoCD Application
|
|
|
|
```bash
|
|
kubectl apply -f - << 'EOF'
|
|
apiVersion: argoproj.io/v1alpha1
|
|
kind: Application
|
|
metadata:
|
|
name: node-bugado
|
|
namespace: argocd
|
|
spec:
|
|
project: default
|
|
source:
|
|
repoURL: git@gitea.kube.quest:usuario/gitops-demo.git
|
|
targetRevision: HEAD
|
|
path: apps/node-bugado
|
|
destination:
|
|
server: https://kubernetes.default.svc
|
|
namespace: demo
|
|
syncPolicy:
|
|
automated:
|
|
prune: true
|
|
selfHeal: true
|
|
syncOptions:
|
|
- CreateNamespace=true
|
|
EOF
|
|
```
|
|
|
|
### 5. Configurar Pipeline no Repositório da App
|
|
|
|
No repositório `node-bugado`, crie `.gitea/workflows/ci.yml`:
|
|
|
|
```yaml
|
|
name: Build and Deploy
|
|
on:
|
|
push:
|
|
branches: [main]
|
|
env:
|
|
REGISTRY: gitea.kube.quest
|
|
jobs:
|
|
build:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
- run: echo "${{ secrets.REGISTRY_TOKEN }}" | docker login ${{ env.REGISTRY }} -u ${{ gitea.actor }} --password-stdin
|
|
- run: |
|
|
docker build -t ${{ env.REGISTRY }}/${{ gitea.repository }}:${{ github.sha }} .
|
|
docker push ${{ env.REGISTRY }}/${{ gitea.repository }}:${{ github.sha }}
|
|
deploy:
|
|
runs-on: ubuntu-latest
|
|
needs: build
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
- run: |
|
|
# Clone GitOps repo e atualizar tag
|
|
# ArgoCD sincroniza automaticamente
|
|
```
|
|
|
|
Configure secrets em: Repository → Settings → Actions → Secrets:
|
|
- `REGISTRY_TOKEN`: Token com permissão de push no registry
|
|
- `DEPLOY_KEY`: Chave SSH privada (com write access ao repo gitops)
|
|
|
|
## Fluxo de Deploy
|
|
|
|
1. **Desenvolvedor** faz push no repositório `node-bugado`
|
|
2. **Gitea Actions** (act_runner) dispara pipeline:
|
|
- Build: Constrói imagem Docker
|
|
- Push: Envia para Gitea Container Registry
|
|
- Deploy: Atualiza `deployment.yaml` no repo GitOps
|
|
3. **ArgoCD** detecta mudança no repo GitOps
|
|
4. **ArgoCD** sincroniza com cluster Kubernetes
|
|
5. **Kubernetes** faz rolling update dos pods
|
|
|
|
## Troubleshooting
|
|
|
|
### ArgoCD não sincroniza
|
|
|
|
```bash
|
|
# Verificar status da Application
|
|
kubectl get applications -n argocd
|
|
|
|
# Ver detalhes
|
|
kubectl describe application node-bugado -n argocd
|
|
|
|
# Ver logs do controller
|
|
kubectl logs -n argocd -l app.kubernetes.io/name=argocd-application-controller
|
|
```
|
|
|
|
### Erro SSH ao conectar repositório
|
|
|
|
```bash
|
|
# Verificar known hosts
|
|
kubectl get configmap argocd-ssh-known-hosts-cm -n argocd -o yaml
|
|
|
|
# Adicionar manualmente
|
|
ssh-keyscan gitea.kube.quest | kubectl create configmap argocd-ssh-known-hosts-cm \
|
|
--from-file=ssh_known_hosts=/dev/stdin -n argocd --dry-run=client -o yaml | kubectl apply -f -
|
|
```
|
|
|
|
## Comandos Úteis
|
|
|
|
```bash
|
|
# ArgoCD
|
|
kubectl get applications -n argocd
|
|
kubectl get pods -n argocd
|
|
argocd app list
|
|
argocd app sync node-bugado
|
|
|
|
# Ver todos os recursos do demo
|
|
kubectl get all -n demo
|
|
|
|
# Forçar re-sync
|
|
argocd app sync node-bugado --force
|
|
|
|
# Ver diff antes de sync
|
|
argocd app diff node-bugado
|
|
```
|
|
|
|
## Lições do Workshop
|
|
|
|
1. **GitOps**: Git como fonte única de verdade para estado do cluster
|
|
2. **Separação CI/CD**: Gitea Actions = build, ArgoCD = deploy
|
|
3. **Auditoria**: Histórico de deploys = histórico Git
|
|
4. **Self-Heal**: ArgoCD corrige drift automaticamente
|
|
5. **Segurança**: Deploy Keys com permissão mínima
|
|
|
|
## Cleanup
|
|
|
|
```bash
|
|
./cleanup.sh
|
|
```
|
|
|
|
Remove ArgoCD. Não remove Gitea ou infraestrutura base.
|
|
|
|
## Custos
|
|
|
|
| Recurso | Custo/mês |
|
|
|---------|-----------|
|
|
| ArgoCD (~832Mi) | Usa workers existentes |
|
|
| **Total Adicional** | ~$0 |
|
|
|
|
## Referências
|
|
|
|
- [ArgoCD Docs](https://argo-cd.readthedocs.io/en/stable/)
|
|
- [ArgoCD Helm Chart](https://github.com/argoproj/argo-helm)
|
|
- [Gitea Actions](https://docs.gitea.com/usage/actions/overview)
|