← 가이드 목록으로 ← Back to guides

Linux 컨테이너와 시스템 프로그래밍 완벽 가이드 Linux Container & System Programming Complete Guide

Docker와 Kubernetes의 기반 기술인 Linux 네임스페이스cgroups를 이해하세요. systemd로 서비스를 관리하고 컨테이너 기술의 핵심을 파악합니다.

Understand Linux namespaces and cgroups, the foundation of Docker and Kubernetes. Manage services with systemd and grasp the core of container technology.

Linux 네임스페이스 Linux Namespaces

네임스페이스는 프로세스를 격리하여 독립된 환경처럼 보이게 합니다. Docker 컨테이너가 호스트와 분리되는 핵심 원리입니다.

Namespaces isolate processes to appear as independent environments. This is the core principle of how Docker containers are separated from the host.

# 현재 프로세스의 네임스페이스 확인
$ ls -la /proc/$$/ns/

# 새로운 네임스페이스에서 쉘 실행 (PID, Network 격리)
$ sudo unshare --pid --net --fork /bin/bash

# 다른 네임스페이스로 진입
$ sudo nsenter -t <PID> -n -p
# Check current process namespaces
$ ls -la /proc/$$/ns/

# Run shell in new namespace (PID, Network isolation)
$ sudo unshare --pid --net --fork /bin/bash

# Enter another namespace
$ sudo nsenter -t <PID> -n -p

📦 주요 네임스페이스 종류 📦 Main Namespace Types

  • PID: 프로세스 ID 격리 (컨테이너 내 PID 1)
  • Network: 네트워크 스택 격리
  • Mount: 파일시스템 마운트 격리
  • User: UID/GID 매핑 (rootless 컨테이너)
  • PID: Process ID isolation (PID 1 in container)
  • Network: Network stack isolation
  • Mount: Filesystem mount isolation
  • User: UID/GID mapping (rootless containers)

cgroups (Control Groups) cgroups (Control Groups)

cgroups는 프로세스 그룹의 리소스(CPU, 메모리, I/O)를 제한하고 모니터링합니다. 컨테이너 리소스 관리의 핵심입니다.

cgroups limit and monitor resources (CPU, memory, I/O) for process groups. This is the core of container resource management.

# cgroups v2 계층 구조 확인
$ cat /sys/fs/cgroup/cgroup.controllers

# 메모리 제한 설정 (100MB)
$ echo "104857600" > /sys/fs/cgroup/mygroup/memory.max

# CPU 가중치 설정
$ echo "50" > /sys/fs/cgroup/mygroup/cpu.weight

# 현재 리소스 사용량 확인
$ cat /sys/fs/cgroup/mygroup/memory.current
# Check cgroups v2 hierarchy
$ cat /sys/fs/cgroup/cgroup.controllers

# Set memory limit (100MB)
$ echo "104857600" > /sys/fs/cgroup/mygroup/memory.max

# Set CPU weight
$ echo "50" > /sys/fs/cgroup/mygroup/cpu.weight

# Check current resource usage
$ cat /sys/fs/cgroup/mygroup/memory.current

systemd 서비스 관리 systemd Service Management

# 커스텀 서비스 파일 생성
# /etc/systemd/system/myapp.service

[Unit]
Description=My Application
After=network.target

[Service]
Type=simple
User=appuser
ExecStart=/usr/bin/myapp
Restart=on-failure
RestartSec=5
MemoryMax=512M
CPUQuota=50%

[Install]
WantedBy=multi-user.target
# systemd 서비스 관리 명령어
$ sudo systemctl daemon-reload
$ sudo systemctl enable myapp
$ sudo systemctl start myapp
$ sudo systemctl status myapp

# 로그 확인
$ journalctl -u myapp -f
# systemd service management commands
$ sudo systemctl daemon-reload
$ sudo systemctl enable myapp
$ sudo systemctl start myapp
$ sudo systemctl status myapp

# Check logs
$ journalctl -u myapp -f

💡 리소스 제한과 모니터링 💡 Resource Limiting & Monitoring

  • systemd-cgtop: 실시간 cgroup 리소스 모니터링
  • systemctl show: 서비스 cgroup 설정 확인
  • 단위 파일에서 직접 리소스 제한 가능
  • systemd-cgtop: Real-time cgroup resource monitoring
  • systemctl show: Check service cgroup settings
  • Resource limits can be set directly in unit files

⚠️ 주의 사항 ⚠️ Cautions

  • cgroups v1과 v2는 호환되지 않음
  • 네임스페이스 작업에는 root 권한 필요
  • 잘못된 설정은 시스템 불안정을 초래
  • cgroups v1 and v2 are not compatible
  • Namespace operations require root privileges
  • Incorrect settings can cause system instability