Last-modified: 2019-07-14 (日) 17:29:03
Azure/チートシート

インストール・アップデート

インストール

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
-
|
!
-
!
 
-
!
-
!
 
-
!
 
 
-
!
# 以前はWebPIでインストールしていたが、2016年あたりからPowerShellGetでインストールするようになった
# WebPIで入れていた環境に入れる場合は、先にそれのアンインストールが必要
 
# PowerShellGetが入っていることを確認
Get-Module PowerShellGet -list | Select-Object Name,Version,Path
 
# ASM
Install-Module Azure
# ARM
Install-Module AzureRM
 
# 明示的なインポート(しなくとも、Azure*モジュール内の関数を呼び出したタイミングでインポートしているような挙動に見える)
Import-Module Azure
Import-Module AzureRM
 
# 確認
Get-Module

アカウント

認証ダイアログでログイン

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
-
!
-
!
# ASM
Add-AzureAccount
# ARM
Add-AzureRmAccount


クライアント証明書でログイン(ASM)

  1. アカウント作成 or 招待
    旧ポータル-[設定]-[管理者]-[追加]にて、共同管理者としてアカウントを作成 or 招待する
    既存のアカウントに対してクライアント証明書を紐づける場合は不要
  2. クライアント証明書を作成
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
    
     
     
     
     
    -
    !
     
     
     
     
     
     
     
     
     
    
    $subject="CN=<紐づけたいアカウント名と同一>"
    $expiryDate = (Get-Date).AddYears(100)
    $outputFilePath=".\out.cer"
     
    # -KeyExportPolicyを、ExportableEncryptedやExportableとすれば、秘密鍵をエクスポートできるようになる
    $cert = New-SelfSignedCertificate `
        -Subject "${subject}" `
        -KeyAlgorithm RSA `
        -KeyLength 4096 `
        -HashAlgorithm sha256 `
        -NotAfter ${expiryDate} `
        -CertStoreLocation "Cert:\CurrentUser\My" `
        -KeyExportPolicy NonExportable `
        -Provider "Microsoft Enhanced Cryptographic Provider v1.0"
    Export-Certificate -cert $cert -FilePath "${outputFilePath}"
  3. アカウントにクライアント証明書を紐づける
    旧ポータル-[設定]-[管理証明書]-[アップロード]で、作成したout.cerをアップロード
  4. 動作確認
    1. まず、完全なログアウト状態を作る
      Everything is expanded.Everything is shortened.
        1
        2
      
      -
      !
      
      # PowerShell端末をすべて終了し、下記パスのAzureProfile.json, TokenCache.datを消す
      C:\Users\<ユーザ名>\AppData\Roaming\Windows Azure Powershell
    2. ログイン
      Everything is expanded.Everything is shortened.
        1
        2
        3
        4
        5
        6
        7
        8
        9
       10
      
       
       
       
       
       
       
       
       
       
       
      
      $fingerPrint = <クライアント証明書のフィンガープリント>
      $subscriptionName = <サブスクリプション名>
      $subscriptionID = <サブスクリプションID>
       
      $cert = Get-ChildItem Cert:\CurrentUser\My\${fingerPrint}
      Set-AzureSubscription `
          -SubscriptionName "${subscriptionName}" `
          -SubscriptionId "${subscriptionID}" `
          -Certificate $cert
      Select-AzureSubscription -SubscriptionName "${subscriptionName}"


クライアント証明書でログイン(ARM)

  1. AADを操作できるアカウントでログイン(ARM)
    Everything is expanded.Everything is shortened.
      1
    
     
    
    Add-AzureRmAccount
  2. AADへサービスプリンパシルを登録
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
    
     
     
     
    -
    !
     
    -
    |
    !
     
     
     
     
     
     
     
     
     
     
    -
    !
     
     
     
     
     
     
    -
    !
     
     
    
    $displayName = "exampleapp"
    $subject = "CN=${displayName}"
    $expiryDate = (Get-Date).AddYears(100)
    # ロールに関しては、項目ロールにて説明している
    $role = "Contributor"
     
    # クライアント証明書を証明書ストアに作成
    # -KeyExportPolicyを、ExportableEncryptedやExportableとすれば、秘密鍵をエクスポートできるようになる
    $cert = New-SelfSignedCertificate `
        -Subject "${subject}" `
        -KeyAlgorithm RSA `
        -KeyLength 4096 `
        -HashAlgorithm sha256 `
        -NotAfter ${expiryDate} `
        -CertStoreLocation "Cert:\CurrentUser\My" `
        -KeyExportPolicy NonExportable `
        -Provider "Microsoft Enhanced Cryptographic Provider v1.0"
    $certValue = [System.Convert]::ToBase64String($cert.GetRawCertData())
     
    # サービスプリンパシルを登録
    $sp = New-AzureRMADServicePrincipal `
        -DisplayName ${displayName} `
        -CertValue ${certValue} `
        -EndDate $cert.NotAfter `
        -StartDate $cert.NotBefore
    Sleep 20
     
    # 権限設定
    New-AzureRmRoleAssignment `
        -RoleDefinitionName ${role} `
        -ServicePrincipalName $sp.ApplicationId
  3. ログインに必要な情報の取得
    Everything is expanded.Everything is shortened.
      1
      2
      3
      4
      5
      6
      7
      8
    
    -
    !
     
    -
    !
     
    -
    !
    
    # テナントID
     (Get-AzureRmSubscription -SubscriptionName <サブスクリプション名>).TenantId
     
    # アプリケーションID
    $sp.ApplicationId
     
    # 登録したクライアント証明書のフィンガープリント
    $cert.Thumbprint
  4. 動作確認
    1. まず、完全なログアウト状態を作る
      Everything is expanded.Everything is shortened.
        1
        2
      
      -
      !
      
      # PowerShell端末をすべて終了し、下記パスのAzureProfile.json, TokenCache.datを消す
      C:\Users\<ユーザ名>\AppData\Roaming\Windows Azure Powershell
    2. ログイン
      Everything is expanded.Everything is shortened.
        1
        2
        3
        4
        5
        6
        7
        8
        9
      
       
       
       
       
       
       
       
       
       
      
      $tenantID = <テナントID>
      $applicationID = <アプリケーションID>
      $fingerPrint = <クライアント証明書のフィンガープリント>
       
      Login-AzureRmAccount `
          -ServicePrincipal `
          -TenantId $tenantID `
          -ApplicationId $applicationID `
          -CertificateThumbprint $fingerPrint

サブスクリプション

サブスクリプションID, テナントID取得

Everything is expanded.Everything is shortened.
  1
 
Get-AzureRmSubscription

サブスクリプションが切り替わらない、消えない、増えない、挙動がおかしい

Everything is expanded.Everything is shortened.
  1
  2
  3
-
!
-
# PowerShell端末をすべて終了し、下記パスのAzureProfile.json, TokenCache.datを消す
C:\Users\<ユーザ名>\AppData\Roaming\Windows Azure Powershell
# PowerShellを起動し、ログイン

ロール(ARM)

ロール一覧取得

Everything is expanded.Everything is shortened.
  1
 
Get-AzureRmRoleDefinition | FT Name, Description

ロールの権限確認

Everything is expanded.Everything is shortened.
  1
  2
 
 
Get-AzureRmRoleDefinition <ロール名> | FL Actions, NotActions
(Get-AzureRmRoleDefinition <ロール名>).Actions

権限一覧取得

Everything is expanded.Everything is shortened.
  1
 
Get-AzureRMProviderOperation * | Out-GridView

ユーザーの権限確認

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
-
!
 
-
!
 
-
!
 
-
!
# 一覧
Get-AzureRmRoleAssignment
 
# 一覧(ASMの権限情報も表示)
Get-AzureRmRoleAssignment -IncludeClassicAdministrators
 
# ユーザー
Get-AzureRmRoleAssignment -SingInName <アカウント名>
 
# アプリ
Get-AzureRmRoleAssignment -ServicePrincipalName <アプリケーションID>

リソースに対するユーザーの権限表示

Everything is expanded.Everything is shortened.
  1
 
Get-AzureRmRoleAssignment -ResourceGroupName <リソースグループ名> | FL DisplayName, RoleDefinitionName, Scope

カスタムロールの作成

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
-
!
-
|
!
 
-
!
 
 
 
-
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
!
 
 
-
!
# 要 AADを操作できるアカウントでログインしていること
 
# 例
# ベースとなるロールを選択
$role = Get-AzureRmRoleDefinition "Reader"
 
# IDを消し、ロールの説明を上書き
$role.id = $null
$role.name = "foo"
$role.description = "bar"
 
# 好きなように権限を修正
$role.Actions.Add("Microsoft.Authorization/*/read")
$role.Actions.Add("Microsoft.ClassicCompute/domainNames/*")
$role.Actions.Add("Microsoft.ClassicCompute/virtualMachines/*")
$role.Actions.Add("Microsoft.ClassicNetwork/reservedIps/link/action")
$role.Actions.Add("Microsoft.ClassicNetwork/reservedIps/read")
$role.Actions.Add("Microsoft.ClassicNetwork/virtualNetworks/join/action")
$role.Actions.Add("Microsoft.ClassicNetwork/virtualNetworks/read")
$role.Actions.Add("Microsoft.ClassicStorage/storageAccounts/disks/read")
$role.Actions.Add("Microsoft.ClassicStorage/storageAccounts/images/read")
$role.Actions.Add("Microsoft.ClassicStorage/storageAccounts/listKeys/action")
$role.Actions.Add("Microsoft.ClassicStorage/storageAccounts/read")
$role.Actions.Add("Microsoft.Insights/alertRules/*")
$role.Actions.Add("Microsoft.ResourceHealth/availabilityStatuses/read")
$role.Actions.Add("Microsoft.Resources/deployments/*")
$role.Actions.Add("Microsoft.Resources/subscriptions/resourceGroups/read")
$role.Actions.Add("Microsoft.Support/*")
 
# スコープの設定
$role.AssignableScopes.Clear()
$role.AssignableScopes.Add("/subscriptions/<サブスクリプションID>")
 
# 新しいロールとして登録
New-AzureRmRoleDefinition -Role $role

リソース

リソース一覧取得

Everything is expanded.Everything is shortened.
  1
  2
 
 
Select-AzureRmSubscription -SubscriptionId <サブスクリプションID>
Get-AzureRmResource | Out-GridView

LogAnalytics(旧OMS)

LogAnalyticsのメリットは、GCP Stackdriver(GCP/AWS)や、AWS CloudWatch(AWS)と違い、どんなクラウド端末でもオンプレ端末でも監視可能という点。
可視化も統合されており、GCPのように別リソースで見るということがない。またStackdriver MonitoringやGoogle DataPortalより遥かに柔軟。
Stackdriver Loggingは長期ログ保存ができず、普通はBigQueryに流すことになるがBigQueryはクエリ実行にコストがかかる上、クエリ構文が冗長かつ検索速度自体もLogAnalyticsより遅い。
デメリットはインジェストコストはStackdriverより高い点と、データのエクスポートが不自由な点。

インストール

  1. Azureポータルを開く
  2. LogAnalyticsの該当リソースを開く
  3. [詳細設定]-[Connected Sources]-[Linux Servers]
  4. [LINUX 用エージェントのインストールとオンボード]のコマンドレットをコピー
  5. 監視したいインスタンス上で、上記コマンドレットを実行

アンインストール

Everything is expanded.Everything is shortened.
  1
 
sudo sh ./onboard_agent.sh --purge

設定

Everything is expanded.Everything is shortened.
  1
  2
-
!
# 収集設定強制取得
sudo su omsagent -c 'python /opt/microsoft/omsconfig/Scripts/PerformRequiredConfigurationChecks.py'

収集設定の個別設定

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
-
!
 
-
!
 
 
 
 
-
!
 
-
|
!
-
!
 
 
 
 
 
 
 
 
 
 
 
-
!
 
# 収集設定の集中管理無効 (通常は無効化するメリットはない)
sudo /opt/microsoft/omsconfig/Scripts/OMS_MetaConfigHelper.py --disable
 
# 自前でsyslog転送設定を書く場合
vi /etc/rsyslog.d/95-omsagent.conf
*.* @127.0.0.1:25224
 
systemctl restart rsyslog
 
# 自前でパフォーマンス転送設定を書く場合
vi /etc/opt/microsoft/omsagent/conf/omsagent.conf
 
# 自前でカスタムログ転送設定を書く場合
# OMS agentはfluentdラッパーなので、fluetndの設定と同じ感覚で書く
vi /etc/opt/microsoft/omsagent/<ワークスペースID>/conf/omsagent.d/customlog.conf
# e.g. audit.log収集
<source>
  type sudo_tail
  path /var/log/audit/audit.log
  pos_file <posファイルパス(e.g. /var/opt/microsoft/omsagent/state/CUSTOM_LOG_BLOB.audit_CL_foo.pos)>
  read_from_head false
  run_interval 60
  tag <タグ(e.g. oms.blob.CustomLog.CUSTOM_LOG_BLOB.audit_CL_foo.*)>
  format none
</source>
 
chown omsagent:omiusers /etc/opt/microsoft/omsagent/<ワークスペースID>/conf/omsagent.d/customlog.conf
 
# 反映
/opt/microsoft/omsagent/bin/service_control restart
/opt/omi/bin/service_control restart

その他

バージョンの確認

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
-
!
 
 
 
-
!
# 新(PowerShellGet経由で入れた場合)
Get-Module
Get-Module Azure -list | Select-Object Name,Version,Path
Get-Module AzureRM -list | Select-Object Name,Version,Path
 
# 旧(WebPI経由で入れた場合)
Get-Module -ListAvailable -Name Azure -Refresh