</>Học Dev
Bài học

Tuần 2 - Ngày 3: EC2 Storage và EBS

Tuần 2 – Ngày 3

Tuần 2 - Ngày 3: EC2 Storage và EBS

Mục tiêu học tập

  • Hiểu các loại storage cho EC2: EBS, Instance Store, EFS
  • Phân biệt 6 loại EBS volume types và use case
  • Nắm snapshot, encryption, multi-attach
  • Biết khi nào dùng Instance Store vs EBS

1. Storage Options cho EC2

EC2StorageOptionsEBS(ElasticBlockStore)Network-attachedblockstoragePersistent(snglâuhơnEC2)1EBS=1AZ1EBSattach1EC2(trio1/io2Multi-Attach)InstanceStore(NVMeSSD)PhysicalstoragegnlinvihostEPHEMERALmtkhistop/terminatePerformancecaonht(sub-mslatency)Free(includedtronginstanceprice)EFS(ElasticFileSystem)NetworkfilesystemNFSv4,sharedacrossmultipleEC2Multi-AZTđngscale(noprovisioning)FSx(ManagedFS:Windows,Lustre,NetApp,OpenZFS)S3(Objectstorage)Khôngphiblockstorage,dùngAPI/HTTP

2. EBS Overview

Đặc điểm

  • Block-level storage, attach to EC2 như external drive
  • Network-attached (qua AWS internal network)
  • Persistent: sống lâu hơn EC2, có thể detach/attach
  • AZ-bound: 1 EBS volume tồn tại trong 1 AZ duy nhất
  • 1:1 attachment: 1 volume → 1 EC2 (trừ Multi-Attach io1/io2)
  • Hỗ trợ resize, snapshot, encryption

EBS Lifecycle

TovolumeAttachtoEC2Use(mount)DetachRe-attach(cùngAZ)DeleteSnapshottoS3(cross-AZ,cross-Region)

3. EBS Volume Types (6 loại chính)

SSD-based (cho transactional, boot volume)

TypeUse caseSizeIOPS maxThroughput max
gp3General purpose (mặc định mới)1 GB - 16 TB16,000 (independent)1,000 MB/s (independent)
gp2General purpose (cũ)1 GB - 16 TB3-16,000 (tied to size)250 MB/s
io2 Block ExpressHighest performance4 GB - 64 TB256,0004,000 MB/s
io2Critical, mission-critical4 GB - 16 TB64,0001,000 MB/s
io1Legacy high-IOPS4 GB - 16 TB64,0001,000 MB/s

HDD-based (cho throughput, không dùng boot volume)

TypeUse caseSizeIOPS maxThroughput max
st1Throughput-optimized (big data, log)125 GB - 16 TB500500 MB/s
sc1Cold storage (infrequent access)125 GB - 16 TB250250 MB/s

gp2 vs gp3 (quan trọng cho thi)

gp2gp3
IOPS3 IOPS/GB (max 16,000) — tied to size3,000 baseline (provision up to 16,000 độc lập với size)
Throughput250 MB/s125 MB/s baseline, provision up to 1,000 MB/s
GiáCao hơn gp3 ~20%Rẻ hơn
Khuyến nghịLegacyMặc định mới — chọn gp3

io1/io2 Multi-Attach

  • Cho phép 1 volume attach vào tối đa 16 EC2 instances trong cùng AZ
  • Use case: Cluster file system (chỉ với cluster-aware FS như GFS2)
  • KHÔNG dùng cho ext4, XFS (không cluster-aware)

4. EBS Volume Selection Decision Tree

Workload?Bootvolume?gp3(mcđnh,rhơngp2)Databasetransactional(Oracle,SQLServer)?<16,000IOPSgp3>16,000IOPSio2/io2BlockExpressMission-criticalviSLA?io2BlockExpress(99.999%durability)Bigdata,logprocessing(sequentialthroughput)?st1(HDD,cheapthroughput)ColdarchivetrênEC2?sc1Sharedblockstorage(cluster)?io1/io2Multi-Attach

5. EBS Snapshots

Định nghĩa

Snapshot = backup point-in-time của EBS volume, lưu vào S3 (managed bởi AWS, không expose bucket).

Đặc điểm

  • Incremental: snapshot sau chỉ lưu block changed
  • Lưu cross-AZ trong cùng Region (snapshot survives AZ failure)
  • Copy cross-Region để DR
  • Share cross-account (encrypted snapshot cần share KMS key)
  • Restore volume từ snapshot vào bất kỳ AZ trong Region

Snapshot lifecycle với DLM (Data Lifecycle Manager)

  • Tự động tạo snapshot theo lịch
  • Tự động retention (giữ N snapshots gần nhất)
  • Tag-based: snapshot resources có tag cụ thể

EBS Snapshot Archive (2021+)

  • Tier rẻ hơn cho snapshot lưu lâu (75% rẻ hơn standard)
  • Minimum retention 90 ngày
  • Restore từ archive mất 24-72 hours
  • Use case: Compliance, archive

Recycle Bin for EBS Snapshots

  • "Undo delete" cho snapshot
  • Set retention 1-365 ngày
  • Bảo vệ chống xóa nhầm/malicious

6. EBS Encryption

Đặc điểm

  • Encryption at-rest và in-transit giữa EC2 ↔ EBS
  • Dùng KMS (Customer Managed Key hoặc AWS managed)
  • Tạo volume encrypted: enable trong console hoặc default cho region

Cách encrypt unencrypted volume

  1. Tạo snapshot của volume
  2. Copy snapshot với option Encrypted=true (chọn KMS key)
  3. Tạo volume mới từ encrypted snapshot
  4. Detach old volume, attach new

EBS Encryption by Default

  • Setting tại region level: tất cả EBS mới sẽ encrypted
  • Khuyến nghị enable ngay từ đầu cho account mới

7. EBS Performance

Burst (gp2)

  • gp2 tích lũy I/O credits khi idle
  • Burst lên 3,000 IOPS khi cần
  • Hết credits → giảm về baseline

gp3 / io2 — không burst, đảm bảo baseline luôn

EBS-Optimized Instance

  • Hầu hết instance mới (m5, c5, r5...) đều EBS-optimized by default
  • Dedicated bandwidth giữa EC2 ↔ EBS, không bị share với traffic mạng khác

8. Instance Store

Định nghĩa

Instance Store là physical storage (NVMe SSD) gắn liền với physical host của EC2.

Đặc điểm

  • Ephemeral: data MẤT khi:
    • Instance stop (start lại sang host khác)
    • Instance terminate
    • Hardware failure
  • PERSISTENT qua reboot (cùng host)
  • Performance cực cao: sub-ms latency, hàng triệu IOPS
  • Free: included trong giá instance
  • Không snapshot được

Use case

  • Cache (Redis, Memcached) — data có thể rebuild
  • Temporary storage (scratch space)
  • Buffer cho HPC workload
  • NoSQL với replication (Cassandra, NoSQL clustered)

Instance Store-supported families

  • I-family (i3, i4i): storage optimized với NVMe SSD lớn
  • D-family (d2, d3): HDD instance store
  • Một số general purpose: m5d, m5ad, c5d, r5d

9. EFS Brief (chi tiết Tuần 3)

Định nghĩa

EFS (Elastic File System) = managed NFSv4 file system, shared giữa multiple EC2 và across AZs.

So sánh nhanh EBS vs EFS vs Instance Store

EBSEFSInstance Store
TypeBlockFile (NFS)Block
Shared1:1 (Multi-Attach: 16)Many EC21:1
Multi-AZNo (per AZ)YesNo
PersistenceYesYesNo (ephemeral)
Performancegp3: 16k IOPSMulti-GB/sHàng triệu IOPS
Use caseDB, boot volumeShared file storageCache, scratch

10. EBS Multi-Attach (io1/io2 only)

Định nghĩa

1 volume io1/io2 attach vào tối đa 16 EC2 instances trong cùng AZ, giúp build shared block storage.

Điều kiện

  • io1 hoặc io2 volume
  • Cùng Availability Zone
  • Cùng VPC (Nitro-based instances)
  • Filesystem phải cluster-aware (GFS2, OCFS2) — KHÔNG dùng ext4/XFS

Use case

  • Cluster databases với shared storage
  • Active-active HA application
  • Hiếm dùng — thường thay bằng EFS hoặc database replication

Câu hỏi ôn tập

  1. Khi EC2 stop, dữ liệu trên Instance Store có còn không?

    Xem đáp án

    Không. Instance Store là ephemeral storage — data mất khi instance stop, terminate, hoặc host hardware fail. Khi reboot thì data vẫn còn. Instance Store phù hợp cho temporary data, buffer, cache, scratch space. Dữ liệu quan trọng phải lưu trên EBS hoặc S3.

  2. Khi nào nên chọn gp3 thay vì gp2?

    Xem đáp án

    Hầu như luôn chọn gp3 — gp3 cho phép cấu hình IOPS (3,000-16,000) và throughput (125-1,000 MB/s) độc lập với storage size, trong khi gp2 buộc IOPS tỷ lệ với size (3 IOPS/GB). gp3 rẻ hơn gp2 ~20% cho cùng size. Chỉ giữ gp2 nếu đang dùng và không cần thay đổi, hoặc có hệ thống không tương thích.

  3. EBS snapshot lưu ở đâu? Cross-AZ hay không?

    Xem đáp án

    EBS snapshot lưu trong Amazon S3 (AWS-managed, không phải bucket của bạn). Snapshot có phạm vi Region — có thể tạo volume từ snapshot ở bất kỳ AZ nào trong cùng Region. Để copy sang Region khác dùng aws ec2 copy-snapshot. Điều này cho phép tạo volume ở AZ khác từ snapshot — quan trọng cho DR và migration.

  4. Encrypt unencrypted EBS volume cần qua bước gì?

    Xem đáp án

    Không thể encrypt trực tiếp volume đang dùng. Phải: (1) Tạo snapshot của volume unencrypted, (2) Copy snapshot với encryption enabled để tạo encrypted snapshot, (3) Tạo volume mới từ encrypted snapshot, (4) Attach volume mới vào instance. Alternatively: enable "EBS encryption by default" cho account/Region — tất cả volumes/snapshots mới tự động encrypted.

  5. EBS volume io2 Multi-Attach có thể dùng với ext4 không?

    Xem đáp án

    Không. Multi-Attach cho phép attach cùng một io1/io2 volume vào nhiều EC2 instances cùng AZ, nhưng yêu cầu ứng dụng tự quản lý concurrent write access. Standard filesystems như ext4 không hỗ trợ concurrent multi-writer — sẽ bị data corruption. Cần dùng cluster-aware filesystem như GFS2, OCFS2, hoặc ứng dụng tự xử lý coordination (như Oracle RAC).

Bài tập thực hành

  • Tạo gp3 EBS volume 10 GB, attach vào EC2, format và mount
  • Tạo snapshot, sau đó tạo volume mới từ snapshot ở AZ khác
  • Enable EBS encryption by default cho region
  • Test Instance Store: launch m5d.large, check /dev/nvme*, write file, stop+start, verify data mất
  • Setup DLM tự động snapshot hằng ngày, giữ 7 snapshots

Tài liệu tham khảo chính thức


Tiếp theo: Elastic Load Balancing (ELB)