Blockfile 스냅샷터
Blockfile 스냅샷터 (Blockfile Snapshotter)
blockfile 스냅샷터는 스냅샷마다 raw 블록 파일(block file)을 사용해요. 블록 파일은 부모 블록 파일이나 기본이 되는 빈 블록 파일에서 복사해서 만들어요. 마운트하려면 가상 머신이나 루프백 마운트 지원이 필요하죠. 이 문서는 이 스냅샷터의 활용 사례, 대안, 설정 방법, 그리고 내부 동작 원리까지 순서대로 설명해요.
출처: 문서
본문
사용 사례 (Use Case)
스냅샷터는 OCI 이미지 스토어에서 이미지를 추출해 컨테이너에 유용한 스냅샷을 만드는 역할을 해요. 디렉터리를 준비하거나 다른 파일시스템을 세팅하는 것 같은 기반 인프라를 잡아 주고, 레이어들을 적용해 컨테이너 베이스로 쓸 단일 마운트 가능 디렉터리를 만들고, 시작 시 컨테이너에 그것을 마운트하는 일까지 맡아요.
가장 널리 쓰이는 스냅샷터는 overlayfs 스냅샷터로, containerd의 기본값이에요. overlayfs 스냅샷터는 호스트 파일시스템에 디렉터리를 제공하고, 이 디렉터리가 컨테이너에 bind-mount 돼요.
blockfile 스냅샷터가 겨냥하는 사용 사례는 컨테이너가 VM 안에서 실행되는 경우예요. 구체적으로는, OCI 이미지가 보통 컨테이너처럼 컨테이너의 파일시스템 역할을 하되, 컨테이너 자체는 VM 안에서 실행되는 거예요. VM은 호스트의 디렉터리를 bind-mount 할 수 없기 때문에, blockfile 스냅샷터는 스냅샷을 위한 블록 디바이스를 만들어요. 이 블록 디바이스를 VM에 붙이면 콘텐츠를 게스트 안으로 넣을 수 있죠.
대안 (Alternatives)
VM 안으로 디렉터리를 마운트하는 데는 blockfile 스냅샷터 말고 다른 방법도 있어요. 하나는 virtiofs 드라이버를 쓰는 방법으로, VMM이 지원하면 가능해요. 마찬가지로 VMM이 지원한다면 9p를 써서 로컬 디렉터리를 VM에 마운트할 수도 있어요.
추가로 devicemapper 스냅샷터를 쓰면 devicemapper thin-pool의 파일시스템 이미지 위에 스냅샷을 만들 수 있어요.
사용법 (Usage)
blockfile 스냅샷터가 사용 가능한지 확인하기
blockfile 스냅샷터가 사용 가능한지 확인하려면 다음 명령을 실행해요:
$ ctr plugins ls | grep blockfile
설정 (Configuration)
스냅샷터를 설정하려면 containerd config.toml에서 다음 구성 옵션을 쓸 수 있어요. 설정을 바꾼 뒤에는 containerd를 재시작하는 걸 잊지 마세요.
[plugins.'io.containerd.snapshotter.v1.blockfile']
scratch_file = "/opt/containerd/blockfile"
root_path = "/somewhere/on/disk"
fs_type = 'ext4'
mount_options = []
recreate_scratch = true
root_path: 블록 파일이 저장되는 디렉터리예요. 이 디렉터리는 containerd 프로세스가 쓰기 가능해야 해요.scratch_file: 블록 파일의 베이스로 쓸 빈 파일의 경로예요. 이 파일은 스냅샷터를 처음 쓰기 전에 존재해야 해요.fs_type: 블록 파일에 쓸 파일시스템 타입이에요. 현재ext4와xfs를 지원해요.mount_options: 블록 파일을 마운트할 때 쓸 추가 마운트 옵션이에요.recreate_scratch:true면 scratch 파일이 없을 때 스냅샷터가 다시 만들어요.false면 scratch 파일이 없을 때 스냅샷터가 실패해요.
scratch 파일 만들기
scratch 파일은 이렇게 만들 수 있어요. 이 예제는 500MB scratch 파일을 씁니다.
$ # make a 500M file
$ dd if=/dev/zero of=/opt/containerd/blockfile bs=1M count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB, 500 MiB) copied, 1.76253 s, 297 MB/s
$ # format the file with ext4
$ sudo mkfs.ext4 /opt/containerd/blockfile
mke2fs 1.47.0 (5-Feb-2023)
Discarding device blocks: done
Creating filesystem with 512000 1k blocks and 128016 inodes
Filesystem UUID: d9947ecc-722d-4627-9cf9-fa2a3b622106
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
컨테이너 실행하기
blockfile 스냅샷터로 컨테이너를 실행하려면 스냅샷터를 지정해야 해요:
$ # ensure that the image we are using exists; it is a regular OCI image
$ ctr image pull docker.io/library/busybox:latest
$ # run the container with the provides snapshotter
$ ctr run -rm -t --snapshotter blockfile docker.io/library/busybox:latest hello sh
Go 클라이언트 API로 쓰는 방법은 다른 스냅샷터를 쓸 때와 똑같아요:
import (
"context"
"github.com/containerd/containerd"
"github.com/containerd/containerd/snapshots"
)
// create a new client
client, err := containerd.New("/run/containerd/containerd.sock")
snapshotter := "blockfile"
cOpts := []containerd.NewContainerOpts{
containerd.WithImage(image),
containerd.WithImageConfigLabels(image),
containerd.WithAdditionalContainerLabels(labels),
containerd.WithSnapshotter(snapshotter)
}
container, err := client.NewContainer(ctx, containerID, cOpts...)
동작 방식 (How It Works)
blockfile 스냅샷터는 다른 스냅샷터들과 비슷하게 동작해요. 컨테이너 이미지의 각 레이어를 개별적으로 언팩하는데, 각 레이어 언팩은 부모(들)의 콘텐츠 위에서 이뤄져요.
blockfile 스냅샷터가 독특한 점은 두 가지예요:
- 호스트 파일시스템이 아니라 디스크 이미지 파일 안에 레이어를 적용해요.
- 레이어마다 블록 이미지 파일을 만들고, 이전 레이어를 그 위에 적용해요.
콘텐츠가 든 단일 디렉터리가 아니라, blockfile 스냅샷터의 과정이 끝나면 전체 파일시스템 이미지 내용을 담은 단일 파일이 만들어져요. 이 이미지 파일은 루프백 마운트하거나 가상 머신에 붙일 수 있죠.
스냅샷터는 레이어마다 새 blockfile을 만들어요. 이전 레이어의 blockfile을 복사해서 시작하는 거예요. 이전 레이어가 없다면, 그러니까 첫 레이어라면 scratch 파일을 복사하죠.
예를 들어 레이어 3개짜리 이미지 — A, B, C라고 부를게요 — 의 과정은 이래요:
- Layer A:
- Copy the scratch file to a new blockfile for layer A.
- Loopback-mount the blockfile for layer A.
- Apply layer A to the mount.
- Unmount the blockfile for layer A.
- Layer B:
- Copy the blockfile for layer A to a new blockfile for layer B.
- Loopback-mount the blockfile for layer B.
- Apply layer B to the mount.
- Unmount the blockfile for layer B.
- Layer C:
- Copy the blockfile for layer B to a new blockfile for layer C.
- Loopback-mount the blockfile for layer C.
- Apply layer C to the mount.
- Unmount the blockfile for layer C.
레이어의 각 언팩은 이전 레이어들의 콘텐츠를 새 blockfile 위에 쌓아 올려요. 마지막 blockfile이 전체 파일시스템 이미지를 담게 되면서 과정이 완료되죠.
이 과정의 결과로, 각 레이어는 시스템에 또 하나의 blockfile을 남겨요:
- Layer A blockfile: contents of layer A
- Layer B blockfile: contents of layer A + layer B
- Layer C blockfile: contents of layer A + layer B + layer C
기반 파일시스템과 호스트 OS가 지원한다면, 이 과정은 sparse file 지원을 가능한 한 활용해요. 덕분에 blockfile은 실제 콘텐츠에 필요한 공간만 차지해요.
예를 들어 scratch 이미지가 500MB이고 각 레이어가 25MB씩 추가된다면, 파일 크기는 이래요:
- Layer A blockfile: 25MB from layer A
- Layer B blockfile: 50MB from layer A and B
- Layer C blockfile: 75MB from layer A, B, and C
그래서 총 공간 사용량은 25+50+75=150MB예요. 각 레이어의 blockfile이 500MB 전체를 썼다면 1500MB가 필요했을 테니, 그 일부에 불과하죠.