/엔지니어/클라우드/GCP Compute Engine 기초 — VM 생
클라우드초급gcpcompute-enginevm

GCP Compute Engine 기초 — VM 생성·방화벽·SSH·스냅샷

GCP Compute Engine에서 VM 인스턴스를 생성하고, 방화벽 규칙을 설정하며, SSH 접속, 디스크 스냅샷, 머신 이미지를 관리하는 방법을 설명합니다.

gcloud CLI 설치 및 초기화

Bash
# 설치 (Linux)
curl https://sdk.cloud.google.com | bash
exec -l $SHELL

# 초기화 (프로젝트·계정 설정)
gcloud init

# 현재 설정 확인
gcloud config list
gcloud auth list

VM 인스턴스 생성

Bash
# 기본 VM 생성
gcloud compute instances create my-vm \
  --project=my-project-id \
  --zone=asia-northeast3-a \
  --machine-type=e2-medium \
  --image-family=ubuntu-2204-lts \
  --image-project=ubuntu-os-cloud \
  --boot-disk-size=20GB \
  --boot-disk-type=pd-ssd \
  --tags=web-server \
  --metadata=startup-script='#!/bin/bash
    apt update && apt install -y nginx'

# 인스턴스 목록
gcloud compute instances list

# 인스턴스 상세 정보
gcloud compute instances describe my-vm --zone=asia-northeast3-a

# 시작 / 중지 / 삭제
gcloud compute instances start my-vm --zone=asia-northeast3-a
gcloud compute instances stop my-vm --zone=asia-northeast3-a
gcloud compute instances delete my-vm --zone=asia-northeast3-a

방화벽 규칙 설정

Bash
# HTTP / HTTPS 허용 (web-server 태그)
gcloud compute firewall-rules create allow-web \
  --allow tcp:80,tcp:443 \
  --source-ranges 0.0.0.0/0 \
  --target-tags web-server \
  --description "Allow HTTP and HTTPS"

# 특정 IP에서 SSH 허용
gcloud compute firewall-rules create allow-ssh-from-office \
  --allow tcp:22 \
  --source-ranges 203.0.113.0/24 \
  --target-tags web-server

# 내부 통신 허용 (VPC 내부)
gcloud compute firewall-rules create allow-internal \
  --allow all \
  --source-ranges 10.0.0.0/8 \
  --network default

# 방화벽 규칙 목록
gcloud compute firewall-rules list

SSH 접속

Bash
# gcloud로 SSH (키 자동 관리)
gcloud compute ssh my-vm --zone=asia-northeast3-a

# 외부 IP 확인
gcloud compute instances describe my-vm \
  --zone=asia-northeast3-a \
  --format='get(networkInterfaces[0].accessConfigs[0].natIP)'

# 프로젝트 SSH 키 등록
gcloud compute project-info add-metadata \
  --metadata ssh-keys="myuser:$(cat ~/.ssh/id_rsa.pub)"

# 포트포워딩
gcloud compute ssh my-vm --zone=asia-northeast3-a \
  -- -L 5432:localhost:5432

디스크 스냅샷

Bash
# 스냅샷 생성
gcloud compute disks snapshot my-vm \
  --zone=asia-northeast3-a \
  --snapshot-names my-vm-snap-$(date +%Y%m%d)

# 스냅샷 목록
gcloud compute snapshots list

# 스냅샷에서 디스크 복원
gcloud compute disks create restored-disk \
  --source-snapshot my-vm-snap-20250527 \
  --zone=asia-northeast3-a

# 스냅샷 삭제
gcloud compute snapshots delete my-vm-snap-20250527

# 자동 스냅샷 정책
gcloud compute resource-policies create snapshot-schedule daily-backup \
  --region=asia-northeast3 \
  --max-retention-days=7 \
  --daily-schedule \
  --start-time=03:00

머신 타입 변경 (스케일업)

Bash
# 중지 후 변경
gcloud compute instances stop my-vm --zone=asia-northeast3-a

gcloud compute instances set-machine-type my-vm \
  --zone=asia-northeast3-a \
  --machine-type=e2-standard-4

gcloud compute instances start my-vm --zone=asia-northeast3-a

비용 절감 팁

  • 선점형(Preemptible) 인스턴스: 최대 80% 저렴, 언제든 중단 가능
  • 약정 사용 할인: 1년/3년 약정으로 최대 57% 할인
  • e2 계열 머신 타입이 n1 대비 저렴하고 성능 좋음
  • 사용하지 않는 VM은 중지 (디스크 비용만 발생)
#gcp#compute-engine#vm#방화벽#ssh#스냅샷
편집 안내 · Editorial Note

이 가이드는 AI 도구를 활용해 초안을 구성하고 사람이 명령어·문맥을 검토해 발행했습니다. 운영체제와 도구 버전에 따라 결과가 달라질 수 있으므로 적용 전 공식 문서를 함께 확인하세요. 오류를 발견하시면 이메일로 제보해 주세요.

질문 & 답변 (Q&A)

이 가이드에 대해 궁금한 점을 질문해보세요. 확인 후 답변드립니다.