Q & A

    版本為 16:26, 6 Oct 2024

    到這個版本。

    返回到 版本存檔.

    查閱目前版本

    Amazon AWS Q & A

    文章索引

     

    Amazon EC2 使用的重要概念

    關於計費

    • 按照服務類型可區分有不同計費方式,分別有 EC2、S3、其他等等。
    • EC2 按每個 Instance及開啟時的每小時計費,直到 Instnace 被 Terminate 後則不在計費。
    • EC2 區分 Instance 等級有不同計費,各等級費用比較表可參閱 http://www.ec2instances.info/
    • S3 按儲存資料量、下載傳輸資料量及其他操作次數(PUT, COPY, POST,etc....)。
       

    關於 Instance

    • 啟動一個新的 Instance 時需先設定一個 Key Pair、Instance Type及 Security Group,之後這些就無法再作變更。
    • 以 AMI 所啟動的 Instance 是唯讀的系統,當系統執行關機或從是 AWS Console 作 Instance Terminate 後,整個系統就會被刪除,如果只是執行 reboot,系統則不會有任何影響,資料也會繼續存在。若系統有資料永久存檔的考量時,應該加掛 EBS 儲存空間來作永久儲存。
       

    關於 Root Device

    • Instance Store
      • 系統磁區除了有 / 空間以外,還會掛載一個約 150 GB 的 /mnt
      • Instance 只能做 Reboot, Terminate
      • 一旦系統啟動失敗,其資料將無法被保留。
    • EBS
      • 系統只會有當初建立 Instance 的所用磁區
      • Instance可以做 Stop, Start, Reboot, Terminate
      • 如果系統啟動失敗,磁區資料仍會被保留,可以進行救援動作。
         

    關於 AMI

    • 註冊新的 AMI 時,必須擁有當時建立同個 AMI 的帳號 Credential(包含兩個 X.509 憑證檔),否則就無成功註冊。
       

    關於 Key Pair

    • 新建一個 Key Pair 後,檔案只能被下載一次,務必妥善保存此檔,除非沒有 Instance 正在使用這個 Key Pair 時。
       

    關於 AKI

    EC2 Network (Internal network)

    有時多個 EC2 Instances 需要互相存取時,為了考量效能及安全性,必須使用內部網路連結。

    • 每個 Instance 都會附帶一個 private IP 及 private DNS name
    • 透過 Security Group 可以開放不同的 Instance 來存取。
    • private IP & DNS 是會變動的,一旦 Instance 被關閉或終止後。
    如何設定不同 Instance 之間的內部網路存取

    設定 Security Group

    • Port range:3306 (以 MySQL 為例)
    • Source:10.1.2.3/29 (這是 inbound 的 IP)
      • security group 只能對 inbound 做控制,所以 10.1.2.3 必須是 mysql client instance,而這個 security group 必須套用在 mysql server instance。
      • sub-mask 必須是 29。

    如果要開放 ping

    • Create a new rule:Custom ICMP rule
    • type:All
    • Source:<instance IP>

    WinSCP 連接 Instance 時,使用 SFTP 連不上,但換成 SCP 就正常

    編輯 /etc/ssh/sshd_config

    註解這一行
    #Subsystem      sftp    /usr/libexec/openssh/sftp-server
    

    主機自動備份-snapshot

    以 EC2 API 方式對 Instance 定期作 snapshot,並只保持最新期間的備份檔。

    作者網站:http://tiger-fish.com/blog/automated...ing-amazon-ec2
    專案首頁:http://code.google.com/p/ec2-delete-old-snapshots/

    主要的 EC2 API Script 有兩個:
    PHP 版本建議是 5.2+

    1. 新增 snapshot:這個 API 有兩種,一個是由 Java 開發的 EC2 API Tools;另一個是由 PHP 開發的 AWS SDK for PHP - CreateSnapshotSample.php。
    2. 刪除舊的 snapshot (ec2-manage-snapshot.php)

     

    Cron Script:

    #!/bin/bash
    
    DATESTAMP=`date +%Y%m%d`
    TIMESTAMP=`date +%H%M`
    LOGFILE="/var/log/tigerfish_ebs_backup.log"
    
    VOLUMES=( vol-12345678 vol-23456789 vol-34567890 )
    
    echo "TIGERFISH EBS BACKUP $DATESTAMP $TIMESTAMP" 2>&1 | tee -a $LOGFILE
    echo " " 2>&1 | tee -a $LOGFILE
    
    # Create a snapshot of each volume.
    for volume in ${VOLUMES[@]}
    do
      php CreateSnapshot.php -v $volume 2>&1 | tee -a $LOGFILE
    done
    
    # Remove older snapshots we don't need to keep any more.
    for volume in ${VOLUMES[@]}
    do
      php ec2-manage-snapshots.php -v $volume 2>&1 | tee -a $LOGFILE
    done
    

    CreateSnapshot.php

    • 這個 PHP 放在 ec2-manage-snapshots.zip (專案名稱是 ec2-delete-old-snapshots ),目錄是 /Amazon/EC2/Samples。
    • 預設的 AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY 變數是包含在同樣目錄的隱藏檔 .config.inc.php。
    • 必須先安裝 AWS SDK for PHP
    • 如果 snapshot 儲存有指定區域(region),可以修改成:
    $ec2Config = array ('ServiceURL' => 'https://eu-west-1.ec2.amazonaws.com');
    $service = new Amazon_EC2_Client(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, $ec2Config);
    

    ec2-manage-snapshots.php

    • 必須先安裝 AWS SDK for PHP
    • 預設的 AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY 變數是定義在 PHP 程式內,為了與 CreatSnapshot.php 作統一管理,可以自行修改成 .config.inc.php 方式來定義。

    Firefox Extensions

    如何取得 AWS 的各種認證資訊

    在 AWS 的服務會用到的認證種類區分三種:

    1. Access Key            - 包括有 Access Key ID 及 Secret Access Key 兩個資訊。
    2. X.509 Certificates  - 有關 AMI 的操作時會需要用到。
    3. Key Pairs                - 用來連接 Instance 時之用,有兩處可以取得,不過要注意:每新增一個 Key Pair 只能被下載一次,所以務必保留妥當,並且不可與他人分享。
      • AWS Portal > Security Cedentials > Key Pairs
      • AWS Management Console > Key Pairs
         

     Access Key ID 與 Secret Access Key 是 AWS 基本的身份認證,只要在網頁上完成註冊,就會有這兩組資訊。
    要取得以上這些資訊,可以點選以下連結後,輸入 AWS 的帳號/密碼 即可。

    http://aws-portal.amazon.com/gp/aws/...ion=access-key

    註:key pairs 的管理不用透過這個連結,在 AWS Console 也能操作。

    使用 Putty 登入 Instance

    1. 在 AWS Console > Key Pairs,新增一組 key pair,並下載 xxx.pem 到本地電腦。(key pair 只有在新增時才能被下載,之後就無法再下載,如果遺失 key pair,就重新再新增一個,不過別忘了,也要一併修改 Instance  的設定)
    2. 下載 PuTTYgen,這工具將用來產生 PuTTY Private Key file(*.ppk)
      執行 PuTTYgen > Conversions > Import key, 載入 xxx.pem > Save private key(提示沒有輸入 key passphrase, 按 yes) > 存檔成 xxx.ppk
    3. 開啟 PuTTY > 輸入 IP > Connection type: SSH >
      Connection > SSH > Auth > Private key file for authentication: 載入 xxx.ppk > Open 連線
    4. 輸入 root,不用密碼就能進入 console。

    ( http://docs.amazonwebservices.com/AW...tml?putty.html )

    How to create pk-*.pem & cert-*.pem for API-Tools

    Notes: The Private key file can only be downloaded at once.

    Making an AMI from existing instance

    Today I will describe in short how to create an AMI instance of EC2 and than upload it to S3 server. All you need to have is running instance of EC2 server (I’m using Debian) with EC2
    tools preinstalled.

    First of all, make sure that you don’t have any unnecessary files on your server filesystem which you don’t need to bundle in an image as it will only slow the process down and make
    your costs of storing backup images higher.
    Once you are sure that your instance is fine and EC2 tools are in place you have to transfer your private key file and certificate file to the server. Lets put the files under /root
    directory.
    If all is set up correctly, first of all, we create and image by running:

    ec2-bundle-vol -d /mnt -k /root/pk-yours.pem --cert /root/cert-yours.pem -u 012345678901
    

    The number after the -u flag is your Amazon account ID which you can find on the AWS website once logged in.
    Running above command will take a bit and will create a number of files under /mnt directory.
    Once we have the files and manifest ready (all happens auto-magically) we can start transferring the image onto S3 account.

    In order to transfer your image on S3 you would have to create a bucket there first, for this you can use famous firefox plugin (S3 Organizer) or do it using Affirma’s library (previous post).

    ec2-upload-bundle -b yourbucketname00404042009 -m /mnt/image.manifest.xml -a your_s3_access_key -s your_s3_secret_access_key
    

    If for some reason you encouter any problems during upload but your bundling went ok, you can retry the upload using –retry flag:

    ec2-upload-bundle -b yourbucketname00404042009 -m /mnt/image.manifest.xml -a your_s3_access_key -s your_s3_secret_access_key --retry
    

    註冊備份好的 AMI image

    使用指令:(若要定義比較詳細的資料,必須用指定,例如 description)

    ec2-register -K pk-*.pem -C cert-*.pem your-bucket/image.manifest.xml -d this_is_description
    

    NOTES:

    * 註冊時的 Credential 必須與 AMI 的建立者相同,否則無法成功註冊。

    * pk-*.pem, cert-*.pem 這兩個是 x509 的 key,需要透過 AWS Console 來產生。

    * 註冊成功後的 AMI,可以在 Elasticfox > Images > Filter: My AMIs 看到。

    * 更多參數可以定義其他資訊,詳細參閱 ec2-register

    使用 AWS Console:

    AWS Management Console > AMIs > Register New AMI

    輸入S3 的 AMI 路徑,例如 your-s3-bucket/dir1/images.manifest.xml

    Notes:

    以上兩種方式所註冊的 AMI 屬於 private,只有自己才能使用。

    註冊好的 Private AMI 可以設定給指定的其他帳號,或以 Public 模式讓所有可以使用,方法如下:

    AWS Management Console > AMIs > Viewing: Owned By Me, All Platforms > 選擇指定的 AMI

    Permission > 選擇 Public 或輸入要分享的 AWS Account ID。

    安裝 EC2 API Tools

    API Tools Reference:http://docs.amazonwebservices.com/AW...reference.html

    // 下載 JRE 1.6 - jre-6u21-linux-i586.bin

    http://www.oracle.com/technetwork/ja...ads/index.html

    // 安裝 JRE 1.6

    # chmod u+x jre-6u21-linux-i586.bin
    # ./jre-6u21-linux-i586.bin
    # mv jre1.6.0_21/ /opt
    

    // 設定 JRE

    # vi /etc/profile.d/java.sh
    
    export JAVA_HOME=/opt/jre1.6.0_21
    export PATH=$JAVA_HOME/bin:$PATH 
    

    // 下載 ec2-api-tools

    http://developer.amazonwebservices.c...externalID=351

    // 安裝 ec2-api-tools

    cd /root
    unzip  ec2-api-tools.zip
    mv ec2-api-tools-1.3-53907/ .ec2/
    vi ~/.bashrc
    
    # for EC2 API Tools
    export EC2_HOME=~/.ec2
    export PATH=$PATH:$EC2_HOME/bin 
    

    如何增加 EBS volume 空間

    ( http://blog.edoceo.com/2009/02/amazo...w-storage.html )

    基本流程:umount > snapshot > create an new volume from snapshot > attach > FS check with e2fsck > resize > FS check again > mount

    NOTES:

    要增加一個線上系統的 EBS volume,系統必須要停機,只是參照本章節的程序,可以讓你盡可能的縮短 downtime。

    // Step1

    假設 EBS volume 是用於儲存資料庫之類,請務必關閉資料庫後,umount EBS volume 再繼續後續的作業;反之,若儲存的資料類型是屬於 off-line,且沒有鎖檔的顧慮,可以不需要 umount EBS volume。

    // Step2

     透過 EC2 command 或 Elasticfox 對 EBS volume 新增一份 snapshot

    // Step3

    透過 EC2 command 或 Elasticfox 對剛新增的 snapshot 新增一個 EBS volume

    // Step4

    透過 EC2 command 或 Elasticfox 附加(attach)新的 EBS volume 到一個 instance,例如為 /dev/sdb。

    TIPS:

    為了縮短 downtime,這裡可以另外啟動一個 instance,或者也可以用 production instance。

    // Step5

    SSH 登入 instance,執行:

    # e2fsck -f /dev/sdb
    # resize2fs -p /dev/sdb
    # e2fsck -f /dev/sdb
    # tune2fs -l /dev/sdb
    

    // Step6

    掛載 /dev/sdb,並檢查資料是否完整。

    心得總結:

    1. 依照本教學,需要 downtime 的作業只有作 snapshot 及 mount/umount 時。
    Powered by MindTouch Core