๊ฐ์ ๋จธ์ (EC2 / Compute Engine)
๊ฐ์ ๋จธ์ (EC2 / Compute Engine)¶
1. ๊ฐ์ ๋จธ์ ๊ฐ์¶
๊ฐ์ ๋จธ์ (VM)์ ํด๋ผ์ฐ๋์์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ์ปดํจํ ๋ฆฌ์์ค์ ๋๋ค.
1.1 ์๋น์ค ๋น๊ต¶
| ํญ๋ชฉ | AWS EC2 | GCP Compute Engine |
|---|---|---|
| ์๋น์ค๋ช | Elastic Compute Cloud | Compute Engine |
| ์ธ์คํด์ค ๋จ์ | Instance | Instance |
| ์ด๋ฏธ์ง | AMI | Image |
| ์ธ์คํด์ค ์ ํ | Instance Types | Machine Types |
| ์์ ์คํฌ๋ฆฝํธ | User Data | Startup Script |
| ๋ฉํ๋ฐ์ดํฐ | Instance Metadata | Metadata Server |
2. ์ธ์คํด์ค ์ ํ¶
2.1 AWS EC2 ์ธ์คํด์ค ์ ํ¶
๋ช
๋ช
๊ท์น: {ํจ๋ฐ๋ฆฌ}{์ธ๋}{์ถ๊ฐ์์ฑ}.{ํฌ๊ธฐ}
์: t3.medium, m5.xlarge, c6i.2xlarge
| ํจ๋ฐ๋ฆฌ | ์ฉ๋ | ์์ |
|---|---|---|
| t | ๋ฒ์ฉ (๋ฒ์คํฐ๋ธ) | t3.micro, t3.small |
| m | ๋ฒ์ฉ (๊ท ํ) | m5.large, m6i.xlarge |
| c | ์ปดํจํ ์ต์ ํ | c5.xlarge, c6i.2xlarge |
| r | ๋ฉ๋ชจ๋ฆฌ ์ต์ ํ | r5.large, r6i.xlarge |
| i | ์คํ ๋ฆฌ์ง ์ต์ ํ | i3.large, i3en.xlarge |
| g/p | GPU | g4dn.xlarge, p4d.24xlarge |
์ฃผ์ ์ธ์คํด์ค ์คํ:
| ์ ํ | vCPU | ๋ฉ๋ชจ๋ฆฌ | ๋คํธ์ํฌ | ์ฉ๋ |
|---|---|---|---|---|
| t3.micro | 2 | 1 GB | Low | ๋ฌด๋ฃ ํฐ์ด, ๊ฐ๋ฐ |
| t3.medium | 2 | 4 GB | Low-Mod | ์๊ท๋ชจ ์ฑ |
| m5.large | 2 | 8 GB | Up to 10 Gbps | ๋ฒ์ฉ |
| c5.xlarge | 4 | 8 GB | Up to 10 Gbps | CPU ์ง์ฝ |
| r5.large | 2 | 16 GB | Up to 10 Gbps | ๋ฉ๋ชจ๋ฆฌ ์ง์ฝ |
2.2 GCP Machine Types¶
๋ช
๋ช
๊ท์น: {์๋ฆฌ์ฆ}-{์ ํ}-{vCPU์} ๋๋ ์ปค์คํ
์: e2-medium, n2-standard-4, c2-standard-8
| ์๋ฆฌ์ฆ | ์ฉ๋ | ์์ |
|---|---|---|
| e2 | ๋น์ฉ ํจ์จ ๋ฒ์ฉ | e2-micro, e2-medium |
| n2/n2d | ๋ฒ์ฉ (๊ท ํ) | n2-standard-2, n2-highmem-4 |
| c2/c2d | ์ปดํจํ ์ต์ ํ | c2-standard-4 |
| m1/m2 | ๋ฉ๋ชจ๋ฆฌ ์ต์ ํ | m1-megamem-96 |
| a2 | GPU (A100) | a2-highgpu-1g |
์ฃผ์ ๋จธ์ ํ์ ์คํ:
| ์ ํ | vCPU | ๋ฉ๋ชจ๋ฆฌ | ๋คํธ์ํฌ | ์ฉ๋ |
|---|---|---|---|---|
| e2-micro | 0.25-2 | 1 GB | 1 Gbps | ๋ฌด๋ฃ ํฐ์ด |
| e2-medium | 1-2 | 4 GB | 2 Gbps | ์๊ท๋ชจ ์ฑ |
| n2-standard-2 | 2 | 8 GB | 10 Gbps | ๋ฒ์ฉ |
| c2-standard-4 | 4 | 16 GB | 10 Gbps | CPU ์ง์ฝ |
| n2-highmem-2 | 2 | 16 GB | 10 Gbps | ๋ฉ๋ชจ๋ฆฌ ์ง์ฝ |
2.3 ์ปค์คํ ๋จธ์ ํ์ (GCP)¶
GCP์์๋ vCPU์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ฐ๋ณ ์ง์ ํ ์ ์์ต๋๋ค.
# ์ปค์คํ
๋จธ์ ํ์
์์ฑ
gcloud compute instances create my-instance \
--custom-cpu=6 \
--custom-memory=24GB \
--zone=asia-northeast3-a
3. ์ด๋ฏธ์ง (AMI / Image)¶
3.1 AWS AMI¶
AMI (Amazon Machine Image) ๊ตฌ์ฑ์์: - ๋ฃจํธ ๋ณผ๋ฅจ ํ ํ๋ฆฟ (OS, ์ ํ๋ฆฌ์ผ์ด์ ) - ์ธ์คํด์ค ์ ํ, ๋ณด์ ๊ทธ๋ฃน ๊ธฐ๋ณธ๊ฐ - ๋ธ๋ก ๋๋ฐ์ด์ค ๋งคํ
# ์ฌ์ฉ ๊ฐ๋ฅํ AMI ๊ฒ์ (Amazon Linux 2023)
aws ec2 describe-images \
--owners amazon \
--filters "Name=name,Values=al2023-ami-*-x86_64" \
--query 'Images | sort_by(@, &CreationDate) | [-1]'
# ์ฃผ์ AMI ์ ํ
# Amazon Linux 2023: al2023-ami-*
# Ubuntu 22.04: ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-*
# Windows Server: Windows_Server-2022-*
3.2 GCP Images¶
# ์ฌ์ฉ ๊ฐ๋ฅํ ์ด๋ฏธ์ง ๋ชฉ๋ก
gcloud compute images list
# ํน์ ํ๋ก์ ํธ์ ์ด๋ฏธ์ง
gcloud compute images list \
--filter="family:ubuntu-2204-lts"
# ์ฃผ์ ์ด๋ฏธ์ง ํจ๋ฐ๋ฆฌ
# debian-11, debian-12
# ubuntu-2204-lts, ubuntu-2404-lts
# centos-stream-9, rocky-linux-9
# windows-2022
4. ์ธ์คํด์ค ์์ฑ¶
4.1 AWS EC2 ์ธ์คํด์ค ์์ฑ¶
Console: 1. EC2 ๋์๋ณด๋ โ "Launch instance" 2. ์ด๋ฆ ์ ๋ ฅ 3. AMI ์ ํ (์: Amazon Linux 2023) 4. ์ธ์คํด์ค ์ ํ ์ ํ (์: t3.micro) 5. ํค ํ์ด ์์ฑ/์ ํ 6. ๋คํธ์ํฌ ์ค์ (VPC, ์๋ธ๋ท, ๋ณด์ ๊ทธ๋ฃน) 7. ์คํ ๋ฆฌ์ง ์ค์ 8. "Launch instance"
AWS CLI:
# ํค ํ์ด ์์ฑ
aws ec2 create-key-pair \
--key-name my-key \
--query 'KeyMaterial' \
--output text > my-key.pem
chmod 400 my-key.pem
# ์ธ์คํด์ค ์์ฑ
aws ec2 run-instances \
--image-id ami-0c55b159cbfafe1f0 \
--instance-type t3.micro \
--key-name my-key \
--security-group-ids sg-12345678 \
--subnet-id subnet-12345678 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=MyServer}]'
4.2 GCP Compute Engine ์ธ์คํด์ค ์์ฑ¶
Console: 1. Compute Engine โ VM ์ธ์คํด์ค โ "๋ง๋ค๊ธฐ" 2. ์ด๋ฆ ์ ๋ ฅ 3. ๋ฆฌ์ /Zone ์ ํ 4. ๋จธ์ ๊ตฌ์ฑ ์ ํ (์: e2-medium) 5. ๋ถํ ๋์คํฌ (OS ์ด๋ฏธ์ง ์ ํ) 6. ๋ฐฉํ๋ฒฝ ์ค์ (HTTP/HTTPS ํ์ฉ) 7. "๋ง๋ค๊ธฐ"
gcloud CLI:
# ์ธ์คํด์ค ์์ฑ
gcloud compute instances create my-instance \
--zone=asia-northeast3-a \
--machine-type=e2-medium \
--image-family=ubuntu-2204-lts \
--image-project=ubuntu-os-cloud \
--boot-disk-size=20GB \
--tags=http-server,https-server
# SSH ํค๋ ์๋ ๊ด๋ฆฌ (OS Login ๋๋ ํ๋ก์ ํธ ๋ฉํ๋ฐ์ดํฐ)
5. SSH ์ ์¶
5.1 AWS EC2 SSH ์ ์¶
# ํผ๋ธ๋ฆญ IP ํ์ธ
aws ec2 describe-instances \
--instance-ids i-1234567890abcdef0 \
--query 'Reservations[0].Instances[0].PublicIpAddress'
# SSH ์ ์
ssh -i my-key.pem ec2-user@<PUBLIC_IP>
# Amazon Linux: ec2-user
# Ubuntu: ubuntu
# CentOS: centos
# Debian: admin
EC2 Instance Connect (๋ธ๋ผ์ฐ์ ): 1. EC2 Console โ ์ธ์คํด์ค ์ ํ 2. "์ฐ๊ฒฐ" ๋ฒํผ ํด๋ฆญ 3. "EC2 Instance Connect" ํญ 4. "์ฐ๊ฒฐ" ํด๋ฆญ
5.2 GCP SSH ์ ์¶
# gcloud๋ก SSH (ํค ์๋ ๊ด๋ฆฌ)
gcloud compute ssh my-instance --zone=asia-northeast3-a
# ์ธ๋ถ IP ํ์ธ
gcloud compute instances describe my-instance \
--zone=asia-northeast3-a \
--format='get(networkInterfaces[0].accessConfigs[0].natIP)'
# ์ง์ SSH (ํค๋ฅผ ์๋ ๋ฑ๋กํ ๊ฒฝ์ฐ)
ssh -i ~/.ssh/google_compute_engine username@<EXTERNAL_IP>
๋ธ๋ผ์ฐ์ SSH: 1. Compute Engine โ VM ์ธ์คํด์ค 2. ์ธ์คํด์ค ํ์ "SSH" ๋ฒํผ ํด๋ฆญ 3. ์ ์ฐฝ์์ ๋ธ๋ผ์ฐ์ ํฐ๋ฏธ๋ ์ด๋ฆผ
6. User Data / Startup Script¶
์ธ์คํด์ค ์์ ์ ์๋์ผ๋ก ์คํ๋๋ ์คํฌ๋ฆฝํธ์ ๋๋ค.
6.1 AWS User Data¶
#!/bin/bash
# User Data ์์ (Amazon Linux 2023)
# ํจํค์ง ์
๋ฐ์ดํธ
dnf update -y
# Nginx ์ค์น
dnf install -y nginx
systemctl start nginx
systemctl enable nginx
# ์ปค์คํ
ํ์ด์ง
echo "<h1>Hello from $(hostname)</h1>" > /usr/share/nginx/html/index.html
CLI์์ User Data ์ง์ :
aws ec2 run-instances \
--image-id ami-12345678 \
--instance-type t3.micro \
--user-data file://startup.sh \
...
User Data ๋ก๊ทธ ํ์ธ:
# ์ธ์คํด์ค ๋ด๋ถ์์
cat /var/log/cloud-init-output.log
6.2 GCP Startup Script¶
#!/bin/bash
# Startup Script ์์ (Ubuntu)
# ํจํค์ง ์
๋ฐ์ดํธ
apt-get update
# Nginx ์ค์น
apt-get install -y nginx
systemctl start nginx
systemctl enable nginx
# ์ปค์คํ
ํ์ด์ง
echo "<h1>Hello from $(hostname)</h1>" > /var/www/html/index.html
CLI์์ Startup Script ์ง์ :
gcloud compute instances create my-instance \
--zone=asia-northeast3-a \
--machine-type=e2-medium \
--metadata-from-file=startup-script=startup.sh \
...
# ๋๋ ์ธ๋ผ์ธ์ผ๋ก
gcloud compute instances create my-instance \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install -y nginx'
Startup Script ๋ก๊ทธ ํ์ธ:
# ์ธ์คํด์ค ๋ด๋ถ์์
sudo journalctl -u google-startup-scripts.service
# ๋๋
cat /var/log/syslog | grep startup-script
7. ์ธ์คํด์ค ๋ฉํ๋ฐ์ดํฐ¶
์ธ์คํด์ค ๋ด๋ถ์์ ์์ ์ ์ ๋ณด๋ฅผ ์กฐํํ ์ ์์ต๋๋ค.
7.1 AWS Instance Metadata Service (IMDS)¶
# ์ธ์คํด์ค ID
curl http://169.254.169.254/latest/meta-data/instance-id
# ํผ๋ธ๋ฆญ IP
curl http://169.254.169.254/latest/meta-data/public-ipv4
# ๊ฐ์ฉ ์์ญ
curl http://169.254.169.254/latest/meta-data/placement/availability-zone
# IAM ์ญํ ์๊ฒฉ ์ฆ๋ช
curl http://169.254.169.254/latest/meta-data/iam/security-credentials/<role-name>
# IMDSv2 (๊ถ์ฅ - ํ ํฐ ํ์)
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" \
-H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
curl -H "X-aws-ec2-metadata-token: $TOKEN" \
http://169.254.169.254/latest/meta-data/instance-id
7.2 GCP Metadata Server¶
# ์ธ์คํด์ค ์ด๋ฆ
curl -H "Metadata-Flavor: Google" \
http://metadata.google.internal/computeMetadata/v1/instance/name
# ์ธ๋ถ IP
curl -H "Metadata-Flavor: Google" \
http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip
# Zone
curl -H "Metadata-Flavor: Google" \
http://metadata.google.internal/computeMetadata/v1/instance/zone
# ์๋น์ค ๊ณ์ ํ ํฐ
curl -H "Metadata-Flavor: Google" \
http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token
# ํ๋ก์ ํธ ID
curl -H "Metadata-Flavor: Google" \
http://metadata.google.internal/computeMetadata/v1/project/project-id
8. ์ธ์คํด์ค ๊ด๋ฆฌ¶
8.1 ์ธ์คํด์ค ์ํ ๊ด๋ฆฌ¶
AWS:
# ์ธ์คํด์ค ์ค์ง
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
# ์ธ์คํด์ค ์์
aws ec2 start-instances --instance-ids i-1234567890abcdef0
# ์ธ์คํด์ค ์ฌ๋ถํ
aws ec2 reboot-instances --instance-ids i-1234567890abcdef0
# ์ธ์คํด์ค ์ข
๋ฃ (์ญ์ )
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0
# ์ธ์คํด์ค ์ํ ํ์ธ
aws ec2 describe-instance-status --instance-ids i-1234567890abcdef0
GCP:
# ์ธ์คํด์ค ์ค์ง
gcloud compute instances stop my-instance --zone=asia-northeast3-a
# ์ธ์คํด์ค ์์
gcloud compute instances start my-instance --zone=asia-northeast3-a
# ์ธ์คํด์ค ์ฌ์์ (reset)
gcloud compute instances reset my-instance --zone=asia-northeast3-a
# ์ธ์คํด์ค ์ญ์
gcloud compute instances delete my-instance --zone=asia-northeast3-a
# ์ธ์คํด์ค ์ํ ํ์ธ
gcloud compute instances describe my-instance --zone=asia-northeast3-a
8.2 ์ธ์คํด์ค ์ ํ ๋ณ๊ฒฝ¶
AWS:
# 1. ์ธ์คํด์ค ์ค์ง
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
# 2. ์ธ์คํด์ค ์ ํ ๋ณ๊ฒฝ
aws ec2 modify-instance-attribute \
--instance-id i-1234567890abcdef0 \
--instance-type t3.large
# 3. ์ธ์คํด์ค ์์
aws ec2 start-instances --instance-ids i-1234567890abcdef0
GCP:
# 1. ์ธ์คํด์ค ์ค์ง
gcloud compute instances stop my-instance --zone=asia-northeast3-a
# 2. ๋จธ์ ํ์
๋ณ๊ฒฝ
gcloud compute instances set-machine-type my-instance \
--zone=asia-northeast3-a \
--machine-type=n2-standard-4
# 3. ์ธ์คํด์ค ์์
gcloud compute instances start my-instance --zone=asia-northeast3-a
9. ๊ณผ๊ธ ์ต์ ¶
9.1 ์จ๋๋งจ๋ vs ์์ฝ vs ์คํ¶
| ์ต์ | AWS | GCP | ํ ์ธ์จ | ํน์ง |
|---|---|---|---|---|
| ์จ๋๋งจ๋ | On-Demand | On-demand | 0% | ์ฝ์ ์์, ์ ์ฐํจ |
| ์์ฝ | Reserved/Savings Plans | Committed Use | ์ต๋ 72% | 1-3๋ ์ฝ์ |
| ์คํ/์ ์ ํ | Spot Instances | Spot/Preemptible | ์ต๋ 90% | ์ค๋จ ๊ฐ๋ฅ |
| ์๋ ํ ์ธ | - | Sustained Use | ์ต๋ 30% | ์ ์ฌ์ฉ๋ ์๋ |
9.2 AWS Spot Instance¶
# ์คํ ์ธ์คํด์ค ์์ฒญ
aws ec2 request-spot-instances \
--instance-count 1 \
--type "one-time" \
--launch-specification '{
"ImageId": "ami-12345678",
"InstanceType": "t3.large",
"KeyName": "my-key"
}'
# ์คํ ๊ฐ๊ฒฉ ํ์ธ
aws ec2 describe-spot-price-history \
--instance-types t3.large \
--product-descriptions "Linux/UNIX"
9.3 GCP Preemptible/Spot VM¶
# Spot VM ์์ฑ (Preemptible ํ์)
gcloud compute instances create spot-instance \
--zone=asia-northeast3-a \
--machine-type=e2-medium \
--provisioning-model=SPOT \
--instance-termination-action=STOP
# Preemptible VM ์์ฑ (๋ ๊ฑฐ์)
gcloud compute instances create preemptible-instance \
--zone=asia-northeast3-a \
--machine-type=e2-medium \
--preemptible
10. ์ค์ต: ์น ์๋ฒ ๋ฐฐํฌ¶
10.1 AWS EC2 ์น ์๋ฒ¶
# 1. ๋ณด์ ๊ทธ๋ฃน ์์ฑ
aws ec2 create-security-group \
--group-name web-sg \
--description "Web server security group"
# 2. ์ธ๋ฐ์ด๋ ๊ท์น ์ถ๊ฐ
aws ec2 authorize-security-group-ingress \
--group-name web-sg \
--protocol tcp --port 22 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress \
--group-name web-sg \
--protocol tcp --port 80 --cidr 0.0.0.0/0
# 3. EC2 ์ธ์คํด์ค ์์ฑ (User Data ํฌํจ)
aws ec2 run-instances \
--image-id ami-0c55b159cbfafe1f0 \
--instance-type t3.micro \
--key-name my-key \
--security-groups web-sg \
--user-data '#!/bin/bash
dnf update -y
dnf install -y nginx
systemctl start nginx
echo "<h1>AWS EC2 Web Server</h1>" > /usr/share/nginx/html/index.html' \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=WebServer}]'
10.2 GCP Compute Engine ์น ์๋ฒ¶
# 1. ๋ฐฉํ๋ฒฝ ๊ท์น ์์ฑ
gcloud compute firewall-rules create allow-http \
--allow tcp:80 \
--target-tags http-server
# 2. Compute Engine ์ธ์คํด์ค ์์ฑ
gcloud compute instances create web-server \
--zone=asia-northeast3-a \
--machine-type=e2-micro \
--image-family=ubuntu-2204-lts \
--image-project=ubuntu-os-cloud \
--tags=http-server \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install -y nginx
echo "<h1>GCP Compute Engine Web Server</h1>" > /var/www/html/index.html'
# 3. ์ธ๋ถ IP ํ์ธ
gcloud compute instances describe web-server \
--zone=asia-northeast3-a \
--format='get(networkInterfaces[0].accessConfigs[0].natIP)'
11. ๋ค์ ๋จ๊ณ¶
- 05_Serverless_Functions.md - ์๋ฒ๋ฆฌ์ค ํจ์
- 08_Block_and_File_Storage.md - ๋ธ๋ก ์คํ ๋ฆฌ์ง (EBS/PD)