This commit introduces the foundational Infrastructure-as-Code for provisioning a test environment on Hetzner Cloud. It defines server nodes, private networking, comprehensive firewalls, and includes documentation on resource lifecycle and safe configuration practices.
160 lines
5.2 KiB
Markdown
160 lines
5.2 KiB
Markdown
# 01 - Test Terraform IaC
|
|
|
|
Bu asamanin amaci test Hetzner Cloud Project icinde minimum IaaS kaynaklarini Terraform ile olusturmaktir. Bu dokuman tek basina uygulanabilir olacak sekilde yazilmistir.
|
|
|
|
## Kapsam
|
|
|
|
Terraform test ortaminda sunlari olusturur:
|
|
|
|
- Private network: `iklim-test-net`
|
|
- Subnetler:
|
|
- App/Swarm subnet: `10.10.10.0/24`
|
|
- DB subnet: `10.10.20.0/24`
|
|
- Firewall:
|
|
- Public ingress: sadece `22/tcp`, `80/tcp`, `443/tcp`
|
|
- Private ingress: `07-private-network-port-matrisi.md` dosyasindaki test kurallari
|
|
- SSH key
|
|
- Placement group: `test-spread`
|
|
- Server:
|
|
- `test-swarm-01`
|
|
- `test-db-01`
|
|
- Ansible inventory output
|
|
|
|
Terraform DB yazilimini kurmaz. DB node sadece makine, network ve firewall seviyesinde hazirlanir.
|
|
|
|
## Onerilen Dosya Yapisi
|
|
|
|
```text
|
|
terraform/
|
|
hetzner/
|
|
test/
|
|
versions.tf
|
|
providers.tf
|
|
variables.tf
|
|
locals.tf
|
|
network.tf
|
|
firewall.tf
|
|
placement.tf
|
|
servers.tf
|
|
outputs.tf
|
|
terraform.tfvars.example
|
|
```
|
|
|
|
`terraform.tfvars` commit edilmeyecek. `.gitignore` icinde ignore edilmelidir.
|
|
|
|
## Degiskenler
|
|
|
|
Minimum degiskenler:
|
|
|
|
```hcl
|
|
hcloud_token = "secret"
|
|
environment = "test"
|
|
location = "fsn1"
|
|
image = "ubuntu-24.04"
|
|
server_type_swarm = "cx32"
|
|
server_type_db = "cx42"
|
|
admin_ssh_public_key_path = "~/.ssh/id_ed25519.pub"
|
|
admin_allowed_cidrs = ["X.X.X.X/32"]
|
|
```
|
|
|
|
`location` icin tek lokasyonla baslanir. Farkli region/lokasyon felaket kurtarma bu asamada konu disidir; ileride dokumana eklenmelidir.
|
|
|
|
## Server Rolleri
|
|
|
|
| Server | Private IP | Rol |
|
|
| --- | --- | --- |
|
|
| `test-swarm-01` | `10.10.10.11` | Swarm manager + app worker + Gitea runner |
|
|
| `test-db-01` | `10.10.20.11` | Manuel DB kurulumu icin hazir DB node |
|
|
|
|
Private IP'ler Terraform icinde sabit tanimlanmalidir. Ansible inventory ve firewall kurallari deterministik kalir.
|
|
|
|
## Firewall Kurallari
|
|
|
|
Public ingress:
|
|
|
|
| Port | Kaynak | Hedef |
|
|
| --- | --- | --- |
|
|
| `22/tcp` | `admin_allowed_cidrs` | Tum test node'lari |
|
|
| `80/tcp` | `0.0.0.0/0`, `::/0` | `test-swarm-01` |
|
|
| `443/tcp` | `0.0.0.0/0`, `::/0` | `test-swarm-01` |
|
|
|
|
Public ingress icin `8200/tcp`, `5432/tcp`, `27017/tcp`, `5672/tcp`, `15672/tcp`, `6379/tcp`, `2379/tcp`, `9180/tcp`, `9090/tcp`, `3000/tcp` acilmayacak.
|
|
|
|
Private ingress icin `07-private-network-port-matrisi.md` kaynak alinacak.
|
|
|
|
## Placement Group
|
|
|
|
`test-spread` placement group `type = "spread"` olacak. Testte iki server oldugu icin bu grup `test-swarm-01` ve `test-db-01` makinelerinin farkli fiziksel host'lara dagitilmasini hedefler.
|
|
|
|
Not: Spread placement group farkli kabinet veya lokasyon garantisi degildir; tek fiziksel host arizasinin etkisini azaltir.
|
|
|
|
## Terraform Cikti Beklentisi
|
|
|
|
`outputs.tf` minimum su bilgileri uretmelidir:
|
|
|
|
```hcl
|
|
output "ansible_inventory_yaml" {
|
|
sensitive = false
|
|
}
|
|
|
|
output "test_private_ips" {
|
|
sensitive = false
|
|
}
|
|
|
|
output "test_public_ips" {
|
|
sensitive = false
|
|
}
|
|
```
|
|
|
|
Inventory output'u daha sonra `ansible/inventory/generated/test.yml` dosyasina yazilabilir. Inventory dosyasinda secret bulunmayacaksa commit edilebilir; secret veya token icerirse commit edilmeyecek.
|
|
|
|
## Lifecycle ve Resize Politikasi
|
|
|
|
### server_type Degisikligi (Yeniden Boyutlandirma)
|
|
|
|
`server_type` degistirmek Terraform destroy+create **tetiklemez**. `hcloud` provider
|
|
bunu natively destekler: sunucuyu durdurur, Hetzner Resize API'sini cagirir,
|
|
yeniden baslatir. `terraform.tfvars` icinde degeri guncelle, `terraform apply` calistir.
|
|
|
|
Downtime olur (sunucu durur ve baslar) ancak disk, kurulu yazilim ve Docker volumes
|
|
korunur. `ignore_changes` veya manuel adim gerekmez.
|
|
|
|
### Hangi Degisiklikler Sunucuyu Zorla Yeniden Olusturur?
|
|
|
|
| Degisen alan | Davranis | Not |
|
|
| --- | --- | --- |
|
|
| `server_type` | In-place resize (provider native) | `terraform apply` yeterli |
|
|
| `hcloud_server_network` | Sadece attachment guncellenir | Ayri resource kullanildigi icin |
|
|
| `hcloud_firewall_attachment` | Sadece attachment guncellenir | Ayri resource kullanildigi icin |
|
|
| `placement_group_id` | Hetzner API degisime izin vermiyor → destroy+create | Degistirme |
|
|
| `image` | Disk imaji degisir → destroy+create | Degistirme |
|
|
| `location` | Baska datacenter'a tasinamaz → destroy+create | Degistirme |
|
|
|
|
### Network ve Firewall Attachment Ayrimi
|
|
|
|
`network` blogu ve `firewall_ids` `hcloud_server` icine gomulmez. Bunun yerine
|
|
ayri resource tanimlanir:
|
|
|
|
- `hcloud_server_network` — private IP atamasi
|
|
- `hcloud_firewall_attachment` — firewall iliskisi
|
|
|
|
Gomulu tanimlamada bazi provider versiyonlari bu alanlardaki degisiklikleri
|
|
sunucu recreation olarak yorumlar. Ayri resource kullanildiginda sadece
|
|
attachment guncellenir, sunucu dokunulmaz.
|
|
|
|
### prevent_destroy Korumasi
|
|
|
|
Her sunucuya `lifecycle { prevent_destroy = true }` eklenir. Bu blok varken
|
|
Terraform hicbir kosulda sunucuyu silemez, plan asamasinda hata verir.
|
|
Kasitli silmek icin once lifecycle blogunu gecici olarak kaldir.
|
|
|
|
## Kabul Kriterleri
|
|
|
|
- `terraform plan` sadece test Hetzner Project token'i ile calisir.
|
|
- `terraform apply` sonrasinda 2 server olusur.
|
|
- Iki server private network uzerinden birbirine erisebilir.
|
|
- Public internetten sadece `22`, `80`, `443` firewall seviyesinde aciktir.
|
|
- Vault `8200` public'ten kapali kalir.
|
|
- Terraform state repo'ya commit edilmez.
|
|
|