LinuxユーザーがイジるはじめてのAzure

LinuxユーザーがAzureを使いこなせるように応援するブログです

はじめてのMicrosoft Azure(ロードバランサ#2)

前回までの記事は以下です。

 

はじめてのMicrosoft Azure(ロードバランサ#1) - LinuxユーザーがイジるはじめてのAzure

 

Azureのロードバランサーの種類が理解できましたので、次は2つ目のステップにはいります。

 

2、WEBアクセスできる仮想マシンを2台用意し上位にロードバランサ

  を設置する

 

では早速WEB用途のサーバーを2台立ち上げましょう。

 

・・・。

 

はい、Azureポータルでポチポチと2台の仮想マシンを作成するのは既にできると思いますが、同じことを行うのは面倒ですので、簡略化しましょう。

 

Azureにはテンプレートからシステムリソースを一気にデプロイする手立てがあります。Template DeployやExport templateなどが該当します。

 

ここでいうテンプレートとは、JSON形式で記載された構造定義となります。つまり1度作成したリソースグループ内の環境をすべて定義化したテンプレートを出すこともできますし、あらかじめ用意されているJSON定義や自作したJSON定義を使って、一気に環境構築を行うことができます。最近ではInfrastructure as a codeなんて呼ばれ方をしていますが、コードからIaaS環境を操作してしまう、というやり方です。

 

ここでは2台のCentOS7.2を一気に作成するテンプレートを紹介しますが、テンプレートの書き方やお作法など、非常に丁寧に記載されている記事がありますので、まずはこちらの記事を読んで、試して、理解してみてください。

 

 

www.cloudninja.asia

 

いかがでしたか?内容はUbuntuですが、RHELCentOSもほぼ一緒です。

とはいえ、白紙の状態からゴリゴリ記載するにはかなり手馴れていないとできませんので、まずはひな型とするテンプレートを用意し、2台の仮想マシンを作成する、といったオリジナルなカスタマイズをしたいですね。

 

AzureのQuick Start Templateでは、Gitに上がった有志の方々から参考になりそうなテンプレートがたくさんあります。まずはそれを覗いてみましょう。

 

 Azureポータルの左メニューにある「リソースグループ」から、「設定」を選ぶと下の方に「Export template」というメニューがあります。こちらをクリックしてみてください。

 

 

作成したばかりのリソースグループであれば、上記のようにJSON定義には何も記載されていない状態になります。上にあるデプロイのアイコンをクリックしましょう。

 

 

「テンプレートの編集」 をクリックすると上記の画面が出てくると思います。クイックスタートテンプレートというアイコンが出ていますので、そちらをクリックします。

 

 

テンプレートはたくさんありますので、「cent」 と入れてcentOSに関するテンプレートを一覧で出してみてください。他のサービスやOSと比べるとかなり少ないですが、いくつかテンプレートが用意されていることがわかります。

※ちなみに(免責事項)にも記載されている通り、このテンプレートはGitに登録した有志の方々が作成されたものを呼び込んでいるにすぎません。オフィシャルなサポートとして受けれるものではない、ということだけ理解しておきましょう。

 

ざっと見たところ2台のCentOS7.2を作成するテンプレートはなさそうです。参考になりそうなものはありますが、デプロイが失敗するテンプレートも中にはありますので注意しましょう。

 

では、クイックスタートテンプレートには目的のものがなさそうですので、これまでに自分が作成したリソースグループの定義をエクスポートし、それをひな型にし修正していきましょう。

 

今まで触っていたCentOS7.2を入れたリソースグループのExport templateから、ひな型をコピーし、Visual Studio Codeに張り付けてください。

 

 

上記のようにカラフルな状態になってなければ、一旦json拡張子として保存してください。

 

ざっと見てみるとわかりますが、この辺をいじればいいのかな、というものものあれば、これはどういじればよいのかな、というものもあると思います。

 

以下、私がちょこっと修正したテンプレートがありますので、※印の内容を読んでご利用ください。

 

※デプロイ前の準備:templatetest-rgというリソースグループを作成しておくこと。

JSON内でやってること

 1、パラメータで新規入力しなければいけないものはadominのパスワードだけ。

 2、SSHキーの公開キーは変更できるようパラメータとして設定。defaultで

   入れている通り、rsa-から始まる公開キー形式にする必要がありますが

   既に作成された仮想マシンのExport templateに記載されているため、それを

   コピペしてください。(ここでは「ssh-rsa AAAAZZZZ」と適当に記載してます。

 3、前回までに作成したCentOS7.2との違いは以下。

   ・ディスクはOSディスクのみ。追加したディスクは外しています。

   ・Network Security Group(FWみたいなもの)では22portだけでなく80portも

    許可しています。

   ・vNETは192.168.0.0/16とし、subnet1に192.168.10.0を用意しています。

   ・プライベートのアドレスは192.168.10.1と、もう1台は192.168.10.2と

    してあります。

 

----ここから-----

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "virtualMachines_templatecent_adminPassword": {
            "defaultValue": null,
            "type": "SecureString"
        },
        "sshKeyData": {
            "type": "string",
            "defaultValue": "ssh-rsa AAAAZZZZ",
            "metadata": {
            "description": "SSH rsa public key file as a string."
            }
        },
        "availabilitySets_templateavail_name": {
            "defaultValue": "templateavail",
            "type": "String"
        },
        "virtualMachines_templatecent_name": {
            "defaultValue": "templatecent",
            "type": "String"
        },
        "virtualMachines_templatecent_name2": {
            "defaultValue": "templatecent2",
            "type": "String"
        },
        "networkInterfaces_templatecent_name": {
            "defaultValue": "templatenic",
            "type": "String"
        },
        "networkInterfaces_templatecent_name2": {
            "defaultValue": "templatenic2",
            "type": "String"
        },
        "networkSecurityGroups_templatecent_name": {
            "defaultValue": "templatecent",
            "type": "String"
        },
        "publicIPAddresses_templatecent_name": {
            "defaultValue": "templatepubip",
            "type": "String"
        },
        "publicIPAddresses_templatecent_name2": {
            "defaultValue": "templatepubip2",
            "type": "String"
        },
        "virtualNetworks_templatetest_rg_name": {
            "defaultValue": "templatetest-rg",
            "type": "String"
        },
        "storageAccounts_templatetestrg_name": {
            "defaultValue": "templatetestrg",
            "type": "String"
        }
    },
    "variables": {},
    "resources": [
        {
            "comments": "リソース '/subscriptions/00b9a304-ca87-4f5e-b622-f782c041e0ad/resourceGroups/templatetest-rg/providers/Microsoft.Compute/availabilitySets/templateavail01' から一般化されました。",
            "type": "Microsoft.Compute/availabilitySets",
            "name": "[parameters('availabilitySets_templateavail_name')]",
            "apiVersion": "2015-06-15",
            "location": "japaneast",
            "properties": {
                "platformUpdateDomainCount": 5,
                "platformFaultDomainCount": 3
            },
            "dependsOn":
        },
        {
            "comments": "リソース '/subscriptions/00b9a304-ca87-4f5e-b622-f782c041e0ad/resourceGroups/templatetest-rg/providers/Microsoft.Compute/virtualMachines/templatecent01' から一般化されました。",
            "type": "Microsoft.Compute/virtualMachines",
            "name": "[parameters('virtualMachines_templatecent_name')]",
            "apiVersion": "2015-06-15",
            "location": "japaneast",
            "properties": {
                "availabilitySet": {
                    "id": "[resourceId('Microsoft.Compute/availabilitySets', parameters('availabilitySets_templateavail_name'))]"
                },
                "hardwareProfile": {
                    "vmSize": "Standard_A2"
                },
                "storageProfile": {
                    "imageReference": {
                        "publisher": "OpenLogic",
                        "offer": "CentOS",
                        "sku": "7.2",
                        "version": "latest"
                    },
                    "osDisk": {
                        "name": "[parameters('virtualMachines_templatecent_name')]",
                        "createOption": "FromImage",
                        "vhd": {
                            "uri": "[concat('https', '://', parameters('storageAccounts_templatetestrg_name'), '.blob.core.windows.net', concat('/vhds/', parameters('virtualMachines_templatecent_name'),'2016411173644.vhd'))]"
                        },
                        "caching": "ReadWrite"
                    },
                    "dataDisks":

                },
                "osProfile": {
                    "computerName": "[parameters('virtualMachines_templatecent_name')]",
                    "adminUsername": "koike",
                    "linuxConfiguration": {
                        "disablePasswordAuthentication": true,
                        "ssh": {
                            "publicKeys": [
                                {
                                    "path": "/home/koike/.ssh/authorized_keys",
                                    "keyData": "[parameters('sshKeyData')]"
                                }
                            ]
                        }
                    },
                    "secrets": ,
                    "adminPassword": "[parameters('virtualMachines_templatecent_adminPassword')]"
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaces_templatecent_name'))]"
                        }
                    ]
                }
            },
            "dependsOn": [
                "[resourceId('Microsoft.Compute/availabilitySets', parameters('availabilitySets_templateavail_name'))]",
                "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccounts_templatetestrg_name'))]",
                "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaces_templatecent_name'))]"
            ]
        },
        {
            "comments": "リソース '/subscriptions/00b9a304-ca87-4f5e-b622-f782c041e0ad/resourceGroups/templatetest-rg/providers/Microsoft.Compute/virtualMachines/templatecent01' から一般化されました。",
            "type": "Microsoft.Compute/virtualMachines",
            "name": "[parameters('virtualMachines_templatecent_name2')]",
            "apiVersion": "2015-06-15",
            "location": "japaneast",
            "properties": {
                "availabilitySet": {
                    "id": "[resourceId('Microsoft.Compute/availabilitySets', parameters('availabilitySets_templateavail_name'))]"
                },
                "hardwareProfile": {
                    "vmSize": "Standard_A2"
                },
                "storageProfile": {
                    "imageReference": {
                        "publisher": "OpenLogic",
                        "offer": "CentOS",
                        "sku": "7.2",
                        "version": "latest"
                    },
                    "osDisk": {
                        "name": "[parameters('virtualMachines_templatecent_name2')]",
                        "createOption": "FromImage",
                        "vhd": {
                            "uri": "[concat('https', '://', parameters('storageAccounts_templatetestrg_name'), '.blob.core.windows.net', concat('/vhds/', parameters('virtualMachines_templatecent_name2'),'2016411173645.vhd'))]"
                        },
                        "caching": "ReadWrite"
                    },
                    "dataDisks":

                },
                "osProfile": {
                    "computerName": "[parameters('virtualMachines_templatecent_name2')]",
                    "adminUsername": "koike",
                    "linuxConfiguration": {
                        "disablePasswordAuthentication": true,
                        "ssh": {
                            "publicKeys": [
                                {
                                    "path": "/home/koike/.ssh/authorized_keys",
                                    "keyData": "[parameters('sshKeyData')]"
                                }
                            ]
                        }
                    },
                    "secrets": ,
                    "adminPassword": "[parameters('virtualMachines_templatecent_adminPassword')]"
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaces_templatecent_name2'))]"
                        }
                    ]
                }
            },
            "dependsOn": [
                "[resourceId('Microsoft.Compute/availabilitySets', parameters('availabilitySets_templateavail_name'))]",
                "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccounts_templatetestrg_name'))]",
                "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaces_templatecent_name2'))]"
            ]
        },
        {
            "comments": "リソース '/subscriptions/00b9a304-ca87-4f5e-b622-f782c041e0ad/resourceGroups/templatetest-rg/providers/Microsoft.Network/networkInterfaces/templatecent0180' から一般化されました。",
            "type": "Microsoft.Network/networkInterfaces",
            "name": "[parameters('networkInterfaces_templatecent_name')]",
            "apiVersion": "2015-06-15",
            "location": "japaneast",
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig1",
                        "properties": {
                            "privateIPAddress": "192.168.10.1",
                            "privateIPAllocationMethod": "Dynamic",
                            "publicIPAddress": {
                                "id": "[resourceId('Microsoft.Network/publicIPAddresses', parameters('publicIPAddresses_templatecent_name'))]"
                            },
                            "subnet": {
                                "id": "[concat(resourceId('Microsoft.Network/virtualNetworks', parameters('virtualNetworks_templatetest_rg_name')), '/subnets/subnet1')]"
                            }
                        }
                    }
                ],
                "dnsSettings": {
                    "dnsServers":

                },
                "enableIPForwarding": false,
                "networkSecurityGroup": {
                    "id": "[resourceId('Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroups_templatecent_name'))]"
                }
            },
            "dependsOn": [
                "[resourceId('Microsoft.Network/publicIPAddresses', parameters('publicIPAddresses_templatecent_name'))]",
                "[resourceId('Microsoft.Network/virtualNetworks', parameters('virtualNetworks_templatetest_rg_name'))]",
                "[resourceId('Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroups_templatecent_name'))]"
            ]
        },
        {
            "comments": "リソース '/subscriptions/00b9a304-ca87-4f5e-b622-f782c041e0ad/resourceGroups/templatetest-rg/providers/Microsoft.Network/networkInterfaces/templatecent0180' から一般化されました。",
            "type": "Microsoft.Network/networkInterfaces",
            "name": "[parameters('networkInterfaces_templatecent_name2')]",
            "apiVersion": "2015-06-15",
            "location": "japaneast",
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ipconfig2",
                        "properties": {
                            "privateIPAddress": "192.168.10.2",
                            "privateIPAllocationMethod": "Dynamic",
                            "publicIPAddress": {
                                "id": "[resourceId('Microsoft.Network/publicIPAddresses', parameters('publicIPAddresses_templatecent_name2'))]"
                            },
                            "subnet": {
                                "id": "[concat(resourceId('Microsoft.Network/virtualNetworks', parameters('virtualNetworks_templatetest_rg_name')), '/subnets/subnet1')]"
                            }
                        }
                    }
                ],
                "dnsSettings": {
                    "dnsServers":
                },
                "enableIPForwarding": false,
                "networkSecurityGroup": {
                    "id": "[resourceId('Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroups_templatecent_name'))]"
                }
            },
            "dependsOn": [
                "[resourceId('Microsoft.Network/publicIPAddresses', parameters('publicIPAddresses_templatecent_name2'))]",
                "[resourceId('Microsoft.Network/virtualNetworks', parameters('virtualNetworks_templatetest_rg_name'))]",
                "[resourceId('Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroups_templatecent_name'))]"
            ]
        },
        {
            "comments": "リソース '/subscriptions/00b9a304-ca87-4f5e-b622-f782c041e0ad/resourceGroups/templatetest-rg/providers/Microsoft.Network/networkSecurityGroups/templatecent01' から一般化されました。",
            "type": "Microsoft.Network/networkSecurityGroups",
            "name": "[parameters('networkSecurityGroups_templatecent_name')]",
            "apiVersion": "2015-06-15",
            "location": "japaneast",
            "properties": {
                "securityRules": [
                    {
                        "name": "default-allow-ssh",
                        "properties": {
                            "protocol": "Tcp",
                            "sourcePortRange": "*",
                            "destinationPortRange": "22",
                            "sourceAddressPrefix": "*",
                            "destinationAddressPrefix": "*",
                            "access": "Allow",
                            "priority": 1000,
                            "direction": "Inbound"
                        }
                    },
                    {
                        "name": "default-allow-http",
                        "properties": {
                            "protocol": "Tcp",
                            "sourcePortRange": "*",
                            "destinationPortRange": "80",
                            "sourceAddressPrefix": "*",
                            "destinationAddressPrefix": "*",
                            "access": "Allow",
                            "priority": 1010,
                            "direction": "Inbound"
                        }
                    }                   
                ]
            },
            "dependsOn":

        },
        {
            "comments": "リソース '/subscriptions/00b9a304-ca87-4f5e-b622-f782c041e0ad/resourceGroups/templatetest-rg/providers/Microsoft.Network/publicIPAddresses/templatecent01' から一般化されました。",
            "type": "Microsoft.Network/publicIPAddresses",
            "name": "[parameters('publicIPAddresses_templatecent_name')]",
            "apiVersion": "2015-06-15",
            "location": "japaneast",
            "properties": {
                "publicIPAllocationMethod": "Static",
                "idleTimeoutInMinutes": 4
            },
            "dependsOn":
        },
        {
            "comments": "リソース '/subscriptions/00b9a304-ca87-4f5e-b622-f782c041e0ad/resourceGroups/templatetest-rg/providers/Microsoft.Network/publicIPAddresses/templatecent01' から一般化されました。",
            "type": "Microsoft.Network/publicIPAddresses",
            "name": "[parameters('publicIPAddresses_templatecent_name2')]",
            "apiVersion": "2015-06-15",
            "location": "japaneast",
            "properties": {
                "publicIPAllocationMethod": "Static",
                "idleTimeoutInMinutes": 4
            },
            "dependsOn":

        },
        {
            "comments": "リソース '/subscriptions/00b9a304-ca87-4f5e-b622-f782c041e0ad/resourceGroups/templatetest-rg/providers/Microsoft.Network/virtualNetworks/templatetest-rg' から一般化されました。",
            "type": "Microsoft.Network/virtualNetworks",
            "name": "[parameters('virtualNetworks_templatetest_rg_name')]",
            "apiVersion": "2015-06-15",
            "location": "japaneast",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "192.168.0.0/16"
                    ]
                },               
                "subnets": [
                    {
                        "name": "subnet1",
                        "properties": {
                            "addressPrefix": "192.168.10.0/24"
                        }
                    }
                ]
            },
            "dependsOn":
        },
        {
            "comments": "リソース '/subscriptions/00b9a304-ca87-4f5e-b622-f782c041e0ad/resourceGroups/templatetest-rg/providers/Microsoft.Storage/storageAccounts/templatetestrg2912' から一般化されました。",
            "type": "Microsoft.Storage/storageAccounts",
            "name": "[parameters('storageAccounts_templatetestrg_name')]",
            "apiVersion": "2015-06-15",
            "location": "japaneast",
            "tags": {},
            "properties": {
                "accountType": "Standard_LRS"
            },
            "dependsOn":

        }
    ]
}

----ここまで-----

 

あとはAzureポータルから以下の画面でOKしていきます。

 

 

「テンプレートの編集」では上記のJSON形式を上書きすれば終わりです。

「パラメータの編集」ではadminのパスワードを新規で入力し、SSHキーの部分をご自分のrsaから始まる公開キーに上書き変更してOKします。

「リソースグループ」は先ほど事前に準備したtemplatetest-rgを選択してください。必然的にリソースグループの場所は東日本固定になります。

「法律条項」はちょっとドキッとしますが、なんてことはなく内容をざっとよんで購入ボタン(購入って言葉にひっかかりますが、同意と解釈しましょう)をおします。

最後に作成、とするとデプロイがはじまります。

 

デプロイが正常に終わったかはポータル右上の鈴マークで確認できますので、進捗状況も含めてみてみてください。

 

長くなったのでここまでとしますが、次回の記事ではいよいよ仮想マシンにhttpdをあげてロードバランサを上位に組み込みましょう。