containerd 주요 기능
containerd 주요 기능 (Features)
containerd의 핵심 기능들을 한눈에 살펴볼 수 있는 문서예요. 클라이언트 패키지부터 네임스페이스, 이미지 배포, 컨테이너, 루트 파일시스템, 태스크, 체크포인트까지 Go 코드 예시와 함께 소개해요. containerd를 자기 플랫폼에 붙이려는 개발자에게 기능 지도 역할을 해주죠.
출처: 문서
본문
아래 절들은 containerd의 핵심 기능을 다뤄요.
Client
containerd는 containerd를 여러분의 플랫폼에 통합하는 데 도움이 되는 완전한 클라이언트 패키지를 제공해요.
import (
"context"
containerd "github.com/containerd/containerd/v2/client"
"github.com/containerd/containerd/v2/pkg/cio"
"github.com/containerd/containerd/v2/pkg/namespaces"
)
func main() {
client, err := containerd.New("/run/containerd/containerd.sock")
defer client.Close()
}
Namespaces
네임스페이스는 여러 소비자가 서로 충돌 없이 같은 containerd를 함께 쓰게 해줘요. 컨테이너와 이미지는 분리를 유지하면서 컨텐츠만 공유할 수 있다는 장점이 있죠.
API 요청에 네임스페이스를 지정하려면:
context = context.Background()
// create a context for docker
docker = namespaces.WithNamespace(context, "docker")
containerd, err := client.NewContainer(docker, "id")
클라이언트에 기본 네임스페이스를 지정하려면:
client, err := containerd.New(address, containerd.WithDefaultNamespace("docker"))
Distribution
// pull an image
image, err := client.Pull(context, "docker.io/library/redis:latest")
// push an image
err := client.Push(context, "docker.io/library/redis:latest", image.Target())
Containers
containerd에서 컨테이너는 메타데이터 객체예요. OCI 런타임 명세, 이미지, 루트 파일시스템 같은 리소스와 다른 메타데이터를 컨테이너에 붙일 수 있죠.
redis, err := client.NewContainer(context, "redis-master")
defer redis.Delete(context)
OCI Runtime Specification
containerd는 컨테이너 실행을 위한 OCI 런타임 명세를 완전히 지원해요. 이미지와 커스텀 파라미터를 바탕으로 런타임 명세를 생성해 주는 내장 함수들도 갖추고 있어요.
컨테이너를 만들 때 명세를 어떻게 수정할지 옵션으로 지정할 수 있어요.
redis, err := client.NewContainer(context, "redis-master", containerd.WithNewSpec(oci.WithImageConfig(image)))
Root Filesystems
containerd는 컨테이너에 overlay나 snapshot 파일시스템을 쓸 수 있게 해줘요. overlayfs와 btrfs는 내장 지원이에요.
// pull an image and unpack it into the configured snapshotter
image, err := client.Pull(context, "docker.io/library/redis:latest", containerd.WithPullUnpack)
// allocate a new RW root filesystem for a container based on the image
redis, err := client.NewContainer(context, "redis-master",
containerd.WithNewSnapshot("redis-rootfs", image),
containerd.WithNewSpec(oci.WithImageConfig(image)),
)
// use a readonly filesystem with multiple containers
for i := 0; i < 10; i++ {
id := fmt.Sprintf("id-%s", i)
container, err := client.NewContainer(ctx, id,
containerd.WithNewSnapshotView(id, image),
containerd.WithNewSpec(oci.WithImageConfig(image)),
)
}
Tasks
컨테이너 객체를 시스템 위에서 실행 가능한 프로세스로 바꾸는 작업은 컨테이너에서 새 Task를 만드는 것으로 이뤄져요. 태스크는 containerd 안에서 실행 가능한 객체를 나타내요.
// create a new task
task, err := redis.NewTask(context, cio.NewCreator(cio.WithStdio))
defer task.Delete(context)
// the task is now running and has a pid that can be used to setup networking
// or other runtime settings outside of containerd
pid := task.Pid()
// start the redis-server process inside the container
err := task.Start(context)
// wait for the task to exit and get the exit status
status, err := task.Wait(context)
Checkpoint and Restore
머신에 criu가 설치돼 있다면 컨테이너와 그 태스크를 체크포인트하고 복원할 수 있어요. 이를 통해 컨테이너를 복제하거나 다른 머신으로 라이브 마이그레이션할 수 있죠.
// checkpoint the task then push it to a registry
checkpoint, err := task.Checkpoint(context)
err := client.Push(context, "myregistry/checkpoints/redis:master", checkpoint)
// on a new machine pull the checkpoint and restore the redis container
checkpoint, err := client.Pull(context, "myregistry/checkpoints/redis:master")
redis, err = client.NewContainer(context, "redis-master", containerd.WithNewSnapshot("redis-rootfs", checkpoint))
defer container.Delete(context)
task, err = redis.NewTask(context, cio.NewCreator(cio.WithStdio), containerd.WithTaskCheckpoint(checkpoint))
defer task.Delete(context)
err := task.Start(context)
Snapshot Plugins
containerd의 내장 스냅숏 플러그인 외에도, 외부 플러그인을 GRPC로 설정해 추가할 수 있어요. 외부 플러그인은 설정된 이름으로 노출되며 내장 플러그인과 나란히 표시돼요.
외부 스냅숏 플러그인을 추가하려면 containerd 설정 파일(기본 경로 /etc/containerd/config.toml)에 플러그인을 등록해요. proxy_plugin. 뒤의 문자열이 스냅숏터 이름으로 쓰이고, 주소는 containerd의 Snapshot GRPC API를 서빙하는 GRPC 리스너가 있는 소켓을 가리켜야 해요. 설정 변경이 적용되려면 containerd를 재시작해야 한다는 점도 잊지 마세요.
[proxy_plugins]
[proxy_plugins.customsnapshot]
type = "snapshot"
address = "/var/run/mysnapshotter.sock"
플러그인을 만드는 방법은 PLUGINS.md를 참고하세요.
더 알아보기 (Learn more)
- containerd 플러그인 문서 — 외부 플러그인 작성 방법
- containerd 시작하기 — 설치와 기본 사용법
- CRIU 프로젝트 — 체크포인트/복원을 떠받치는 도구