Cloud/AWS

aws cli, ansible 사용하여 자동화 - nfs 서버 만들기

Jen'_' 2021. 5. 6. 23:04
반응형

 

NFS 서버란?

NFS란 Network File system의 약자로 네트워크 상에서 파일 시스템을 공유하도록 설계된 파일 시스템의 한종류 이다. 다른 호스트에 있는 파일 시스템의 일부를 자신의 디렉토리인 것처럼 사용해주는 것이 NFS이다.

NFS의 특징은 위 그림과 같이 한개의 서버에 NFS로 여러대의 웹서버로  연결하여 사용하여 여러대의 웹서버를 이중화 하는 과정으로 많이 쓰인다. 이러한 장점으로 여러호스트에서 저장된 정보를 공유할 수 있다.

 

하지만 NFS는 다른 서버에 있는 파티션을 마치 내 로컬 영역인것처럼 네트워크를 이용하는 것이기 때문에 보안에 취약하다.

 


 

실습내용

- aws-cli를 이용해서 인스턴스 2개를 만드는 스택을 생성한다

- 인스턴스를 server와 client로 가정하고 nfs서버를 구성한다

- nfs구성은 ansible-playbook 을 이용해서 자동화 할 것이다

 

※ 실습한거 복습 하는거라서 파일 설명이 미흡할 수 있음

 

ec2.ini
0.00MB
ec2.py
0.07MB

 

ec2.init  ec2.py  ansible.cfg 세개가 있는 디렉터리에서 ansible 명령어를 실행시켜야 한다

jen@DESKTOP-MIF4OE4:~/sample/Chapter03/ansible$ sudo pip3 install ansible

jen@DESKTOP-MIF4OE4:~/sample/Chapter03/ansible$ ls

ec2.init  ec2.py  ansible.cfg  nfs.yml  cloudformation.yml

jen@DESKTOP-MIF4OE4:~/sample/Chapter03/ansible$ chmod +x ec2.init ec2.py 

jen@DESKTOP-MIF4OE4:~/sample/Chapter03/ansible$ python ec2.py

{
  "_meta": {
    "hostvars": {}
  }
}

 

ansible.cfg

[defaults]
inventory      = ./ec2.py
remote_user    = ec2-user
become         = True
become_method  = sudo
become_user    = root
nocows         = 1

 노드들이 ./ec2.py 파일에 있도록 설정

/hosts 에 저장되어있다고 생각, ec2.py 파일은 inventory 로 인식 

 

AWS 접근방법
1. 콘솔
2. CLI
3. SDK

./ec2.py 으로 AWS에 접근 할것이기 때문에 AWS SDK인 boto3 필요
python -> SDK(boto3) -> aws

 

jen@DESKTOP-MIF4OE4:~/sample/Chapter03/ansible$ pip3 install boto3

jen@DESKTOP-MIF4OE4:~/sample/Chapter03/ansible$ sudo apt install dos2unix

jen@DESKTOP-MIF4OE4:~/sample/Chapter03/ansible$ dos2nuix ec2.py

 

cloudformation.yml

Parameters:
  KeyName:
    Description: Name of an existing EC2 KeyPair to enable SSH access to the instances. Linked to AWS Parameter
    Type: AWS::EC2::KeyPair::KeyName
    ConstraintDescription: must be the name of an existing EC2 KeyPair.

Resources:
  Server:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-03b42693dc6a7dc35
      InstanceType: t2.micro
      KeyName: !Ref KeyName
      Tags:
        - Key: Name
          Value: Server
      SecurityGroups:
        - !Ref MySG
      
  Client:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-03b42693dc6a7dc35
      InstanceType: t2.micro
      KeyName: !Ref KeyName
      Tags:
        - Key: Name
          Value: Client
      SecurityGroups:
        - !Ref MySG
      
            
  MySG:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Enable HTTP access via port 80 and SSH access via port 22
      SecurityGroupIngress:
      # rpcbind가 사용
      - IpProtocol: tcp
        FromPort: 111 
        ToPort: 111
        CidrIp: 0.0.0.0/0
      - IpProtocol: udp
        FromPort: 111
        ToPort: 111
        CidrIp: 0.0.0.0/0  
      # nfs 서비스가 사용  
      - IpProtocol: tcp
        FromPort: 2049
        ToPort: 2049
        CidrIp: 0.0.0.0/0
      # mountd 데몬이 사용  
      - IpProtocol: tcp
        FromPort: 20048
        ToPort: 20048
        CidrIp: 0.0.0.0/0  
      - IpProtocol: tcp
        FromPort: 22
        ToPort: 22
        CidrIp: 0.0.0.0/0


스택생성명령어

aws cloudformation create-stack --capabilities CAPABILITY_IAM --stack-name <스택이름> --template-body file://<스택파일경로> --parameters ParameterKey=<스택파일 안의 ParameterKey>,ParameterValue=<키페어>

 

jen@DESKTOP-MIF4OE4:~/sample/Chapter03/ansible$ aws cloudformation create-stack --capabilities CAPABILITY_IAM --stack-name nfs --template-body file://cloudformation.yml --parameters ParameterKey=KeyName,ParameterValue=jenkey

 

nfs.yml

server 에서  /etc/exports 설정하고 client가 마운트를 거는 ansible-playbook

---
- name: Setup for nfs server
  hosts: 13.209.4.232 # server ip
  gather_facts: no
  
  tasks:
    - name: make nfs_shared directory
      file:
        path: /home/ec2-user/nfs_shared
        state: directory
        mode: 0777                          

    - name: configure /etc/exports 
      become: yes
      lineinfile:
        path: /etc/exports
        line: /home/ec2-user/nfs_shared 54.180.97.151/24(rw,sync)

    - name: nfs service restart
      become: yes
      service:
        name: nfs
        state: restarted

- name: Setup for nfs clients
  hosts: 54.180.97.151 # client ip
  gather_facts: no

  tasks:
    - name: make nfs_client directory
      file:
        path: /home/ec2-user/nfs
        state: directory

    - name: mount point directory as client
      become: yes
      mount:
        path: /home/ec2-user/nfs
        src: 13.209.4.232:/home/ec2-user/nfs_shared
        fstype: nfs
        opts: nfsvers=3
        state: mounted

 

/etc/exports
파일에 마운트를 허가할 디렉토리와 마운트를 허가할 호스트 목록을 설정하는 파일

rw : 읽기, 쓰기 가능
ro : 읽기만 가능

secure : 클라이언트 마운트 요청시 포트를 1024 이하로 합니다.
noaccess : 액세스 거부
root_squach : 클라이언트의 root가 서버의 root권한을 획득하는 것을 막습니다.
no_root_squash : 클라이언트의 root와 서버의 root를 동일하게 합니다.
sync : 파일 시스템이 변경되면 즉시 동기화합니다.
all_squach : root를 제외하고 서버와 클라이언트의 사용자를 동일한 권한으로 설정합니다.
no_all_squach : root를 제외하고 서버와 클라이언트의 사용자들을 하나의 권한을 가지도록 설정합니다. 

ansible-playbook 배포 명령어

ansible-playbook <플레이북이름> --private-key ~/.ssh/<키페어이름> --e target=<목적지그룹명>

 

jen@DESKTOP-MIF4OE4:~/sample/Chapter03/ansible$ ansible-playbook nfs.yml --private-key ~/.ssh/jenkey.pem --e target=ec2

 

server

[ec2-user@ip-172-33-39-198 ~]$ ls
nfs_shared

 

client

[ec2-user@ip-172-31-32-145 ~]$ ls
nfs

 

 

디렉터리가 생성되고 마운트가 걸렸다 !

 

 

 

참고

server-talk.tistory.com/117

 

 

반응형