Azure CLIからARMテンプレートを利用してUbuntuをデプロイする

◆ Live配信スケジュール ◆
サイオステクノロジーでは、Microsoft MVPの武井による「わかりみの深いシリーズ」など、定期的なLive配信を行っています。
⇒ 詳細スケジュールはこちらから
⇒ 見逃してしまった方はYoutubeチャンネルをご覧ください
【5/21開催】Azure OpenAI ServiceによるRAG実装ガイドを公開しました
生成AIを活用したユースケースで最も一番熱いと言われているRAGの実装ガイドを公開しました。そのガイドの紹介をおこなうイベントです!!
https://tech-lab.connpass.com/event/315703/

はじめに

Visual Studio 2015からAzure上にCentOSをデプロイしてみたよ。

という記事もありますが、今回はCentOS(CLI)からARMテンプレートを利用してAzure上にUbuntu14.04をデプロイしてみます。ARMテンプレートによるデプロイは仮想マシンだけでなく、WebAppsやSQL Databaseなどでもできます。

今回もSSHでログインできるまで試します。

Azure portalから構築する方法はこちらの記事も御覧ください。
Azureに仮想マシン(CentOS)を構築する

方法

Azure Resource Manage(以下、ARM)を使ってARMテンプレートから仮想マシンをデプロイします。このARMテンプレートはJSON形式で書かれており、デプロイ対象リソースの依存関係も定義します。Azure Portalでできる操作はほぼ全て記述可能です。

ARMテンプレートを使うことによって、常に一貫した状態でデプロイすることができます。

事前準備

Azure CLIが使える環境を用意

ARMテンプレートからデプロイする手段は、Azure CLI、Azure Portal、PowerShellなどいろいろありますが今回はAzure CLIを利用します。

Azure CLIのインストールについては下記記事が参考になります。ぜひご覧ください。
LinuxサーバからAzure CLIを使ってみました

また、Azure CLI のバージョンを 0.9.0以上にする必要があります。

# azure --version

SSH Keyの作成

仮想マシンを作成する際にSSH Keyペアが必要です。事前に作成します。

1.ssh-keygenで作成

# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): パスフレーズ入力
Enter same passphrase again: パスフレーズ再入力
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.

2.作成できたことを確認

鍵の作成方法についてはこちらの記事が参考になります。
OSS on Azureことはじめ 【連載第9回】 ー SSHをmore

# ls ~/.ssh
id_rsa  id_rsa.pub  known_hosts

デプロイ

それでは、実際にARMテンプレートを利用してAzureにリソースをデプロイしてみます。

1.Azureにログインする

# azure login
info:    Executing command login
|info:    To sign in, use a web browser to open the page https://aka.ms/devicelogin. Enter the code AAAAAAAAA to authenticate.
info:    login command OK

(https://aka.ms/devicelogin にブラウザからアクセスして、コマンドラインのcodeを入力します。)

※多要素認証でなく、組織のMicrosoftアカウントであれば非対話型での認証が可能なようです。azure login -u account@example.com

2.利用するサブスクリプションを指定する

サブスクリプションを確認します。

# azure account list
info: Executing command account list
data: Name Id Current State
data: -------------- ------------------------------------ ------- -------
data: TESTSUBSCRIPTION1 11112222-aaaa-bbbb-cccc-ddddddddddd true Enabled
data: TESTSUBSCRIPTION2 33334444-wwww-xxxx-yyyy-zzzzzzzzzzzz false Enabled
info: account list command OK

true が現在のサブスクリプションです。

変更する場合は

# azure account set TESTSUBSCRIPTION2
info: Executing command account set
info: Setting subscription to TESTSUBSCRIPTION2 with id 33334444-wwww-xxxx-yyyy-zzzzzzzzzzzz.
info: Changes saved
info: account set command OK

3.Azure CLI をResources Manager Modeにする
ModeはClassicとResources Managerの2種類があります。

Classicは、Azure Service Manager
Resources Managerは、Azure Resource Manager

Azure Resource ManagerでないとARMテンプレートからのデプロイができないので、Resources Manager Modeにします。

# azure config mode arm
info:    Executing command config mode
info:    New mode is arm
info:    config mode command OK

4.ARMテンプレートからデプロイする

ARMテンプレートは、作成する事もできますが、Azure クイック スタート テンプレートにARMテンプレートがあるので今回はこちらを利用します。

azure group create -n <リソースグループ名> -l <ロケーション> --template-uri <URI>

-n : Resources Groupの名前を指定します。

-l : Locationを指定します。
米国東部なら eastus
東日本なら japaneast
インド南部なら southindia
利用可能なLocationはazure location listで確認できます。

–template-uri : jsonが置かれているURIを指定します。ローカルにあるファイルを指定するなら--template-file

Resources Group名をarmtest
Locationをjapaneast
利用するJSONはhttps://raw.githubusercontent.com/azure/azure-quickstart-templates/master/101-vm-sshkey/azuredeploy.json

で実行します。公開鍵が必要なので準備しておいてください。

# azure group create -n armtest -l japaneast --template-uri https://raw.githubusercontent.com/azure/azure-quickstart-templates/master/101-vm-sshkey/azuredeploy.json

sshKeyDataを聞かれるので、事前準備で作成した公開鍵を貼り付けます。

# azure group create -n armtest -l japaneast --template-uri https://raw.githubusercontent.com/azure/azure-quickstart-templates/master/101-vm-sshkey/azuredeploy.json
info:    Executing command group create
+ Getting resource group armtest
+ Creating resource group armtest
info:    Created resource group armtest
info:    Supply values for the following parameters
sshKeyData:  ssh-rsa AAA---省略---Pn5P <your_user@yourdomain.com>

少し待ちます。

+ Initializing template configurations and parameters
+ Creating a deployment
info:    Created template deployment "azuredeploy"
data:    Id:                  /subscriptions/11112222-aaaa-bbbb-cccc-ddddddddddd/resourceGroups/armtest
data:    Name:                armtest
data:    Location:            japaneast
data:    Provisioning State:  Succeeded
data:    Tags: null
data:
info:    group create command OK

「Provisioning State: Succeeded」!。できたようです。

コマンドラインから確認してみます。

5.リソースグループを確認する
azure group listを使います。

# azure group list
info:    Executing command group list
+ Listing resource groups
data:    Name                       Location   Provisioning State  Tags:
data:    -------------------------  ---------  ------------------  -----
data:    armtest                    japaneast  Succeeded           null
info:    group list command OK

armtest が作成されてますね。

6.仮想マシンが作成されたことを確認する

グループ内のリソースを確認するにはazure resource list <リソース グループ名>を利用します。

# azure resource list armtest
info:    Executing command resource list
+ Listing resources
data:    Id                                                                                                                                                  Name                   Resource Group  Type                                     Parent  Location   Tags
data:    --------------------------------------------------------------------------------------------------------------------------------------------------  ---------------------  --------------  ---------------------------------------  ------  ---------  ----
data:    /subscriptions/11112222-aaaa-bbbb-cccc-ddddddddddd/resourceGroups/armtest/providers/Microsoft.Compute/virtualMachines/sshvm                        sshvm                  armtest         Microsoft.Compute/virtualMachines                japaneast  null
data:    /subscriptions/11112222-aaaa-bbbb-cccc-ddddddddddd/resourceGroups/armtest/providers/Microsoft.Network/networkInterfaces/sshNIC                     sshNIC                 armtest         Microsoft.Network/networkInterfaces              japaneast  null
data:    /subscriptions/11112222-aaaa-bbbb-cccc-ddddddddddd/resourceGroups/armtest/providers/Microsoft.Network/networkSecurityGroups/networkSecurityGroup1  networkSecurityGroup1  armtest         Microsoft.Network/networkSecurityGroups          japaneast  null
data:    /subscriptions/11112222-aaaa-bbbb-cccc-ddddddddddd/resourceGroups/armtest/providers/Microsoft.Network/publicIPAddresses/sshPublicIP                sshPublicIP            armtest         Microsoft.Network/publicIPAddresses              japaneast  null
data:    /subscriptions/11112222-aaaa-bbbb-cccc-ddddddddddd/resourceGroups/armtest/providers/Microsoft.Network/virtualNetworks/sshVNET                      sshVNET                armtest         Microsoft.Network/virtualNetworks                japaneast  null
data:    /subscriptions/11112222-aaaa-bbbb-cccc-ddddddddddd/resourceGroups/armtest/providers/Microsoft.Storage/storageAccounts/vhgf5canowvrcsasshvm         vhgf5canowvrcsasshvm   armtest         Microsoft.Storage/storageAccounts                japaneast  null
info:    resource list command OK

NICやIPやら仮想マシンなどが作成できたようです。

7.Azure Portalからも確認する

ss87

Azure Prtalからも作成できていることが確認できました。

8.SSH 接続する

秘密鍵を指定してSSH接続してみます。ユーザー名はazureuserです。

# ssh -i /root/.ssh/id_rsa azureuser@IPADDRESS
azureuser@sshvm:~$ uname -a
Linux sshvm 3.19.0-65-generic #73~14.04.1-Ubuntu SMP Wed Jun 29 21:05:22 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

問題なく接続できました。

終わりに

今回はAzure CLIからARMテンプレートを利用して仮想マシンをデプロイしてみました。Azure クイックスタートのテンプレートを利用しましたが、作成する
場合はVisual Studioを利用すれば比較的容易に作成することができます。また、仮想マシンを作成してそこからテンプレートをエクスポートするアプローチも良さそうです。

次回は、もう少し複雑はARMテンプレートを作成してみたいと思います。

アバター画像
About サイオステクノロジーの中の人です 88 Articles
サイオステクノロジーで働く中の人です。
ご覧いただきありがとうございます! この投稿はお役に立ちましたか?

役に立った 役に立たなかった

0人がこの投稿は役に立ったと言っています。


ご覧いただきありがとうございます。
ブログの最新情報はSNSでも発信しております。
ぜひTwitterのフォロー&Facebookページにいいねをお願い致します!



>> 雑誌等の執筆依頼を受付しております。
   ご希望の方はお気軽にお問い合わせください!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


質問はこちら 閉じる