[[PageOutline]]
{{{
#!html
實作六:Eucalyptus 管理介面和 Euca2ools 操作
}}}
= 【前言】 =
 * 必須已完成'''實作五''' (安裝好 Eucalyptus 和 啟動成功)
 * 我們使用 Eucalyptus 的 '''API''' 來控制 Eucalyptus (Eucalyptus 提供的 API 為 Euca2ools)
 * __'''指令說明:'''__
|| '''euca-describe-availability-zones verbose''' || 察看可用的資源       ||
|| '''euca-describe-instances'''                  || 察看目前正在執行的 VM ||
|| '''euca-bundle-image'''                        || 打包好將要上傳的檔案   ||
|| '''euca-upload-bundle'''                       || 上傳到檔案 Eucalyptus|| 
|| '''euca-register'''                            || 註冊上傳的檔案        ||
|| '''euca-describe-images'''                     || 察看可用的 images    ||
----
= 【流程】 =
{{{
#!graphviz
digraph finite_state_machine {
 rankdir=LR;
 ranksep=0.3;
 size="11,8";
 node[shape=box,width=3.0];
 
 "4.使用 Euca2ools 上傳 Kernel / Ramdisk / FS Image" -> "5.開啟 VM";
 "1.新增 Eucalyptus API 套件庫" -> "2.下載帳戶憑證" -> "3.使用 Euca2ools 來操作 Eucalyptus"; 
}
}}}
----
= 【Step 1:安裝 Eucalyptus API】 =
 * 使用 '''__aptitude install__'''指令安裝 euca2ools
{{{
$ sudo aptitude update
$ sudo aptitude -y install euca2ools 
}}}
----
= 【Step 2:下載帳戶憑證】 =
 * 使用你的帳戶登入到 Eucalyptus Web 並下載你的憑證 (要用 Euca2ools 控制 Eucalyptus 必須要讓 Eucalyptus 系統信任你,所以需要此憑證)
 * 下載至你的家目錄
[[Image(down.png,width=800)]]
{{{
$ cd 
$ mkdir euca
$ mv euca2-admin-x509.zip euca/
$ cd euca
$ unzip euca2-admin-x509.zip
}}}
 * 解開壓縮檔後,它有4個檔案,當中的 eucarc 是每次你要使用 Euca2ools 必須先 source 它 (他會幫設定好環境變數和認証Key)
{{{
$ cat eucarc
$ source eucarc
}}}
----
= 【Step 3:使用 Euca2ools 來操作 Eucalyptus】 =
 * 使用 Euca2ools 提供的 '''__ecca-describe-availability-zones__''' 指令察看可用的資源
{{{
$ euca-describe-availability-zones verbose
}}}
 * 使用 Euca2ools 提供的 '''__euca-describe-instances__''' 指令察看目前正在執行的 VM
{{{
$ euca-describe-instances
}}}
----
= 【Step 4:使用 Euca2ools 上傳 Kernel / Ramdisk / FS Image】 =
 * 目前 Eucalyptus 系統上並無 Kernel、Ramdisk 和 FS Image,所以我們使用官方提供的 Image (上傳需要用到三個指令)
   * '''__euca-bundle-image__''' - 打包好將要上傳的檔案
   * '''__euca-upload-bundle__''' - 上傳到 Eucalyptus
   * '''__euca-register__''' - 註冊上傳的檔案
 * 上傳 Kernel
{{{
$ euca-bundle-image -i /boot/vmlinuz-2.6.26-2-xen-amd64 --kernel true
$ euca-upload-bundle -b kernel -m /tmp/vmlinuz-2.6.26-2-xen-amd64.manifest.xml
$ euca-register kernel/vmlinuz-2.6.26-2-xen-amd64.manifest.xml
}}}
 * 上傳 Ramdisk
{{{
$ euca-bundle-image -i /boot/initrd.img-2.6.26-2-xen-amd64 --ramdisk true
$ euca-upload-bundle -b ramdisks -m /tmp/initrd.img-2.6.26-2-xen-amd64.manifest.xml
$ euca-register ramdisks/initrd.img-2.6.26-2-xen-amd64.manifest.xml
}}}
 * 上傳 FS Image
{{{
$ cd
$ scp clouder@192.168.1.254:euca-debian-5.0-x86_64.tar.gz .
$ tar zxvf euca-debian-5.0-x86_64.tar.gz
$ cd euca-debian-5.0-x86_64/
$ euca-bundle-image -i debian.5-0.x86-64.img
$ euca-upload-bundle -b images -m /tmp/debian.5-0.x86-64.img.manifest.xml
$ euca-register images/debian.5-0.x86-64.img.manifest.xml
}}}
 * 察看上傳的檔案
   * https://localhost:8443/#images
   * 使用 '''__euca-describe-images__''' 指令
{{{
$ euca-describe-images
}}}
----
= 【Step 5: 開啟 VM】 =
 * 產生 VM 前必須先拿到登入的 key,開啟之後才能順利的使用此 private key 來登入 VM
{{{
$ cd ~/euca
$ euca-describe-keypairs
$ euca-add-keypair mykey >mykey.private
$ chmod 0600 mykey.private
}}}
 * 使用此 mykey 來產生一個 VM
{{{
$ euca-describe-images
$ euca-run-instances -k mykey -n 1  
$ euca-describe-instances
}}}
 * 使用 mykey.private 來登入 VM
{{{
$ ssh -i mykey.private root@192.168.1.X
# ping ptt.cc
CTRL+D
}}}
 * 關掉 VM
{{{
$ euca-describe-instances
$ euca-terminate-instances 
$ euca-describe-instances
}}}
----
= 【補充:使用 Amazon EC2】 =
 * 操作 EC2,有三種方法可以操作:
   1. Amazon EC2 API tools (指令介面) (Eucalyptus 使用的 API 也是使用 EC2 再加以修改成的)
   2. Elasticfox (Firefox 外掛) (Eucalyptus 使用的則為 Hybridfox)
   3. Management Console (網頁介面)[[BR]][[Image(Amazon.png)]]
 * 這邊簡單介紹 API 的使用方法,剛好可以跟 Eucalyptus 做比較
   0. 系統需安裝好 JAVA 和設定 JAVA_HOME (因 API 使用的 lib 都是 jar 檔)
   1. 安裝 [http://developer.amazonwebservices.com/connect/entry.jspa?externalID=351&categoryID=88 API]
{{{
$ wget http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip
$ unzip ec2-api-tools.zip
}}}
   2. 下載 Amazon 給你的 Security Credentials (下載 X.509 Certificate 和 private key),將這兩個檔案放置 ec2 目錄下
   3. 設定環境變數
{{{
$ cd ec2
$ echo "export EC2_HOME=~/ec2-api-tools-1.3-51254/bin" >> ~/ec2/ec2rc
$ echo "export EC2_PRIVATE_KEY=~/ec2/pk-APKAIBFA52PVKFBXMUJA.pe" >> ~/ec2/ec2rc
$ echo "export EC2_CERT=~/ec2/cert-M2CNECVUTHTPYJHPZVCTSBQTBF3KWAPW.pem" >> ~/ec2/ec2rc
$ echo "export PATH=$PATH:~/ec2-api-tools-1.3-51254/bin"
$ source ~/ec2/ec2rc
}}}
   4. 測試
{{{
$ ec2-version
1.3-51254 2009-11-30
$ ec2-describe-availability-zones 
AVAILABILITYZONE	us-east-1a	available	us-east-1	
AVAILABILITYZONE	us-east-1b	available	us-east-1	
AVAILABILITYZONE	us-east-1c	available	us-east-1	
AVAILABILITYZONE	us-east-1d	available	us-east-1
$ ec2-describe-regions
REGION	eu-west-1	ec2.eu-west-1.amazonaws.com
REGION	us-east-1	ec2.us-east-1.amazonaws.com
REGION	us-west-1	ec2.us-west-1.amazonaws.com
REGION	ap-southeast-1	ec2.ap-southeast-1.amazonaws.com
$ ec2-describe-images -a
$ ec2-describe-images -o amazon
$ ec2-add-keypair mykeypair
//將畫面顯示的 key 資訊存到 mykeypair.pem
$ chmod 600 mykeypair.pem
$ ec2-describe-keypairs
$ ec2-run-instances ami-225fba4b -k mykeypair
RESERVATION	r-de50fab5	997070620294	default
INSTANCE	i-e93ddf83	ami-225fba4b			pending	mykeypair	0		m1.small	2010-06-23T07:24:01+0000	us-east-1d				monitoring-disabled		instance-store	
$ ec2-describe-instances
RESERVATION	r-de50fab5	997070620294	default
INSTANCE	i-e93ddf83	ami-225fba4b	ec2-204-236-192-34.compute-1.amazonaws.com	ip-10-196-215-0.ec2.internal	runningmykeypair	0		m1.small	2010-06-23T07:24:01+0000	us-east-1d				monitoring-disabled	204.236.192.34	10.196.215.0			instance-store
$ ssh -i mykeypair.pem root@ec2-204-236-192-34.compute-1.amazonaws.com
$ ec2-terminate-instances i-e93ddf83
INSTANCE	i-e93ddf83	running	shutting-down
}}}
   5. 其他的功能,有興趣的學員可自行玩玩
----
= 【補充:使用 !HybridFox】 =
 * 可透過瀏覽器外掛來監控 instances (基於 !ElasticFox 1.6 修改而成的)
 * [安裝] 
{{{
# wget http://hybridfox.googlecode.com/files/hybridfox-1.6.000024.xpi
}}}
   * 打開 firefox -> 工具 -> 附加元件 -> 安裝附加元件[[BR]][[Image(Hybridfox.png)]]
 * [設定]
   * 設定 Regions (可參考 eucarc 檔案的資訊)[[BR]][[Image(Regions2.png)]]
   * 設定 credentials (可參考 eucarc 檔案的資訊)[[BR]][[Image(Credentials.png)]]
 * [使用]
   * 目前功能還無法支援開啟 instance,但可以透過其看到目前正在執行的 VM,並對其進行關機和登入的操作[[BR]][[Image(instances.png)]]
   * 察看 Images[[BR]][[Image(Images.png)]]
   * 察看 Key Pairs[[BR]][[Image(Keys.png)]]
 * [登入 Instances]
   * 登入時要給予 private key[[BR]][[Image(Login.png)]][[BR]][[Image(Login2.png)]]
 * [Reference]
   * [http://code.google.com/p/hybridfox/ 官網]
   * [http://architects.dzone.com/news/hybridfox-elasticfox Using Hybridfox with Eucalyptus] 
   * [http://yogeshg1987.in/blog/2010/06/using-hybridfox-with-eucalyptus/ Hybridfox: Elasticfox for Eucalyptus]
   * [http://trac.nchc.org.tw/grid/wiki/Eucalyptus/HybirdFox 參考連結]
----
= 【討論 & 休息】 =
 * 練習再開啟兩個 VM
{{{
$ euca-run-instances -k mykey -n 2  
$ euca-describe-instances
}}}
 * 練習關掉全部 VM
{{{
$ euca-describe-instances
$ euca-terminate-instances   
$ euca-describe-instances
}}}
[[BR]]
[[BR]]