GCP Compute Engine Yönetilen Örnek Grubu (MIG) Kurulumu

Google Cloud’da production ortamı kuruyorsun ve uygulamanın yük altında çökmemesi gerekiyor. Tek bir sanal makineyle yola çıkmak, gece 2’de telefon almak için biçilmiş kaftan. İşte tam bu noktada Managed Instance Group (MIG) devreye giriyor. MIG, aynı yapılandırmaya sahip sanal makineleri otomatik olarak yöneten, ihtiyaç halinde ölçeklendiren ve arızalanan örnekleri kendiliğinden yeniden başlatan GCP’nin temel altyapı bileşenlerinden biri. Bu yazıda sıfırdan production-ready bir MIG kurulumunu adım adım inceleyeceğiz.

Managed Instance Group Nedir ve Ne Zaman Kullanılır?

MIG özünde bir instance template üzerinden oluşturulan, homojen sanal makine koleksiyonu. Tek tek VM yönetmek yerine grubu bir bütün olarak yönetiyorsun. Aşağıdaki senaryolarda MIG tam ihtiyacın olan çözüm:

  • Web uygulamanın trafiği gün içinde dramatik dalgalanmalar yaşıyorsa
  • VM’lerden biri çöktüğünde otomatik kurtarma istiyorsan
  • Zero-downtime deployment yapman gerekiyorsa (rolling update)
  • Multi-zone yüksek erişilebilirlik kuruyorsan
  • Load balancer arkasında homojen bir backend fleet istiyorsan

MIG’in iki türü var:

  • Zonal MIG: Tek availability zone’da çalışır, latency açısından avantajlı ama zone outage’ına karşı savunmasız
  • Regional MIG: Birden fazle zone’a yayılır, production için önerilen seçenek

Ön Hazırlık: GCP Projesi ve gcloud Kurulumu

Başlamadan önce ortamını hazırla. Google Cloud SDK kurulu ve authenticated olmuş olmalısın.

# gcloud SDK kurulumu sonrası authenticate et
gcloud auth login
gcloud config set project YOUR_PROJECT_ID

# Compute Engine API'yi aktifleştir
gcloud services enable compute.googleapis.com

# Çalışacağın region ve zone'u ayarla
gcloud config set compute/region europe-west1
gcloud config set compute/zone europe-west1-b

# Mevcut konfigürasyonu doğrula
gcloud config list

Proje ID’ni her seferinde yazmamak için bir environment variable tanımla:

export PROJECT_ID=$(gcloud config get-value project)
export REGION="europe-west1"
export ZONE="europe-west1-b"
echo "Proje: $PROJECT_ID | Region: $REGION"

Instance Template Oluşturma

MIG’in kalbi instance template. Bu template, gruptaki her VM’in nasıl görüneceğini tanımlar: machine type, disk, network, metadata, startup script. Template’i bir kez tanımlayıp sonradan değiştirdiğinde rolling update ile tüm fleet’i güncelleyebiliyorsun.

Önce basit bir web sunucusu için startup script hazırlayalım:

cat > startup-script.sh << 'EOF'
#!/bin/bash
apt-get update -y
apt-get install -y nginx

# Instance metadata'dan hostname al
INSTANCE_NAME=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/name" -H "Metadata-Flavor: Google")
ZONE=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/zone" -H "Metadata-Flavor: Google" | cut -d'/' -f4)

# Basit bir index sayfası oluştur
cat > /var/www/html/index.html << HTML
<html>
<body>
<h1>Merhaba! Ben: $INSTANCE_NAME</h1>
<p>Zone: $ZONE</p>
</body>
</html>
HTML

systemctl enable nginx
systemctl start nginx

# Health check endpoint
cat > /var/www/html/health << HEALTH
OK
HEALTH

echo "Startup script tamamlandi: $(date)" >> /var/log/startup-script.log
EOF

Şimdi bu script’i kullanarak instance template oluştur:

gcloud compute instance-templates create web-server-template-v1 
  --machine-type=e2-medium 
  --image-family=debian-11 
  --image-project=debian-cloud 
  --boot-disk-size=20GB 
  --boot-disk-type=pd-balanced 
  --tags=http-server,https-server 
  --metadata-from-file=startup-script=startup-script.sh 
  --region=$REGION 
  --description="Web sunucusu MIG template - v1"

# Template'i doğrula
gcloud compute instance-templates describe web-server-template-v1

Template parametrelerini anlamak önemli:

  • –machine-type: VM boyutu, e2-medium 2 vCPU + 4GB RAM
  • –image-family: Debian 11 base image, otomatik en son stable sürümü seçer
  • –boot-disk-type: pd-balanced performans ve maliyet dengesi için iyi seçim
  • –tags: Firewall rule’larla network erişimini kontrol etmek için
  • –metadata-from-file: Startup script’i dosyadan okur, inline yazmaktan çok daha temiz

Firewall Kuralları

Template’de http-server tag kullandık, şimdi bu tag için firewall açalım:

# HTTP erişimi
gcloud compute firewall-rules create allow-http 
  --allow=tcp:80 
  --target-tags=http-server 
  --source-ranges=0.0.0.0/0 
  --description="HTTP trafiğine izin ver"

# Health check için Google'ın IP aralıklarına izin ver
gcloud compute firewall-rules create allow-health-check 
  --allow=tcp:80 
  --target-tags=http-server 
  --source-ranges=130.211.0.0/22,35.191.0.0/16 
  --description="GCP load balancer health check IP aralıkları"

Managed Instance Group Oluşturma

Template hazır, şimdi asıl MIG’i oluşturalım. Regional MIG kullanacağız:

gcloud compute instance-groups managed create web-server-mig 
  --template=web-server-template-v1 
  --size=3 
  --region=$REGION 
  --description="Web sunucusu managed instance group"

# MIG durumunu kontrol et
gcloud compute instance-groups managed list-instances web-server-mig 
  --region=$REGION

Bu komut regional MIG oluşturur ve --size=3 ile 3 instance başlatır. GCP bu instance’ları bölgedeki farklı zone’lara otomatik dağıtır. Tek zone’da kalmak isteseydin --zone parametresi kullanman gerekirdi.

MIG oluşturulduktan sonra instance’ların RUNNING durumuna gelmesini bekle:

# Instance'lar hazır olana kadar bekle
gcloud compute instance-groups managed wait-until stable web-server-mig 
  --region=$REGION 
  --timeout=300

# Grup detaylarını gör
gcloud compute instance-groups managed describe web-server-mig 
  --region=$REGION

Autoscaling Konfigürasyonu

Şimdi gelin işi ilginç hale getirelim. CPU kullanımına göre otomatik ölçeklendirme ekleyelim:

gcloud compute instance-groups managed set-autoscaling web-server-mig 
  --region=$REGION 
  --min-num-replicas=2 
  --max-num-replicas=10 
  --target-cpu-utilization=0.70 
  --cool-down-period=90 
  --scale-in-control=max-scaled-in-replicas=2,time-window=120

# Autoscaling politikasını doğrula
gcloud compute instance-groups managed describe web-server-mig 
  --region=$REGION 
  --format="yaml(autoscaler)"

Parametrelerin anlamı:

  • –min-num-replicas: En az 2 instance her zaman çalışsın, sıfıra inmesin
  • –max-num-replicas: En fazla 10 instance’a kadar çıkabilir
  • –target-cpu-utilization: CPU %70’e ulaşınca scale-out başlar
  • –cool-down-period: Yeni instance başladıktan 90 saniye sonra metriklere dahil et, startup süresini hesaba kat
  • –scale-in-control: Scale-in sırasında 2 dakikada maksimum 2 instance azalt, agresif scale-in’i önler

HTTP yük tabanlı autoscaling da ekleyebilirsin, özellikle load balancer kullanıyorsan:

# Load balancing utilization tabanlı autoscaling
gcloud compute instance-groups managed set-autoscaling web-server-mig 
  --region=$REGION 
  --min-num-replicas=2 
  --max-num-replicas=10 
  --update-stackdriver-metric=compute.googleapis.com/instance/network/received_bytes_count 
  --stackdriver-metric-utilization-target=1000000 
  --stackdriver-metric-utilization-target-type=DELTA_PER_SECOND

Health Check Tanımlama

MIG’in arızalı instance’ları tespit edip yenilemesi için health check şart:

# HTTP health check oluştur
gcloud compute health-checks create http web-server-health-check 
  --port=80 
  --request-path=/health 
  --check-interval=10s 
  --timeout=5s 
  --healthy-threshold=2 
  --unhealthy-threshold=3 
  --description="Web sunucusu HTTP health check"

# Health check'i MIG'e bağla
gcloud compute instance-groups managed update web-server-mig 
  --region=$REGION 
  --health-checks=web-server-health-check 
  --initial-delay=120

# Health check durumunu gör
gcloud compute instance-groups managed list-instances web-server-mig 
  --region=$REGION 
  --format="table(name,zone,status,healthStatus)"

–initial-delay: Instance başladıktan 120 saniye boyunca health check başarısız olsa bile instance’ı silme. Startup script’in tamamlanması için zaman ver.

Health check parametreleri:

  • –check-interval: Her 10 saniyede bir kontrol et
  • –timeout: 5 saniye içinde yanıt gelmezse başarısız say
  • –healthy-threshold: 2 ardışık başarılı yanıt alınca HEALTHY işaretle
  • –unhealthy-threshold: 3 ardışık başarısız yanıt sonrası UNHEALTHY say ve yenile

Rolling Update ile Deployment

Gerçek dünyada en çok işe yarayan özellik bu. Uygulamayı güncellerken mevcut trafiği kesintisiz sürdürmek.

Önce yeni bir template oluştur:

# V2 template - örneğin nginx sürümünü güncelledik diyelim
gcloud compute instance-templates create web-server-template-v2 
  --machine-type=e2-medium 
  --image-family=debian-11 
  --image-project=debian-cloud 
  --boot-disk-size=20GB 
  --boot-disk-type=pd-balanced 
  --tags=http-server,https-server 
  --metadata=startup-script='#!/bin/bash
apt-get update -y
apt-get install -y nginx
echo "V2 deployment" > /var/www/html/version.txt
systemctl enable nginx && systemctl start nginx
echo "OK" > /var/www/html/health' 
  --region=$REGION 
  --description="Web sunucusu MIG template - v2"

Şimdi rolling update başlat:

gcloud compute instance-groups managed rolling-action start-update web-server-mig 
  --version=template=web-server-template-v2 
  --max-surge=1 
  --max-unavailable=0 
  --region=$REGION 
  --type=proactive

# Update durumunu izle
watch -n 5 'gcloud compute instance-groups managed list-instances web-server-mig 
  --region=europe-west1 
  --format="table(name,zone,status,currentAction,version.instanceTemplate)"'

Rolling update parametreleri:

  • –max-surge=1: Update sırasında en fazla 1 ekstra instance oluştur
  • –max-unavailable=0: Hiçbir instance kullanılamaz durumda olmasın, zero-downtime garantisi
  • –type=proactive: GCP aktif olarak eski instance’ları güncellesin, opportunistic seçersen sadece yeni instance’lar yeni template’le gelir

Eğer update sırasında sorun çıkarsa geri al:

# Rollback - önceki template'e dön
gcloud compute instance-groups managed rolling-action start-update web-server-mig 
  --version=template=web-server-template-v1 
  --max-surge=1 
  --max-unavailable=0 
  --region=$REGION

Canary Deployment Stratejisi

Production’da %100 güvenli deployment için canary kullan. Önce trafiğin %20’sini yeni versiyona yönlendir, sorun yoksa devam et:

# %20 canary deployment
gcloud compute instance-groups managed rolling-action start-update web-server-mig 
  --version=template=web-server-template-v1,name=stable 
  --canary-version=template=web-server-template-v2,target-size=20%,name=canary 
  --region=$REGION

# Canary sağlıklıysa %100'e çıkar
gcloud compute instance-groups managed rolling-action start-update web-server-mig 
  --version=template=web-server-template-v2 
  --region=$REGION

# Sorun varsa canary'yi iptal et
gcloud compute instance-groups managed rolling-action stop-proactive-update web-server-mig 
  --region=$REGION

Instance Yönetimi ve Troubleshooting

Günlük operasyonlarda sık kullanacağın komutlar:

# Belirli bir instance'a SSH bağlan (instance adını MIG listesinden al)
gcloud compute ssh web-server-mig-XXXX --zone=europe-west1-b

# Tüm instance'lara komut gönder (ops automation için)
for instance in $(gcloud compute instance-groups managed list-instances web-server-mig 
  --region=$REGION --format="value(name,zone)" | awk '{print $1"/"$2}'); do
  name=$(echo $instance | cut -d'/' -f1)
  zone=$(echo $instance | cut -d'/' -f2)
  echo "--- $name ($zone) ---"
  gcloud compute ssh $name --zone=$zone --command="systemctl status nginx" --quiet 2>/dev/null
done

# Belirli bir instance'ı MIG'den ayırarak debug et
gcloud compute instance-groups managed abandon-instances web-server-mig 
  --instances=web-server-mig-XXXX 
  --region=$REGION

# Grup boyutunu manuel olarak ayarla (autoscaling geçici olarak devre dışı)
gcloud compute instance-groups managed resize web-server-mig 
  --size=5 
  --region=$REGION

# Tüm instance'ları yeniden oluştur (rolling restart)
gcloud compute instance-groups managed rolling-action replace web-server-mig 
  --max-surge=1 
  --max-unavailable=0 
  --region=$REGION

MIG log’larını Cloud Logging’den çekebilirsin:

# Startup script loglarını gör
gcloud logging read "resource.type=gce_instance AND logName=projects/$PROJECT_ID/logs/startupscript" 
  --limit=20 
  --format="table(timestamp,resource.labels.instance_id,textPayload)"

Stateful MIG Kullanımı

Bazı uygulamalar durum bilgisi tutar, örneğin bir veritabanı cluster’ı. Stateful MIG bu ihtiyacı karşılar:

# Stateful policy ile MIG oluştur
gcloud compute instance-groups managed create stateful-mig 
  --template=web-server-template-v1 
  --size=3 
  --region=$REGION 
  --stateful-disk=device-name=persistent-disk-0,auto-delete=never

# Instance'a stateful metadata ekle
gcloud compute instance-groups managed set-stateful-policy stateful-mig 
  --region=$REGION 
  --stateful-disk=device-name=persistent-disk-0,auto-delete=on-permanent-instance-deletion

Stateful MIG’de instance silinse bile diski korunur ve yeni instance aynı diski bağlar. Redis, MySQL gibi stateful workload’lar için kritik.

Monitoring ve Alerting Kurulumu

MIG’i kurduktan sonra izlemek şart. Temel metrikleri Cloud Monitoring’e ekle:

# MIG için notification channel oluştur (email)
gcloud alpha monitoring channels create 
  --display-name="MIG Alert Email" 
  --type=email 
  [email protected]

# Alerting policy - instance count düşerse uyar
cat > alert-policy.json << 'EOF'
{
  "displayName": "MIG Instance Count Alert",
  "conditions": [{
    "displayName": "Instance count too low",
    "conditionThreshold": {
      "filter": "metric.type="compute.googleapis.com/instance_group/size" resource.type="instance_group"",
      "comparison": "COMPARISON_LT",
      "thresholdValue": 2,
      "duration": "60s",
      "aggregations": [{
        "alignmentPeriod": "60s",
        "perSeriesAligner": "ALIGN_MEAN"
      }]
    }
  }],
  "alertStrategy": {
    "autoClose": "604800s"
  }
}
EOF

gcloud alpha monitoring policies create --policy-from-file=alert-policy.json

Maliyet Optimizasyonu: Spot Instance Kullanımı

Test ortamları veya fault-tolerant uygulamalar için spot (preemptible) instance’lar ciddi maliyet tasarrufu sağlar:

# Spot instance template oluştur
gcloud compute instance-templates create web-server-spot-template 
  --machine-type=e2-medium 
  --image-family=debian-11 
  --image-project=debian-cloud 
  --provisioning-model=SPOT 
  --instance-termination-action=STOP 
  --tags=http-server 
  --metadata-from-file=startup-script=startup-script.sh 
  --region=$REGION 
  --description="Spot instance template - maliyet optimizasyonu"

# Mixed MIG: %30 spot, %70 normal instance
gcloud compute instance-groups managed create mixed-mig 
  --template=web-server-template-v1 
  --size=5 
  --region=$REGION

gcloud compute instance-groups managed update mixed-mig 
  --region=$REGION 
  --instance-redistribution-type=PROACTIVE

Spot instance kullanırken dikkat edilmesi gerekenler:

  • Stateless uygulama zorunlu: GCP istediği zaman bu instance’ları durdurabilir
  • Graceful shutdown handler yaz: SIGTERM aldığında bağlantıları temizle
  • Min-replicas en az 1 normal instance: Hiç spot bulunamazsa uygulama çalışsın
  • max-unavailable değerini artır: Spot instance’lar ani kapanabileceğinden toleransı yüksek tut

Sonuç

GCP Managed Instance Group, “ben bu VM’i elle yönetirim” anlayışından “altyapı kendini yönetsin” anlayışına geçişin en somut adımı. Bu yazıda ele aldığımız temel akışı özetleyelim: instance template hazırla, MIG oluştur, autoscaling politikasını tanımla, health check bağla, rolling update ile deployment yap.

Production’a geçmeden önce kontrol listen şöyle olsun:

  • Regional MIG kullan, single zone’da kalma
  • Health check initial delay’i startup sürenden uzun tut
  • max-unavailable=0 ile zero-downtime sağla
  • Autoscaling cool-down süresini uygulamanın gerçek startup süresine göre ayarla
  • Spot instance kullanıyorsan graceful shutdown senaryosunu test et
  • MIG instance’larına doğrudan SSH yerine managed SSH ya da bastion host kullan

MIG tek başına yeterli değil, önünde bir Load Balancer olmadan instance’lara doğrudan erişim production’da antipattern. Bir sonraki adım olarak HTTP(S) Load Balancer kurulumu ve MIG backend service entegrasyonuna bakmanı öneririm. O noktada gerçekten production-grade bir altyapın olacak.

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir