orenoblog

エンジニアになりたいExcel方眼紙erの物語

cloudmonkeyを使ってIDCFrontier Cloudで仮想マシンを起動しよう

普段GUIポチーしていたのですが、以前中の人にCLIcloudmonkeyが便利ですよと教えていただいたので、実際に使ってみました

クライアントはMac OS Xです。 pythonbrewを使いPython2.7.3をインストールしています。

cloudmonkeyのインストールとセットアップ

client$ pip install cloudmonkey
client$ vi ~/.cloudmonkey/config
[user]
apikey = <APIキー>
secretkey = <秘密鍵>

[server]
path = /portal/client/api
host = api.noahcloud.jp
protocol = https
port = 443
timeout = 3600 

cloudmonkeyでマシンを作る

cloudmonkeyを利用して以下の順で環境を準備します。

  1. SSH用Keypairを作成
  2. 仮想マシンの作成

  3. SSH keypairの作成 秘密鍵の情報はコピペして保管しておきます。

client $ cloudmonkey
> create sshkeypair name=server
keypair:
name = server
fingerprint = **:**:**:**:**:**
privatekey = -----BEGIN RSA PRIVATE KEY-----
*********************
-----END RSA PRIVATE KEY-----

IDCフロンティアから提供されているCentOS6.4テンプレートを利用します。 マシンの起動に最低限必要な情報は下記の通りです。

  • templateid(テンプレートのID)
  • serviceofferings(スペック)
  • sshkeypair(sshキーペア名)
  • zoneid(AZ)

  • テンプレートIDの取得

client $ cloudmonkey
> list templates templatefilter=all
※表示されない・・・
> list templates templatefilter=all id=3039
count = 2
template:
id = 3039☆
name = [LATEST] CentOS 6.4 64-bit
  • serviceofferingsの取得

今回はM4で起動したいのでidを調べます

client$ cloudmonkey
> list serviceofferings
count = 13
..省略..
name = M4
id = 21 ☆
..省略..
  • sshkeypair名の取得
client$ cloudmonkey
> list sshkeypairs name=server
count = 1
sshkeypair:
name = server
account = ****
domainid = ****
fingerprint = ****
  • ZoneIDの取得

現在2拠点利用可能です。今回はzoneid=1を利用します。

client$ cloudmonkey
> list zones
count = 2
zone:
name = jp-east-t1v
id = 1
..省略..
  • 起動

起動後に出力されるidを記録しておきます。 ポートフォワードの設定で必要になります。

client$ cloudmonkey
> deploy virtualmachine templateid=3039 keypair=server zoneid=1 displayname=server serviceofferingid=21
jobid = ******
jobprocstatus = 0
jobresult:
virtualmachine:
id = 53415☆
name = *-****-******-**
account = **********
cpunumber = *
cpuspeed = *****
created = ****-**-**T**:**:**+0900
displayname = server
domain = **************
domainid = ****
guestosid = ***
haenable = *****
hypervisor = *
memory = ****
nic:
id = *******
gateway  = *.*.*.*
ipaddress = *.*.*.*
isdefault = ****
macaddress = **:**:**:**:**:**
netmask = ***.***.***.***
networkid = *
traffictype = *
type = *
password = *
passwordenabled = *
rootdeviceid = *
rootdevicetype = *
securitygroup:
serviceofferingid = 21
serviceofferingname = *
state = Running
templatedisplaytext = Root Disk: 15GB, (v1)
templateid = 3039
templatename = [LATEST] CentOS 6.4 64-bit
zoneid = 1
zonename = *
jobresultcode = 0
jobresulttype = object
jobstatus = 1

これで仮想マシンが起動しました。

ネットワークの設定

今回作成した仮想マシンはインターネット経由でssh接続したいので 以下の流れで作業を行います

  1. グローバルIPの取得
  2. ファイアウォールの設定
  3. ポートフォワードの設定

  4. グローバルIPの取得

assciate ipaddressで取得できます。 zoneidは仮想マシンを起動したidを指定します。

client$ cloudmonkey
> associate ipaddress zoneid=1
jobid =*
jobprocstatus = 0
jobresult:
ipaddress:
id = 2142☆
account = *
..省略..
ipaddress = ***.***.***.***
...省略...

sshポートを許可します。

client$ cloudmonkey
> create firewallrule ipaddressid=2142 cidrlist=***.***.***.***/** protocol=tcp startport=22 endport=22
jobid = *****
jobprocstatus = 0
jobresult:
firewallrule:
id = 138856
cidrlist = ***.***.***.***/**
endport = 22
ipaddress = ***.***.***.***
ipaddressid = 2142
protocol = tcp
startport = 22
state = Active
jobresultcode = 0
jobresulttype = object
jobstatus = 1
  • ポートフォワード設定
client$ cloudmonkey
> create portforwardingrule ipaddressid=2142 virtualmachineid=53415 protocol=tcp publicport=22  publicendport=22 privateport=22 privateendport=22
  jobid = 831613
jobprocstatus = 0
jobresult:
portforwardingrule:
id = 138859
cidrlist =
ipaddress = ***.***.***.***
ipaddressid = 2142
privateendport = 22
privateport = 22
protocol = tcp
publicendport = 22
publicport = 22
state = Active
virtualmachinedisplayname = server
virtualmachineid = 53409
virtualmachinename = *
jobresultcode = 0
jobresulttype = object
jobstatus = 1

これで外部からsshで起動した仮想マシンへアクセスできるようになりました。

接続確認

作成した秘密鍵を準備してssh接続を確認します。 sshクライアント用のconfig

client$ vi config
Host server
    User root
    Port 22
    Hostname ***.****.***.***
    IdentityFile .ssh/server.pem
    IdentitiesOnly yes
client$ ssh -F config server
    ________  ______   ______                 __  _
   /  _/ __ \/ ____/  / ____/________  ____  / /_(_)__  _____
   / // / / / /      / /_  / ___/ __ \/ __ \/ __/ / _ \/ ___/
 _/ // /_/ / /___   / __/ / /  / /_/ / / / / /_/ /  __/ /
/___/_____/\____/  /_/   /_/   \____/_/ /_/\__/_/\___/_/

[root@host ~]#

これで接続確認完了です。 cloudmonkeyはタブ補完が利用できること、listコマンドの出力形式をtable, jsonで取得できるので大変便利です。 利用してみてはいかがでしょうか。

例) jsonフォーマット(set display json)で取得した情報をjqでフィルタリング

client$ cloudmonkey list virtualmachines|jq .count
17