본문 바로가기

Storage

[실습] FSx or PlugIn(goofys, s3fs) 이용하여 EC2에 S3 bucket 연결(mount) 구성하기

반응형

안녕하세요 서후아빠입니다. ^_^

이번 세션은 EC2에 EFS가 아닌 S3 버킷을 마운트 연결하는 실습을 해보겠습니다.


구성도

방법 1.  FSx로 연결하기

1단계 : FSx 생성 

Amazon FSx > File systems > Create file system > File system options (Amazon FSx for Lustre)

File system details Network & security, Encryption Data Repository Import/Export (S3 연결 시 필수)
File system name (옵션) : myfs
Deployment and storage type  : Persistent, SSD
Throughput per unit of storage(250/500/1000 MB/s/TiB) : 125
Storage capacity(1.2~2.4TiB) : 1.2
Data compression type : NONE
Virtual Private Cloud (VPC) : vpcA
VPC Security Groups : 아래 링크 참조 
Subnet : priA-sn-a
Encryption key  : aws/fsx
Import data from and export data to S3 : Enable 
File system path : /
Data repository path : s3://mybucket
Import metadata from repository(옵션) : Enable
Import settings (옵션) : New(Enable), Changed(Enable), Deleted(Enable)
Export settings (옵션) : New(Enable), Changed(Enable), Deleted(Enable)
Logging(옵션)  Backup and maintenance (옵션) Root Squash (옵션) 
Log errors : Enable
Log warnings : Enable
Daily automatic backup : Disabled
Daily automatic backup window : No preference
Weekly maintenance window : No preference
Root Squash
  - User ID : 0
  - Group ID : 0
Client addresses : -
Root Squash : 클라이언트(ex : EC2)가 서버(S3 버킷)의 파일 액세스에 대한 권한 설정 
  - 0 / 0 경우 : no_root_squash, 클라이언트가 root 권한 획득, 서버에 파일 생성시 클라이언트의 권한으로 생성
  - 0 / 0 아닌 경우 : root_squash, 클라이언트에게 root 권한 획득 막고 nobody로 매핑
  - Client addresses에 등록된 클라이언트(ex : EC2)는 Root Squash 적용이 예외되어 no_root_squash로 동작

Amazon FSx > File systems > myfs 선택 > Status(Available) 확인
Amazon FSx > File systems > myfs 선택 > Data repository(tab) > Status(Available) 확인

2023년 3월 기준 Subnet은 1개만 지정 가능하며, 생성 완료까지는 약 20분 이상 소요됩니다.
보안그룹은 생성 후, FSx에서 수정 불가능하고, Interface(EC2 > Network Interfaces > FSx용 선택)에서 수정이 가능합니다. 
  ※ 보안그룹 가이드 : https://docs.aws.amazon.com/ko_kr/AWSEC2/latest/UserGuide/storage_fsx.html 

2단계 : DNS hostname 활성화

VPC > Your VPCs > 1단계의 VPC 선택 > Actions > Edit VPC settings > Enable DNS hostnames(Enable)> Save

비활성화 시 3단계에서 마운트 작업을 할 때 "mount.lustre: Can't parse NID 'fs-0f758df8e24a622a8.fsx.ap-northeast-2.amazonaws.com@tcp:/iz6i5bmv'" 오류 발생합니다.

3단계 : S3 bucket에 마운트 및 마운트 해제

Amazon FSx > File systems > myfs 선택 > Attach에서 마운트 방법 확인

# Lustre 클라이언트 설치 (EC2 AMI는 Amazon Linux2 사용, 아래 링크 참조하여 진행)
sudo su
uname -r  
amazon-linux-extras install -y lustre

# mount할 폴더 생성
mkdir -p /mountpath

# mount
mount -t lustre -o noatime,flock fs-0f758df8e24a622a8.fsx.ap-northeast-2.amazonaws.com@tcp:/iz6i5bmv /mountpath/


# mount 결과 확인 (10.0.3.89는 FSx가 pri-sn-01에 배포되면서 생성된 Interface)
df -h | grep mountpath
  10.0.3.89@tcp:/l24y7bmv  1.2T  7.8M  1.2T   1% /mountpath

# umount 
umount /mountpath

# umount 강제
fuser -ck /mountpath

# 폴더(mountpath) 사용중인 사용자 확인
fuser -cu /mountpath

# EC2 재부팅 후에도 자동 mount 되도록 설정 (아래 방식 외 fstab에 등록하는 방식도 존재)
systemctl enable rc-local.service
chmod 755 /etc/rc.d/rc.local
vi /etc/rc.d/rc.local
  mount -t lustre -o noatime,flock fs-0f758df8e24a622a8.fsx.ap-northeast-2.amazonaws.com@tcp:/iz6i5bmv /mountpath/
Lustre 클라이언트 설치 (amazon Linux 2023 가이드는 찾지 못함)는 https://docs.aws.amazon.com/ko_kr/fsx/latest/LustreGuide/install-lustre-client.html  참조 바랍니다.
mount 후에 linux 명령어(ex : mkdir -p /mountpath/test)로 폴더를 생성하면 S3 bucket에도 동일하게 보입니다.
반대로 S3 bucket에서 test 폴더를 삭제하면 linux에서도 삭제된 상태로 확인됩니다.

방법 2.  플러그인으로 연결하기

플러그인 종류

구분 내용
goofys or s3fs - 무료 오픈 소스 
- Linux 배포판 및 MacOS 지원 
- 단순 AWS S3 버킷을 시스템 드라이브로 표시
- s3fs보다 goofys가 성능이 뛰어남
ObjectiveFS - 상용 제품 
- AWS S3 및 GCP Storage 백엔드 지원
- 전체 POSIX 호환 파일 시스템, 우수한 성능
RioFS - C언어 사용, 경량 유틸리티
- S3FS-FUSE와 비슷
- 제약사항 존재 : 파일 추가 미지원, 완전한 POSIX 호환 파일 시스템 미지원, 폴더 이름 변경 불가

1단계 : goofys or s3fs 설치

# goofys 설치 및 설정 (EC2 AMI는 Amazon Linux 2023 AMI 사용)
sudo su
yum -y install golang fuse wget
go version
  go version go1.19.6 linux/amd64
which go
  /usr/bin/go
export GOBIN=/usr/bin/go
go env | egrep 'GOROOT|GOBIN|GOPATH'
  GOBIN="/usr/bin/go"
  GOPATH="/root/go"
  GOROOT="/usr/lib/golang"
cd /usr/bin/
mkdir -p github.com/kahing  <-경로가 다르면 go install 명령어 동작하지 않음
wget http://bit.ly/goofys-latest -O github.com/kahing/goofys
chmod 755 github.com/kahing/goofys
go install github.com/kahing/goofys@v0.24.0  <-최신버전 확인하여 입력
goofys --version
  goofys version 0.24.0-943e017724ea820eb4185419ef3c41d6f921a324
mkdir -p ~/.aws
vi ~/.aws/credentials 
  [default]
  aws_access_key_id=AKIA2YHSTM5
  aws_secret_access_key=P3B9jSqBdbz 

# s3fs 설치 및 설정 (EC2 AMI는 Amazon Linux 2023 AMI 사용)
sudo su
yum -y update all
yum -y install automake fuse fuse-devel gcc-c++ git libcurl-devel libxml2-devel make openssl-devel
cd /usr/src/
git clone https://github.com/s3fs-fuse/s3fs-fuse.git
cd s3fs-fuse/
./autogen.sh
./configure
make
make install 
vi /etc/passwd-s3fs
  AKIA2YHSTM5:P3B9jSqBdbz
chmod 600 /etc/passwd-s3fs
goofys 최신버전 정보는 https://github.com/kahing/goofys/releases/latest/에서 확인 가능합니다.
  ※ goofys 설치 파일을 go get 명령어로 동작하지 않아서 wget 명령어로 받음
IAM User에서 생성한 Access Key(AKIA2YHSTM5)와 Secret Key(P3B9jSqBdbz)라고 가정
  ※ Access Key, Secret Key 탈취에 의한 보안사고를 대비하여 IAM User는 S3 마운트에 대한 최소한의 권한만 부여

2단계 : S3 bucket에 마운트 및 마운트 해제

# mount할 폴더 생성
mkdir -p /mountpath

# mount (버킷명 : mybucket or mybucket:/prefix)
goofys mybucket /mountpath   <-goofys 경우
/usr/local/bin/s3fs mybucket /mountpath   <-s3fs 경우

# mount 결과 확인
df -h | grep mountpath
  mybucket 1.0P   0   1.0P  0%   /mountpath  <-goofys 경우
  s3fs     16E    0   16E   0%   /mountpath  <-s3fs 경우

# umount 
umount /mountpath

# umount 강제
fuser -ck /mountpath

# 폴더(mountpath) 사용중인 사용자 확인
fuser -cu /mountpath

# EC2 재부팅 후에도 자동 mount 되도록 설정 (아래 방식 외 fstab에 등록하는 방식도 존재)
systemctl enable rc-local.service
chmod 755 /etc/rc.d/rc.local
vi /etc/rc.d/rc.local
  goofys mybucket /mountpath  <-goofys 경우
  /usr/local/bin/s3fs mybucket /mountpath  <-s3fs 경우
mount 후에 linux 명령어(ex : mkdir -p /mountpath/test)로 폴더를 생성하면 S3 bucket에도 동일하게 보입니다.
반대로 S3 bucket에서 test 폴더를 삭제하면 linux에서도 삭제된 상태로 확인됩니다.

기타 : 파일 사용 권한

1단계 : EC2에서 마운트한 버킷에 파일 생성

# ec2에서 test.txt 파일 생성
touch test.txt
ll -al | grep test
  -rw-r--r--.  1 root root   0 Mar 30 05:39  test.txt
chmod 600 test.txt  
  -rw-------.  1 root root   0 Mar 30 05:39  test.txt

2단계 : S3에 저장된 파일의 메타데이터 값 확인

S3 > Buctkets > mybucket 선택 > test.txt 선택 > Properties(tab) > Metadata 확인

[FSx로 마운트된 경우]
[PlugIn으로 마운트된 경우]

3단계 : EC2에서 파일의 권한 변경 (root -> ec2-user)

# ec2에서 파일 권한 변경(root -> ec2-user)
chown ec2-user.ec2-user test.txt
ll -al | grep test
  -rw-------.  1 ec2-user ec2-user   0 Mar 30 05:39 test.txt

# ec2-user의 uid, gid값 확인
cat /etc/passwd | grep ec2-user
  ec2-user:x:1000:1000:EC2 Default User:/home/ec2-user:/bin/bash

4단계 : S3에 저장된 파일의 메타데이터 값 확인

 

S3 > Buctkets > mybucket 선택 > test.txt 선택 > Properties(tab) > 변경된 Metadata 확인

[FSx로 마운트된 경우]
[PlugIn으로 마운트된 경우]

반대 상황 (S3의 Metadata를 수정하면 EC2의 파일에 적용되는지 여부)에 대한 테스트 결과
  - FSx 경우 : 즉시 적용됨
  - PlugIn 경우 : 즉시 적용되지 않음 (마운트를 해제하고 재마운트할 때 적용)
반응형