呂文楷
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Midterm Project-Chord, Part 2: Auto-Scaling on AWS Cloud > Please be mindful of avoiding extra charges when using your AWS account: > [How to Avoid](https://aws.amazon.com/premiumsupport/knowledge-center/free-tier-charges/) The design of the Chord protocol allows for easy scability. In Part 2 of the midterm project, you will be leveraging AWS infrastructure and Chord node implementation from Part 1 to build a distributed file system that can automatically scale out on the AWS cloud. In this system, a cloud node (computing unit) will be running a Chord node and a file server. Multiple Chord nodes must be deployed on individual cloud machines respectively. <!-- The goal of this part is to scale out while the average disk usage exceed a threshold and make sure the new nodes can join the chord ring automatically. --> There are several ways to deploy the Chord-based distributed file system with auto-scaling capabilities on the cloud, and an example architecture is provided. You may adopt a different architecture design as long as it meets the requirements, but it is highly recommended to discuss your design with TAs beforehand. This document provides an operational guide for creating a **basic** auto-scaling file system, which serves as a foundation for developing an automatically scalable Chord-based distributed file system. The process of building the architecture can be divided into two phases. In the first phase, you will run a file server on a single AWS instance without enabling auto-scaling. In the second phase, your distributed file system should automatically scale new EC2 instances running new file servers, based on the disk usage of each instance. Once completed, you should be able to integrate Chord nodes into the system yourself. The final architecture should resemble the diagram below: ![](https://i.imgur.com/W5NdWqr.png) ## Prerequisite 1. Newly created (in less than 12 months) AWS account **or** 2. AWS academy account (with $100 free credit for each user) Also, download and extract the assignment data: ```shell $ wget ntu.im/IM5057/chord-part-2.tar.gz $ tar zxvf chord-part-2.tar.gz ``` The `chord-part-2` folder contains the following directories: ``` chord-part-2/ | +-- chord | +-- download.py | +-- upload.py | +-- test_download.py | +-- test_upload.py ``` The `chord` file is a compiled binary that meets the requirements in Part 1 of the midterm project. You may use this for your Chord nodes if you did not finish your implementation. `upload.py`, `download.py`, `test_upload.py` and `test_download.py` are Python scripts for uploading and downloading files to/from the file server `uploadserver`, which will be introduced later. <!-- `uploadserver` we used in the previous guide. --> <!-- 2. Your Chord code from part 1, or the binary file provided by TA. --> ## Phase 1: Running the First Node on AWS Cloud In phase 1, you will be creating some services using the AWS console and run a simple file server on a single cloud node. > To prevent additional costs, follow these two optional steps: > #### Create Budget Alart > - AWS Billing > Budgets > Create budget > #### Enable Free Tier Usage Alart > - AWS Billing > Billing preferences > Receive Free Tier Usage Alerts ### Create IAM Role - Go to IAM > Roles > Create role - Create a new policy and allow IAM:PassRole in the policy visual editor ![](https://i.imgur.com/Uh9XmeQ.png) - Create a new role for your service - Attach predefined Policy EC2:AmazonEC2FullAccess and the policy you created ### Create Security Group - Go to EC2 > Network & Security > Security Groups > Create security group ### Modify Security Group Inbound Rules - Go to EC2 > Network & Security > Security Groups > Select target security group > Inbound Rules > Edit inbound rules You should: 1. Add all TCP inbound rules from yourself (security group), **not from 0.0.0.0**, to allow communications between nodes in same security group. All instances should be configured in the same security group. 2. Add ssh and TCP from your IP or 140.112.0.0/16 to allow your local computer to access the EC2 instances (using VPN). **You are not recommended to allow connections from all IPs to your instances.** ### Create Free Tier EC2 Instances EC2 > Instances > Instances > Launch instances Important instance specifications: - OS image: Amazon Linux 2 - Instance type: t2.micro (under free tier) - Keypair: create or attach one - IAM role: attach the role you just create - Security group: attach the security group you just create ### Connect EC2 instance through SSH - Go to EC2 > Instances > Select target intance > Connect > SSH Client > Wait until the EC2 is fully initialized and ready. ### Install Amazon CloudWatch Agent ```shell $ sudo yum install amazon-cloudwatch-agent -y ``` ### Install and Run File Server We will be using Python `uploadserver` as a simple file server. To install the file server: ```shell $ sudo yum install -y python3-pip python3 python3-setuptools -y $ pip3 install uploadserver ``` To start the file server listening on port 5058: ```shell $ python3 -m uploadserver 5058 ``` ### Install some Additional Requirements ```shell $ pip3 install boto3 ec2_metadata ``` ### (Optional) Launch CloudWatch Wizard Launch CloudWatch Wizard to generate metric config file: ```shell $ sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard ``` ### Enable CloudWatch Agent Create the metric config file and start the CloudWatch agent. You may need to modify the config file even if you created with the Wizard. Please refer to [**Cloudwatch Agent**](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/create-cloudwatch-agent-configuration-file-wizard.html) and [**Cloudwatch Agent Config Detail**](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html) for more information. In your config file: - set `agent.metrics_collection_interval` to 60 seconds - add `AutoScalingGroupName` to `metrics.append_dimensions` - add `used_percent` at `metrics.disk.measurement` to collect `disk_used_percent` for each node After finishing your config file, start the CloudWatch agent: ```shell $ sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:<your config file> ``` ### (Optional) CloudWatch Verification It may take some time for the metrics to be displayed on the CloudWatch dashboard. To verify your CloudWatch connection, you may use telnet: ```shell $ sudo yum install telnet -y $ telnet monitoring.ap-northeast-1.amazonaws.com 443 ``` ### Check CloudWatch Metrics - Go to CloudWatch > Metrics > Browse > CWAgent > device, fstype, path, then select desired metrics ![](https://i.imgur.com/0LpLOzH.png) > Remember to change the metric period to one minute in the tab "Graphed metrics". ### Test your File Server from Local or Another EC2 instance After successfully running `uploadserver` on your EC2 instances, you can use the `test_upload.py` and `test_download.py` scripts for testing the access to your `uploadserver`. For example, you may use ```shell $ ./test_upload.py a.txt 10.10.10.10 ``` to upload the `a.txt` file onto the file server running on 10.10.10.10. You can also check if the `a.txt` file appears on your 10.10.10.10 instance. Similarly, you may use ```shell $ ./test_download.py a.txt 10.10.10.10 ``` to download the `a.txt` file from the file server running on 10.10.10.10. ## Phase 2: Enabling Auto-scaling In this phase, we will use the instance created in part 1 to build the auto-scaling file system. ### Create AMI First, we need to create a new Amazon Machine Image (AMI) based on the instance we created in part 1. - Go to EC2 > Instances > Instances > Select target intance > Actions > Image and templates > Create image ### Launch an Auto-scaling Group - EC2 > Instances > Launch Templates > Create launch template Create Launch Configuration 1. Select the AMI you created in the previous step. 2. Modfiy the template details, and select the same instance specification as in phase 1. 3. Select the Security Group, IAM, keypair, etc. 4. Add User data and make sure it runs successfully when new instance is created. An example of User data: ```shell #!/bin/sh /bin/echo "Hello World" >> /tmp/testfile.txt sudo yum install -y python3-pip python3 python3-setuptools -y /usr/bin/python3 -m pip install boto3 ec2_metadata uploadserver /usr/bin/python3 -m uploadserver 5058 --directory /home/ec2-user/files --//-- ``` The command ```shell $ /usr/bin/python3 -m uploadserver 5058 --directory /home/ec2-user/files ``` runs the `uploadserver` listening on port 5058 and uses `/home/ec2-user/files` as the file directory. > Please keep an eye on the source version when modifying the launch template. > Note that cloud-init will run the user data only at the time when the instance is created, and the log will be stored in `/var/log/cloud-init-output.log`. ### Create Auto-scaling Group Go to EC2 > Auto Scaling Group > Create - name, desired launch template - VPC, AZ - no load balancer, reduce Health check grace period, optional settings - group size: - min=1, max=3, desire=2 for frugality - min=2, max=5, desire=3 for better experience if you have free credits :) - Scaling policies: you can attach it later > If you are going to create/delete the auto scaling group multiple times, it is recommended to write the `create_auto_scaling_group.py` script and run it. ### Create CloudWatch Alarm and Attach it to Auto-scaling Group - Go to EC2 > Auto Scaling Group - Go to Cloudwatch > Alarms > Create alarm > If you are going to create and attach alarms multiple times, it is recommended to write the `create_scaling_policy.py` script and run it. Your file system is now set up to achieve auto-scaling on AWS. ## Requirements After deploying the file servers and configuring auto-scaling metrics, you are required to integrate Chord nodes into the system. When a new cloud node is initialized, in addition to launching a `uploadserver` file server, the cloud node should also start a Chord node, and make the Chord node join the existing Chord ring system. The Chord nodes should listen on port 5057, and the `uploadserver` file server should listen on port 5058. > To join the existing Chord ring system, you can use [AutoScaling.Client.describe_auto_scaling_groups](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/autoscaling/client/describe_auto_scaling_groups.html) to find IDs of all running instances in the Auto-scaling Group, and then use [EC2.Client.describe_instances](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2/client/describe_instances.html) to obtain their public IP addresses. You can use the `upload.py` and `download.py` scripts for testing the correctness of your Chord-based distributed file system. Different from `test_upload.py` and `test_download.py`, `upload.py` and `download.py` compute the hash of the file name, and find the specific cloud node for storing the file by sending a `"find_successor"` RPC request, before uploading and downloading a file. To upload an `a.txt` file onto the file server, we can run the `upload.py` script by providing the file name and the URL(IP address) of an arbitrary cloud node as arguments: ```shell $ ./upload.py a.txt 10.10.10.10 Hash of a.txt is 607204990 Uploading file to http://20.20.20.20 ``` In this example, the IP 10.10.10.10 is the IP address of a random cloud node in the deployed system. However, the IP is not necessary the IP of the file server for uploading the file. The script first calculates the 32-bit hash of the file name (in this case, 607204990). After this, the script calls the `"find_successor"` RPC of the Chord node 10.10.10.10:5057, and gets the Node information of the cloud node for uploading the file (in this case, 20.20.20.20). Finally, the script uploads the file to the file server 20.20.20.20:5058. Similarly, the `download.py` script calculates the hash of the file name, get the Node information by calling the `"find_successor"` RPC, and finally download the file from the file server on the cloud node. ### 1. Auto-Scaling (50%) Your system should be capable of automatically adding new cloud nodes when the storage usage hits the threshold. **Please note that if your system failed to meet the Auto-Scaling requirement, you will not receive credit for the next part.** ### 2. Integrating Chord Nodes (50%) A Chord node should be started together with the file server on each of the cloud nodes. The Chord node should be able to join each other and together form a Chord ring. Also, you should be able to upload and download files to/from your distributed file system using the provided `upload.py` and `download.py` scripts on your local machine. You are required to demonstrate specific functionalities to the TA for grading. Parts 2 and 3 of the midterm project will be evaluated together during the demonstration. ## Appendix: Friendly Reminders Remember to clean up your AWS account and stop unused resources. Here are some tips: - stop/terminate EC2 - deregister AMI - delete Auto scaling group - delete Launch Template - delete Additional CloudWatch metrices - delete all the unused resourses - for more information, please check out: [find unactive services to avoid unexpected charges](https://aws.amazon.com/premiumsupport/knowledge-center/check-for-active-resources/)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully