tonykang22 / study

0 stars 0 forks source link

[컨테이너를 구성하는 리눅스 기술] union mount filesystem의 이해 #102

Open tonykang22 opened 2 years ago

tonykang22 commented 2 years ago

union mount filesystem의 이해

도입

ubuntu@ip-172-31-47-152:~$ sudo mount -t tmpfs tmpfs /home/ubuntu/linux_campus/ ubuntu@ip-172-31-47-152:~$ mount | grep tmpfs devtmpfs on /dev type devtmpfs (rw,relatime,size=487876k,nr_inodes=121969,mode=755,inode64) tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,inode64) tmpfs on /run type tmpfs (rw,nosuid,nodev,size=98944k,mode=755,inode64) tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k,inode64) tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755,inode64) tmpfs on /run/snapd/ns type tmpfs (rw,nosuid,nodev,size=98944k,mode=755,inode64) tmpfs on /run/user/1000 type tmpfs (rw,nosuid,nodev,relatime,size=98940k,mode=700,uid=1000,gid=1000,inode64) tmpfs on /home/ubuntu/linux_campus type tmpfs (rw,relatime,inode64)

ubuntu@ip-172-31-47-152:~$ ls -l linux_campus/ total 0


<br>

## Union mount
- 하나의 디렉토리 위치에 여러 개의 디렉토리를 마운트하면 하나의 통합된 디렉토리처럼 보이게 하는 방법
- 주요 개념
    * Image Layer : 마지막 레이어를 제외하고 Read Only
    * Copy-on-write(CoW) : 변경된 파일만 저장
        * (첨부 이미지) Layer 2에도 file1이 존재하지만, 변경되지 않았기 때문에 한번의 write만 하면 된다.
- 지원 파일시스템
    * AUFS, OverlayFS(기본, 가장 많이 사용), BTRFS 등
<img width="869" alt="image" src="https://user-images.githubusercontent.com/86760744/188368228-cff81d5c-4a39-45fa-896c-1eedc5c3e0af.png">

<br>

### Union mount filesystem의 이해
- Docker에 적용? -> 스토리지 드라이버라는 이름으로 적용되어 있다.
    * overlay, btrfs, aufs, devicemapper 등
- Overlay FS
    * 하나의 파일시스템을 다른 파일 시스템 상단에 overlay
    * 하단 파일시스템에 상관 없이 구성 가능
        * 단, RedHat Linux는 XFS만 하단 파일시스템으로 지원

<img width="820" alt="image" src="https://user-images.githubusercontent.com/86760744/188368514-eae9bba9-a6bb-43a9-b8a1-e5c25b87a967.png">

- 컨테이너에 마운트된 파일시스템에 파일이 추가된다면 어디에 저장이 될까?
    * 실제 저장되는 위치는 `upperdir`(read/write 권한을 가지고 있다.)

<br><br>

- BTRFS
    * snapshots, RAID, self-healing을 주요 기능으로 지원하는 copy on write 방식의 파일시스템
        * 변경 내용만 반영이 된다.
    * Incremental backup 방식 사용하여 snapshot 생성

<img width="321" alt="image" src="https://user-images.githubusercontent.com/86760744/188368469-70d5036e-2e9a-41df-8368-727083cad824.png">

<br>

<img width="599" alt="image" src="https://user-images.githubusercontent.com/86760744/188368493-5cdd5d20-5a86-48fa-8f15-afc48f5788f8.png">

- Copy on write 방식으로 변경이 필요한 내용만 스냅샷에 추가로 적용이 된다.

<br><br>

## Overlay 파일시스템 실습 1
### OverlayFS로 union mount 진행
- 오버레이 파일시스템 실습에 사용할 디렉토리 생성
    * `mkdir /tmp/{lower1,lower2,upper,merged,work}`
        * lower1, lower2 : read-only
        * upper : writabe한 layer
        * merged : lower, upper layer를 겹친 실제 사용할 레이어
        * work : 파일시스템에서 관리 목적으로 사용하는 디렉토리
- 실습에 사용할 파일 생성
    * `echo "lower1 a" > /tmp/lower1/a.txt`
    * `echo "lower1 b" > /tmp/lower1/b.txt`
    * `echo "lower2 a" > /tmp/lower2/a.txt` 
    * `echo "lower2 c" > /tmp/lower2/c.txt`
        * a.txt 는 동일 이름으로 merged에서 어떤 형태를 갖는지 확인해볼 수 있다.
- 마운트 정보
``` shell
$ mount
...
overlay on /tmp/merged type overlay (rw,relatime, 
lowerdir=/tmp/lower1:/tmp/lower2, 
upperdir=/tmp/upper,
workdir=/tmp/work)
# 사용할 디렉토리 생성
ubuntu@ip-172-31-47-152:~$ mkdir /tmp/{lower1,lower2,upper,merged,work}

# 각 디렉토리에 파일 생성
ubuntu@ip-172-31-47-152:~$ echo "lower1 a" > /tmp/lower1/a.txt
ubuntu@ip-172-31-47-152:~$ echo "lower1 b" > /tmp/lower1/b.txt
ubuntu@ip-172-31-47-152:~$ echo "lower2 a" > /tmp/lower2/a.txt
ubuntu@ip-172-31-47-152:~$ echo "lower2 c" > /tmp/lower2/c.txt

# 생성한 파일 확인
ubuntu@ip-172-31-47-152:~$ ls -l /tmp/lower1/a.txt
-rw-rw-r-- 1 ubuntu ubuntu 9 Sep  5 06:38 /tmp/lower1/a.txt

# mount 명령어를 통해 overlay type을 사용
ubuntu@ip-172-31-47-152:~$ sudo mount -t overlay overlay \
> -o lowerdir=/tmp/lower1:/tmp/lower2 \
> /tmp/merged

# ro(read-only)인 것을 확인할 수 있다. (lowerdir만 설정해주었기 때문이다.)
ubuntu@ip-172-31-47-152:~$ mount | grep overlay
overlay on /tmp/merged type overlay (ro,relatime,lowerdir=/tmp/lower1:/tmp/lower2)

# 그렇기 때문에 write 할 수 없다.
ubuntu@ip-172-31-47-152:~$ echo "hello ro" > /tmp/merged/d.txt
-bash: /tmp/merged/d.txt: Read-only file system

# 다시 unmount를 해준 뒤, 확인
ubuntu@ip-172-31-47-152:~$ sudo umount /tmp/merged
ubuntu@ip-172-31-47-152:~$ ls -l /tmp/merged/
total 0
ubuntu@ip-172-31-47-152:~$ mount | grep overlay

# 이번엔 upperdir, workdir 까지 설정해준다.
ubuntu@ip-172-31-47-152:~$ sudo mount -t overlay overlay \
> -o lowerdir=/tmp/lower1:/tmp/lower2,upperdir=/tmp/upper,workdir=/tmp/work \
> /tmp/merged

# 아까와 달리 rw(read-write)인 것을 확인할 수 있다.
ubuntu@ip-172-31-47-152:~$ mount | grep overlay
overlay on /tmp/merged type overlay (rw,relatime,lowerdir=/tmp/lower1:/tmp/lower2,upperdir=/tmp/upper,workdir=/tmp/work)

# 파일 저장 시에도 merged 디렉토리에 저장되는 것을 확인할 수 있다.
ubuntu@ip-172-31-47-152:~$ echo "Hello World" > /tmp/merged/hello.txt
ubuntu@ip-172-31-47-152:~$ ls -l /tmp/merged/
total 16
-rw-rw-r-- 1 ubuntu ubuntu  9 Sep  5 06:38 a.txt
-rw-rw-r-- 1 ubuntu ubuntu  9 Sep  5 06:38 b.txt
-rw-rw-r-- 1 ubuntu ubuntu  9 Sep  5 06:38 c.txt
-rw-rw-r-- 1 ubuntu ubuntu 12 Sep  5 06:43 hello.txt

# upper가 read-write 권한이 있는 layer
ubuntu@ip-172-31-47-152:~$ cd /tmp/upper
ubuntu@ip-172-31-47-152:/tmp/upper$ ls -l
total 4
-rw-rw-r-- 1 ubuntu ubuntu 12 Sep  5 06:43 hello.txt

# 다시 unmount하고 merged 확인
ubuntu@ip-172-31-47-152:~$ sudo umount /tmp/merged
ubuntu@ip-172-31-47-152:~$ ls -l /tmp/merged
total 0


Overlay 파일시스템 실습 2

Docker Image 관리에 사용되는 OverlayFS 정보 확인

$ docker pull nginx
$ docker run --rm -d nginx



Review

컨테이너를 구성하는 3가지 주요 리눅스 기술