LoginSignup
5

More than 5 years have passed since last update.

AzureのCLIツールを使ってCoreOS/Docker上にWebサーバーを立ててみる。

Posted at

こんにちは、とりあえずAWSを選ぶWeb制作者へ!「EC2でWebサーバーを立てる」をAzureでやってみたという記事をLIGに書きました。この記事では管理画面からボタンをぽちぽち押してVMを立ち上げてました。

今回はAWSでもCLIツールがあるようにAzureでもCLIツールがあるので使ってみます。

筆者環境

  • macOS Sierra
  • Node.js v6.6.0

Azureアカウント (無料でけっこういける)

https://azure.microsoft.com/ja-jp/ からアカウントを作ってブラウザでログインしておきましょう。

Azureコマンドのインストール

参考: Windows Azure コマンドライン ツール を使ってみる

事前準備としてNode.jsのインストールが必須です。
npm経由でインストールできるのはNoderな僕にはかなりうれしいです。

Node.jsのインストールはこちらの記事を参考にしてください。

いまアツいJavaScript!ゼロから始めるNode.js入門〜5分で環境構築編〜

  • npm経由でインストール

npmのページ: azure-cli

npm install azure-cli -g

そこそこ時間かかります。

インストールが完了したら

azure

とコマンドを入力します。

Microsoft Azure CLI would like to collect data about how users use CLI
commands and some problems they encounter.  Microsoft uses this information
to improve our CLI commands.  Participation is voluntary and when you
choose to participate your device automatically sends information to
Microsoft about how you use Azure CLI.

If you choose to participate, you can stop at any time later by using Azure
CLI as follows:
1.  Use the azure telemetry command to turn the feature Off.
To disable data collection, execute: azure telemetry --disable

If you choose to not participate, you can enable at any time later by using
Azure CLI as follows:
1.  Use the azure telemetry command to turn the feature On.
To enable data collection, execute: azure telemetry --enable

Select y to enable data collection :(y/n) n

You choose not to participate in Microsoft Azure CLI data collection.

という表示がでるのでnを入力します。

info:             _    _____   _ ___ ___
info:            /_\  |_  / | | | _ \ __|
info:      _ ___/ _ \__/ /| |_| |   / _|___ _ _
info:    (___  /_/ \_\/___|\___/|_|_\___| _____)
info:       (_______ _ _)         _ ______ _)_ _
info:              (______________ _ )   (___ _ _)
info:
info:    Microsoft Azure: Microsoft's Cloud Platform
info:
info:    Tool version 0.10.4
help:
help:    Display help for a given command
help:      help [options] [command]
help:
help:    Log in to an Azure subscription using Active Directory or a Microsoft account identity.
help:      login [options]
help:
help:    Log out from Azure subscription using Active Directory. Currently, the user can log out only via Microsoft organizational account
help:      logout [options] [username]
help:
help:    Open the portal in a browser
help:      portal [options]
help:
help:    Manages the data collection preference.
help:      telemetry [options]
help:
help:    Commands:
help:      account          Commands to manage your account information and publish settings
help:      acs              Commands to manage your container service.
help:      ad               Commands to display Active Directory objects
help:      appserviceplan   Commands to manage your Azure appserviceplans
help:      availset         Commands to manage your availability sets.
help:      batch            Commands to manage your Batch objects
help:      cdn              Commands to manage Azure Content Delivery Network (CDN)
help:      config           Commands to manage your local settings
help:      datalake         Commands to manage your Data Lake objects
help:      feature          Commands to manage your features
help:      group            Commands to manage your resource groups
help:      hdinsight        Commands to manage HDInsight clusters and jobs
help:      insights         Commands related to monitoring Insights (events, alert rules, autoscale settings, metrics)
help:      iothub           Commands to manage your Azure IoT hubs
help:      keyvault         Commands to manage key vault instances in the Azure Key Vault service
help:      lab              Commands to manage your DevTest Labs
help:      location         Commands to get the available locations
help:      network          Commands to manage network resources
help:      policy           Commands to manage your policies on ARM Resources.
help:      powerbi          Commands to manage your Azure Power BI Embedded Workspace Collections
help:      provider         Commands to manage resource provider registrations
help:      quotas           Command to view your aggregated Azure quotas
help:      rediscache       Commands to manage your Azure Redis Cache(s)
help:      resource         Commands to manage your resources
help:      role             Commands to manage role definitions
help:      servermanagement Commands to manage Azure Server Managment resources
help:      storage          Commands to manage your Storage objects
help:      tag              Commands to manage your resource manager tags
help:      usage            Command to view your aggregated Azure usage data
help:      vm               Commands to manage your virtual machines
help:      vmss             Commands to manage your virtual machine scale sets.
help:      vmssvm           Commands to manage your virtual machine scale set vm.
help:      webapp           Commands to manage your Azure webapps
help:
help:    Options:
help:      -h, --help     output usage information
help:      -v, --version  output the application version
help:
help:    Current Mode: arm (Azure Resource Management)

これでazureコマンドのインストールは完了です。

Azure CLIで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 GUCG58FX8 to authenticate.

こういう表示がでます。 http://aka.ms/devicelogin にアクセスして表示されたコードで認証してくれと言っています。

ブラウザからURLにアクセスしてみます。

ログイン画面でコードGUCG58FX8を入力 (コードは自身の環境のものにあわせてください)

するとアカウントログインにリダイレクトされて、無事にコマンドライン側でログインができます。

Azure CLIからVMを立ち上げてみる

「AWSでいういわゆるEC2インスタンス立ち上げ」です。

Azureのページに載ってる使い方を参考に進めてみます

今回はCoreOSにしてみます。CoreOSはかなり薄いので軽く試すだけなら良いと思います。

azure vm quick-create -M ~/.ssh/id_rsa.pub -Q CoreOS

実行すると、次の項目を聞かれるので入力しましょう

  • Resource group name ・・・ リソースグループ名
  • Virtual machine name ・・・ VMの名前
  • Location name ・・・ ロケーション名 (東日本の場合は japan east )
  • Operating system Type [Windows, Linux] ・・・ OSのタイプ
  • User name ・・・ ログインユーザー名
$ azure vm quick-create -M ~/.ssh/id_rsa.pub -Q CoreOS

info:    Executing command vm quick-create
Resource group name:  n0bisuke-test
Virtual machine name:  n0bi-vm
Location name:  japan east 
Operating system Type [Windows, Linux]:  Linux
User name:  n0bisuke
+ Listing virtual machine sizes available in the location "japaneast"
+ Looking up the VM "n0bi-vm"
info:    Verifying the public key SSH file: /Users/n0bisuke/.ssh/id_rsa.pub
info:    Using the VM Size "Standard_DS1"
info:    The [OS, Data] Disk or image configuration requires storage account
+ Looking up the storage account stoziviw4mlqpsh
info:    Could not find the storage account "stoziviw4mlqpsh", trying to create new one
+ Creating storage account "stoziviw4mlqpsh" in "japaneast"
+ Looking up the storage account stoziviw4mlqpsh
+ Looking up the NIC "n0bi--japan-bybyx2z9haqx-nic"
info:    An nic with given name "n0bi--japan-bybyx2z9haqx-nic" not found, creating a new one
+ Looking up the virtual network "n0bi--japan-bybyx2z9haqx-vnet"
info:    Preparing to create new virtual network and subnet
/ Creating a new virtual network "n0bi--japan-bybyx2z9haqx-vnet" [address prefix: "10.0.0+0/16"] with subnet "n0bi--japan-bybyx2z9haqx-snet" [address prefix: "10.0.1.0/24"]
+ Looking up the virtual network "n0bi--japan-bybyx2z9haqx-vnet"
- Looking up the subnet "n0bi--japan-bybyx2z9haqx-snet" under the virtual network "n0bi--+apan-bybyx2z9haqx-vnet"
info:    Found public ip parameters, trying to setup PublicIP profile
+ Looking up the public ip "n0bi--japan-bybyx2z9haqx-pip"
info:    PublicIP with given name "n0bi--japan-bybyx2z9haqx-pip" not found, creating a new one
+ Creating public ip "n0bi--japan-bybyx2z9haqx-pip"
+ Looking up the public ip "n0bi--japan-bybyx2z9haqx-pip"
+ Creating NIC "n0bi--japan-bybyx2z9haqx-nic"
+ Looking up the NIC "n0bi--japan-bybyx2z9haqx-nic"
+ Looking up the storage account clisto3776169046n0bivm
+ Creating VM "n0bi-vm"
+ Looking up the VM "n0bi-vm"
+ Looking up the NIC "n0bi--japan-bybyx2z9haqx-nic"
+ Looking up the public ip "n0bi--japan-bybyx2z9haqx-pip"
data:    Id                              :/subscriptions/91b020f6-c166-42d6-bebd-0cd88ed38dc0/resourceGroups/n0bisuke-test/providers/Microsoft.Compute/virtualMachines/n0bi-vm
data:    ProvisioningState               :Succeeded
data:    Name                            :n0bi-vm
data:    Location                        :japaneast
data:    Type                            :Microsoft.Compute/virtualMachines
data:
data:    Hardware Profile:
data:      Size                          :Standard_DS1
data:
data:    Storage Profile:
data:      Image reference:
data:        Publisher                   :CoreOS
data:        Offer                       :CoreOS
data:        Sku                         :Stable
data:        Version                     :latest
data:
data:      OS Disk:
data:        OSType                      :Linux
data:        Name                        :cli1ee1e87bc1de6a66-os-1475043625598
data:        Caching                     :ReadWrite
data:        CreateOption                :FromImage
data:        Vhd:
data:          Uri                       :https://stoziviw4mlqpsh.blob.core.windows.net/vhds/cli1ee1e87bc1de6a66-os-1475043625598.vhd
data:
data:    OS Profile:
data:      Computer Name                 :n0bi-vm
data:      User Name                     :n0bisuke
data:      Linux Configuration:
data:        Disable Password Auth       :true
data:
data:    Network Profile:
data:      Network Interfaces:
data:        Network Interface #1:
data:          Primary                   :true
data:          MAC Address               :00-0D-3A-50-9D-98
data:          Provisioning State        :Succeeded
data:          Name                      :n0bi--japan-bybyx2z9haqx-nic
data:          Location                  :japaneast
data:            Public IP address       :13.78.87.17
data:            FQDN                    :n0bi--japan-bybyx2z9haqx-pip.japaneast.cloudapp.azure.com
data:
data:    Diagnostics Profile:
data:      BootDiagnostics Enabled       :true
data:      BootDiagnostics StorageUri    :https://clisto3776169046n0bivm.blob.core.windows.net/
data:
data:      Diagnostics Instance View:
info:    vm quick-create command OK

無事にエラーなく立ち上がりました。

Webのポータルで確認するとしっかりとVMが作成されています。ポータル反映にはすこし時間がかかります。

CLIからやったほうが一瞬でできますね笑
CLI操作に慣れてる人はこっちのほうが便利かと思います。

MacからSSHでAzure VM / CoreOSにログイン

先ほどのログで 13.78.87.17 がこのVMのパブリックIPということがわかりました。SSHログインしてみます。

$ ssh -i ~/.ssh/id_rsa 13.78.87.17

The authenticity of host '13.78.87.17 (13.78.87.17)' can't be established.
ECDSA key fingerprint is SHA256:glcFeWL+ALTdHkugu37jQNw6V31KeRdmlDOmd3AAuFY.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '13.78.87.17' (ECDSA) to the list of known hosts.
CoreOS stable (1122.2.0)
Last login: Wed Sep 28 06:32:59 2016 from 106.161.100.40

n0bisuke@n0bi-vm ~ $

バージョンなど確認

n0bisuke@n0bi-vm ~ $ cat /etc/os-release
NAME=CoreOS
ID=coreos
VERSION=1122.2.0
VERSION_ID=1122.2.0
BUILD_ID=2016-09-06-1449
PRETTY_NAME="CoreOS 1122.2.0 (MoreOS)"
ANSI_COLOR="1;32"
HOME_URL="https://coreos.com/"
BUG_REPORT_URL="https://github.com/coreos/bugs/issues"

ちゃんとCoreOSが入ってますね!

CoreOS上のdockerでUbuntu Webサーバーを立ち上げる

こちらの記事(所要時間わずか10分で「Docker」を体験しよう――Azureで始めるDocker入門 (1/2))を元にWebサーバーを立ち上げるまでをやってみます。

n0bisuke@n0bi-vm ~ $ sudo docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 1.10.3
Storage Driver: overlay
 Backing Filesystem: extfs
Execution Driver: native-0.2
Logging Driver: json-file
Plugins:
 Volume: local
 Network: null host bridge
Kernel Version: 4.7.0-coreos
Operating System: CoreOS 1122.2.0 (MoreOS)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 3.363 GiB
Name: n0bi-vm
ID: QNJH:HASF:H46D:N6QV:JPB3:X7LD:WN3D:YV6Q:JKBT:TXM5:OZXU:2SLG
n0bisuke@n0bi-vm ~ $ sudo docker version
Client:
 Version:      1.10.3
 API version:  1.22
 Go version:   go1.5.4
 Git commit:   1f8f545
 Built:
 OS/Arch:      linux/amd64

Server:
 Version:      1.10.3
 API version:  1.22
 Go version:   go1.5.4
 Git commit:   1f8f545
 Built:
 OS/Arch:      linux/amd64
  • docker上にUbuntu立ち上げ
sudo docker run -t -i ubuntu /bin/bash
  • Ubuntuにwebサーバーインストール (dockerコンテナ)
apt-get update && apt-get install apache2
exit
  • 新しいイメージを作成

コンテナIDを調べます。

sudo docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
c9b283d4667a        ubuntu              "/bin/bash"         5 minutes ago       Exited (127) 4 seconds ago                       adoring_wozniak

僕の場合はc9b283d4667aでした。sudo docker commit コンテナID ユーザー名/イメージ名という形でコミットします。ユーザー名とイメージ名は適当でも大丈夫です。

sudo docker commit c9b283d4667a n0bisuke/azuretest
  • Webサーバー立ち上げ
sudo docker run -d -p 80:80 n0bisuke/azuretest /usr/sbin/apache2ctl -D FOREGROUND

これで立ち上がりました。

ブラウザから http://13.78.87.17 (VMのパブリックIP)にアクセスしてみましょう。

無事にWebサーバー立ち上げまで行けました。

Azure側のネットワーク設定などはしなくとも接続できます。

まとめ

「AWSでEC2インスタンスを立ち上げる」っぽいことをAzureのCLIからやってみました。

やはりCLIのほうがWeb画面を見なくてですしマウス操作がいらないのでかなり早い操作で使うことができます。

コマンドラインなので色々と自動化もやりやすそうですし、CLI操作のほうが慣れてる人はこちらをお勧めです。

ひとこと

CoreOSってびっくりするくらい薄いですね

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5