Windows Azure でLL 言語系 Web アプリケーション開発
はてな 田中 慎司stanaka @ hatena.ne.jp
http://d.hatena.ne.jp/stanaka/http://twitter.com/stanaka/
アジェンダ
Azure とは GAE/AWS と比較
試してみた Hello World 級 PHP on Azure PHP & MySQL on Azure Mediawiki on Azure
スペック 基本的に Linux 系
いわゆる LAMP スタック Linux, Apache, MySQL, Perl IDE は使わない
Windows との接点 メインのノート PC は Windows VB, C# は知っている COM コンポーネントは一度書いたことがある
VisualStudio
今日のテーマ LL 系言語エンジニアからの視点
クラウドサービスでの Azure の位置付け Azure – AWS – GAE
Azure の勘所 とりあえず動かしてみる
Azure とは
Microsoft によるクラウドサービス アプリケーションの動作サービス (Windows Azure) ネットワークサービス (.NET Services) クラウドの RDB(SQL Azure)
Windows Azure
Windows Azure Compute Web Role Worker Role
Windows Azure Storage BLOB Table Queue Drive
Role
Web Role ウェブアプリケーション向け IIS .NET Framework
Worker Role バッチ処理向け
普通の TCP 通信もできる .NET Framework
Storage
BLOB メディアイメージ HTTP REST でアクセス
Table BigTable 的 DB
Queue メッセージキュー
Drive( 未リリース ?) NTFS ドライブ
Amazon Web Services EC2
IA サーバ環境を提供 時間貸しレンタルサーバ
ストレージ S3 CloudFront
サービス Elastic Block Storage Relational Database Service SimpleDB Simple Queue Service
Google Application Engine 自動的にスケールする 柔軟なデータストア (BigTable) サービス
URL フェッチ メール Memcache 画像操作 cron XMPP 通信
Google Application Engine の制約 言語の制約
Python/Java のみ 処理機能の制約
サーブレットによる 30 秒以上を要するリクエスト処理
サーブレットによるレスポンス送出時のデータストリーミング
ファイルシステムへの書き込み 外部サーバーへのソケット接続
VM 機能の制約 スレッド生成 ガベージコレクション実行やシステム停止 カスタムクラス・ローダの利用
Windows Azure の特性 EC2 と GAE の中間
インスタンスは意識する ただし各インスタンスの直接制御はできない
.NET が基本だが、ネイティブアプリも動作 C/C++ のコードも動く
言語処理系を上げてしまえば、なんでも動く !! MySQL/memcached も動く
開発環境 VisualStudio
Microsoft Visual Web Developer 2008 Express Edition
Eclipse Azure SDK
Azure Platform Interoperability
Windows Azure Tools for Eclipse PHP / Java などとの Interoperability に
も注力 http://www.microsoft.com/windowsazure/
interop/
Azure の開発フロー Visual Studio / Eclipse で開発
ビルドも ローカルテスト環境にデプロイ Azure にデプロイ
Staging で確認 Prodution に展開
LL 言語系 Web 屋から観点 .NET の世界
C# という言語がある IIS ってなんだ
Internet Information Services IDE に抵抗がある
統合開発環境 Eclipse / Visual Studio
Azure への期待 クラウドプレイヤーの増加
AWS vs GAE vs Azure 競争促進
ロックインの排除 アプリレイヤーとクラウドレイヤーの疎結合
化 Azure は、 GAE ほど極端ではない
Azure の開発フロー (LL 言語向け )
Emacs / vi で開発 コマンドラインでビルド
IDE だとよく分からない
簡単な Azure アプリ
/ProjectRoot/ WebRole/ index.html simple.csdef .. クラウド・サービス定義ファイル simple.cscfg .. クラウド・サービス構成ファイル
簡単な Azure アプリ simple.csdef クラウド・サービス定義
ファイル<?xml version="1.0" encoding="utf-8"?><ServiceDefinition name="Simple" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition"> <WebRole name="WebRole"> <ConfigurationSettings> </ConfigurationSettings> <InputEndpoints> <!-- Must use port 80 for http and port 443 for https when running in the cloud --> <InputEndpoint name="HttpIn" protocol="http" port="80" /> </InputEndpoints> </WebRole></ServiceDefinition>
簡単な Azure アプリ simple.cscfg クラウド・サービス構成
ファイル
<?xml version="1.0"?><ServiceConfiguration serviceName="Simple" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration"> <Role name="WebRole"> <Instances count="1"/> <ConfigurationSettings> </ConfigurationSettings> </Role></ServiceConfiguration>
簡単な Azure アプリ index.html
<html> <head><title>Quick Sample</title></head> <body> This a quick sample. <br /> <img alt="Sample Photo" src="photo.jpg" /> </body></html>
簡単な Azure アプリ ビルド
デプロイ
>cspack simple.csdef /copyonlyWindows(R) Azure(TM) Packaging Tool version 1.0.0.0for Microsoft(R) .NET Framework 3.5Copyright (c) Microsoft Corporation. All rights reserved.
>csrun simple.csx simple.cscfgWindows(R) Azure(TM) Desktop Execution Tool version 1.0.0.0for Microsoft(R) .NET Framework 3.5Copyright (c) Microsoft Corporation. All rights reserved.Using session id 1Created deployment(34)Started deployment(34)Deployment input endpoint HttpIn of role WebRole at http://127.0.0.1:82/
簡単な Azure アプリ 開発環境にデプロイ
簡単な Azure アプリ 動作 !
簡単な Azure アプリ 本番
PHP on Azure
PHP の言語処理系 (php.exe) を Azure 上に送りこむ ネイティブコードサポート !
PHP on Azure
/ProjectRoot/ WebRole/ index.php web.config simple.csdef .. クラウド・サービス定義ファイル simple.cscfg .. クラウド・サービス構成ファイル
PHP on Azure
phpsimple.csdef クラウド・サービス定義ファイル
<?xml version="1.0" encoding="utf-8"?><ServiceDefinition name="phpsimple" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition"> <WebRole name="WebRole" enableNativeCodeExecution="true“> <ConfigurationSettings> </ConfigurationSettings> <InputEndpoints> <!-- Must use port 80 for http and port 443 for https when running in the cloud --> <InputEndpoint name="HttpIn" protocol="http" port="80" /> </InputEndpoints> </WebRole></ServiceDefinition>
PHP on Azure
phpsimple.cscfg クラウド・サービス構成ファイル
<?xml version="1.0"?><ServiceConfiguration serviceName="phpsimple" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration"> <Role name="WebRole"> <Instances count="1"/> <ConfigurationSettings> </ConfigurationSettings> </Role></ServiceConfiguration>
PHP on Azure
index.php
<html><head><title>Hello World PHP</title></head><body><?phpecho 'Today is '. date('Y-m-d') ."\n";?> </body></html>
PHP on Azure
web.config<?xml version="1.0"?><configuration> <system.webServer> <handlers> <add name="FastCGIHandler" verb="*" path="*.php" scriptProcessor="%RoleRoot%\approot\PHP\php-cgi.exe" modules="FastCgiModule" resourceType="Unspecified" /> </handlers> <defaultDocument> <files> <add value="index.php"/> </files> </defaultDocument> </system.webServer></configuration>
PHP on Azure
web.roleconfig
<?xml version="1.0" encoding="utf-8" ?><configuration> <system.webServer> <fastCgi> <application fullPath="%RoleRoot%\approot\PHP\php-cgi.exe" /> </fastCgi> </system.webServer></configuration>
PHP on Azure
ビルド
デプロイ
>cspack phpsimple.csdef /copyonlyWindows(R) Azure(TM) Packaging Tool version 1.0.0.0for Microsoft(R) .NET Framework 3.5Copyright (c) Microsoft Corporation. All rights reserved.
>csrun phpsimple.csx phpsimple.cscfgWindows(R) Azure(TM) Desktop Execution Tool version 1.0.0.0for Microsoft(R) .NET Framework 3.5Copyright (c) Microsoft Corporation. All rights reserved.Using session id 1Created deployment(32)Started deployment(32)Deployment input endpoint HttpIn of role WebRole at http://127.0.0.1:82/
PHP on Azure
開発環境にデプロイ
PHP on Azure
動作 !
PHP & MySQL on Azure
Windows Azure MySQL PHP Solution Accelerator http://code.msdn.microsoft.com/
winazuremysqlphp
MySQL php.exe に加え、 mysql.exe も送りこむ Worker Role で mysql のインスタンスを起動
させる
PHP & MySQL on Azure
Windows Azure MySQL PHP Solution Accelerator をダウンロード・展開
PHP, MySQL, PHPMyAdmin などを詰め込む
ディレクトリ構成
MySQL を WorkerRole で動かす LocalStorage を定義
(ServiceDefinition.csdef)
<LocalResources> <LocalStorage name="InstMgrLocalStorage" sizeInMB="75" cleanOnRoleRecycle="false" /> <LocalStorage name="MySQL" sizeInMB="75" cleanOnRoleRecycle="false" /> <LocalStorage name="MySQLDatastore" sizeInMB="122880" cleanOnRoleRecycle="false" /> <LocalStorage name="BlobBackup" sizeInMB="2" cleanOnRoleRecycle="false" /></LocalResources>
MySQL を WorkerRole で動かす Wrapper で指定 (MySQLAccess.cs)
public bool Start(int id){ try { string baseDir = RoleEnvironment.GetLocalResource("MySQL").RootPath.Replace('\\', '/'); string dataDir = RoleEnvironment.GetLocalResource("MySQLDatastore").RootPath.Replace('\\', '/'); string blobDir = RoleEnvironment.GetLocalResource("BlobBackup").RootPath.Replace('\\', '/');
…
File.Copy("my.ini", iniFile, true); //update teh my.ini file with mysql server details UpdateMyIni(iniFile, baseDir, dataDir, port, id.ToString());
PHP & MySQL on Azure
PHP & MySQL on Azure
Mediawiki on Azure
Windows Azure Mediawiki MySQL Solution Accelerator http://code.msdn.microsoft.com/
winazuremediawiki 基本的には、 Windows Azure MySQL PHP
Solution Accelerator と同等
Mediawiki on Azure
memcached を動かす MySQL と 同 様 に Worker Role + C# の
Wrapperprotected void RunMemcached() { IPEndPoint endpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["memcached"].IPEndpoint; string cacheSize = RoleEnvironment.GetConfigurationSettingValue("CacheSizeInMB"); string arguments = "-m " + cacheSize + " -l " + endpoint.Address + " -p " + endpoint.Port; ProcessStartInfo startInfo = new ProcessStartInfo();… try { using (Process exeProcess = Process.Start(startInfo)) { exeProcess.WaitForExit();
Mediawiki on Azure
その他 Ruby on Rails on Azure
FastCGI 対応のものは動かせる Ruby on Rails も http://rubyonrails.cloudapp.net
まとめ Azure を試してみた
LL 系言語アプリも十分動かせる Amazon EC2 の対抗として十分なポテン
シャルがありそう
ただし、もうすこし UNIX-like な開発環境があると親和性が高くなる OSX な開発者もいるので ..