티스토리 뷰

Azure 기타

Azure Powershell, Azure CLI 사용

zerobig-k8s 2024. 3. 11. 08:10

지난 Part1, Part2에 이어 계속 진행합니다.

 

 

이제 설치된 도구를 활용해 보도록 하자. PowerShell과 Azure CLI를 활용하는 내용을 살펴 보겠다.

이전 실습에서 생성하여 중지해 둔 VM을 시작하고 접속한다.

 

 

 

 

참조 :

https://learn.microsoft.com/ko-kr/training/modules/configure-azure-resources-tools/4-use-azure-powershell

https://learn.microsoft.com/ko-kr/training/modules/configure-azure-resources-tools/6-use-azure-cli

 

 

 

 

Azure Powershell 사용

 

먼저 Azure PowerShell을 사용하여 Azure 구독에 리소스를 만들어 보자.

Azure PowerShell은 Windows PowerShell 또는 PowerShell Core에 추가하여 Azure 구독에 연결하고 리소스를 관리할 수 있는 모듈이다.

Az는 Azure 기능을 사용하는 cmdlet이 포함된 Azure PowerShell 모듈의 정식 이름이다. 여기에는 모든 Azure 리소스의 거의 모든 측면을 제어할 수 있는 수백 개의 cmdlet이 포함되어 있다. 다음을 비롯한 기능을 사용할 수 있다.

  • 리소스 그룹
  • 스토리지
  • VM
  • Microsoft Entra ID
  • 컨테이너
  • 기계 학습

이 모듈은 GitHub에서 사용할 수 있는 오픈 소스 구성 요소입니다.

 

 

 

예제: Azure PowerShell을 사용하여 리소스 그룹을 만드는 방법

Azure 모듈을 설치하면 Azure로 작업을 시작할 수 있다. 리소스 그룹을 만들어 보자. 새 리소스 그룹을 만드는 것은 새 Azure 솔루션을 시작할 때 수행할 첫 번째 작업 중 하나이다.

다음 4 단계를 수행해야 한다.

  1. Azure cmdlet을 가져온다.
  2. Azure 구독에 연결한다.
  3. 리소스 그룹을 만든다.
  4. 생성이 완료되었는지 확인한다.

다음 그림은 이러한 단계의 개요를 보여준다.

 

 

Azure cmdlet 가져오기

PowerShell 3.0부터 모듈 내에서 cmdlet을 사용하면 모듈이 자동으로 로드된다. 기본 모듈 자동 로드 설정을 변경하지 않는 한 더 이상 PowerShell 모듈을 수동으로 가져올 필요가 없다.

 

연결하기

Azure PowerShell의 로컬 설치를 사용하는 경우 Azure 명령을 실행하기 전에 먼저 인증해야 한다. 새로 Terminal 창을 띄우고 Connect-AzAccount 명령을 수행한다.

Connect-AzAccount

 

Sign in 창이 뜨면 로그인을 시도한다.

 

로그인이 이루어 지면 브라우저에 다음과 같이 인증 성공 안내 메시지가 출력 된다. 확인 후 창을 닫는다.


 

터미널 창을 확인해 보면 다음과 같이 접속이 이루어진 구독 결과가 전시된다.

 
 

만들기

만들기에 앞서 Azure를 처음 사용하는 경우 대부분은 단일 구독만 보유할 것이지만, 필자와 같이 여러 구독을 보유한 경우도 있을 것이며 이런 경우 특정 구독을 대상으로 명령을 실행하도록 Azure PowerShell을 구성할 수 있다.

하나의 구독만 보유할 경우 다음 내용은 참고만 한다.

전체 구독 목록을 가져와 특정 구독을 선택해보자.

1. Get-AzSubscription 명령을 사용하여 계정에 모든 구독 이름 목록을 가져온다.

Get-AzSubscription

 

PS C:\Users\zerobig> Get-AzSubscription


   TenantId: 067db808-c3ee-4430-8135-05f15d0d242f

Name          Id                                   State
----          --                                   -----
VSE_TDG - MPN 37ac1ac0-fcd4-41ba-b002-e7ba3274e34d Enabled

WARNING: You're using Az.Accounts version 2.15.1. The latest version of Az.Accounts is 2.16.0. Upgrade your Az modules
using the following commands:
  Update-Module Az.* -WhatIf    -- Simulate updating your Az modules.
  Update-Module Az.*            -- Update your Az modules.

   TenantId: 917bfe84-0ca6-488d-ad3a-236e41ceafe9

Name     Id                                   State
----     --                                   -----
AzureMVP 1199b626-xxxx-xxxx-xxxx-xxxxxxxxxxxxx Enabled
VSE-MPN  98483591-xxxx-xxxx-xxxx-xxxxxxxxxxxxx Enabled

 

2. 다음과 같이 Set-AzContext명령과 구독 정보를 전달하여 대상 구독을 선택한다.

Set-AzContext -Subscription '00000000-0000-0000-0000-000000000000'
PS C:\Users\zerobig> Set-AzContext -Subscription "1199b626-xxxx-xxxx-xxxx-xxxxxxxxxxxxx"


   Tenant: 기본 디렉터리 (917bfe84-0ca6-488d-ad3a-236e41ceafe9)

SubscriptionName SubscriptionId                       Account               Environment
---------------- --------------                       -------               -----------
AzureMVP         1199b626-xxxx-xxxx-xxxx-xxxxxxxxxxxxx zerobig.kim@gmail.com AzureCloud


PS C:\Users\zerobig>

 

 

모든 리소스 그룹 목록 가져오기

활성 구독에서 모든 리소스 그룹 목록을 검색할 수 있다.

Get-AzResourceGroup
PS C:\Users\zerobig> Get-AzResourceGroup


ResourceGroupName : RG-AOI
Location          : canadaeast
ProvisioningState : Succeeded
Tags              :
ResourceId        : /subscriptions/1199b626-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/RG-AOI

ResourceGroupName : zerobigdnsrg
Location          : westus
ProvisioningState : Succeeded
Tags              :
ResourceId        : /subscriptions/1199b626-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/zerobigdnsrg

ResourceGroupName : cloud-shell-storage-southeastasia
Location          : southeastasia
ProvisioningState : Succeeded
Tags              :
ResourceId        : /subscriptions/1199b626-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/cloud-shell-storage-southeastasia

ResourceGroupName : DefaultResourceGroup-SE
Location          : koreacentral
ProvisioningState : Succeeded
Tags              :
ResourceId        : /subscriptions/1199b626-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/DefaultResourceGroup-SE

<이하 생략>

 

 

보다 간결하게 표시하려면 파이프 '|'를 사용하여 Get-AzResourceGroup의 출력을 Format-Table cmdlet으로 보낼 수 있다.

Get-AzResourceGroup | Format-Table
PS C:\Users\zerobig> Get-AzResourceGroup | Format-Table

ResourceGroupName                                      Location      ProvisioningState Tags                                               TagsTable
-----------------                                      --------      ----------------- ----                                               ---------
RG-AOI                                                 canadaeast    Succeeded
zerobigdnsrg                                           westus        Succeeded
cloud-shell-storage-southeastasia                      southeastasia Succeeded
DefaultResourceGroup-SE                                koreacentral  Succeeded

<이하 생략>

 

 

리소스 그룹 만들기

New-AzResourceGroup 명령을 사용하여 리소스 그룹을 만들 수 있다. 이 때 이름 및 위치를 지정해야 한다. 이름은 구독 내에서 고유해야 한다.

New-AzResourceGroup -Name <name> -Location <location>
PS C:\Users\zerobig> New-AzResourceGroup -Name RG-PSDemo -Location KoreaCentral


ResourceGroupName : RG-PSDemo
Location          : koreacentral
ProvisioningState : Succeeded
Tags              :
ResourceId        : /subscriptions/1199b626-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/RG-PSDemo



PS C:\Users\zerobig>

 

 

리소스 확인

다음과 같이 Get-AzResourceGroup 명령을 수행하여 리소스 그룹 생성이 성공적이었는지 확인한다.

Get-AzResourceGroup -Name <name>
PS C:\Users\zerobig> Get-AzResourceGroup -Name "RG-PSDemo"


ResourceGroupName : RG-PSDemo
Location          : koreacentral
ProvisioningState : Succeeded
Tags              :
ResourceId        : /subscriptions/1199b626-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/RG-PSDemo



PS C:\Users\zerobig>

 

 

리소스 제거

결과가 확인 되었으면 Get-AzResourceGroup 명령을 수행하여 생성했던 리소스 그룹을 제거한다.

Remove-AzResourceGroup -Name <name>
PS C:\Users\zerobig> Remove-AzResourceGroup -Name RG-PSDemo

Confirm
Are you sure you want to remove resource group 'RG-PSDemo'
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y
True
PS C:\Users\zerobig>\

 

 

 

 

Azure CLI 사용

 

Azure CLI를 사용하면 모든 Azure 리소스의 거의 모든 측면을 제어할 수 있다. 리소스 그룹, 스토리지, 가상 머신, Microsoft Entra ID, 컨테이너, 기계 학습 등을 사용할 수 있다.

CLI의 명령은 그룹 및 하위 그룹으로 구조화된다. 각 그룹은 Azure에서 제공하는 서비스를 나타내며, 하위 그룹은 이러한 서비스에 대한 명령을 논리적 그룹으로 나눈다. 예를 들어 storage 그룹에는 account, blob, sharequeue 등의 하위 그룹이 있다.

필요한 특정 명령을 찾는 방법 중 한 가지 방법은 Azure 설명서를 사용하여 명령, CLI 등에 대해 자세히 설명하는 AI 로봇인 az find를 사용하는 것이다.

먼저 Azure CLI 실습을 진행하기에 앞서 “wsl” 명령을 수행하여 앞서 구성한 WSL (리눅스 환경)으로 전환한다. 물론 현재 작업해오던 윈도우즈 터미널 상에서도 az 명령이 지원되지만 자동 완성 미지원 등의 사유로 필자는 wsl로 전환하여 작업하는 것을 선호한다.
PS C:\Users\zerobig> wsl
Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 5.15.146.1-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

 * Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
   just raised the bar for easy, resilient and secure K8s cluster deployment.

   https://ubuntu.com/engage/secure-kubernetes-at-the-edge

This message is shown once a day. To disable it please create the
/home/zerobig/.hushlogin file.
zerobig@win11vm-sa:/mnt/c/Users/zerobig$​

 

 

예를 들어 스토리지 Blob을 관리하는 데 도움이 되는 명령을 찾으려면 find 명령을 사용한다.

az find blob
zerobig@win11vm-sa:/mnt/c/Users/zerobig$ az find blob
Finding examples...

Here are the most common ways to use [blob]:

Restore blobs in the specified blob range.
time=`date -u -d "30 minutes" '+%Y-%m-%dT%H:%MZ'`
az storage blob restore --account-name mystorageaccount -g MyResourceGroup -t $time -r container0/blob1 container0/blob2

Download a blob to a file path. (autogenerated)
az storage blob download --account-name mystorageaccount --auth-mode login --container-name mycontainer --file /path/to/file --name MyBlob

Restore blobs in two specified blob ranges. For examples, (container1/blob1, container2/blob2) and (container2/blob3..container2/blob4).
az storage blob restore --account-name mystorageaccount --resource-group MyResourceGroup --time-to-restore 2020-02-27T03:59:59Z --blob-range container1/blob1 container2/blob2 --blob-range container2/blob3 container2/blob4

You have 2 update(s) available. Consider updating your CLI installation with 'az upgrade'
zerobig@win11vm-sa:/mnt/c/Users/zerobig$

 

 

 

예제: Azure CLI를 사용하여 리소스 그룹을 만드는 방법

새 Azure 리소스를 만드는 3단계는 일반적으로 Azure 구독에 연결하고, 리소스를 만들고, 리소스가 제대로 만들어졌는지 확인하는 것이다.

 

 

연결하기

Azure PowerShell과 마찬가지로 Azure CLI login 명령을 사용하여 Azure 명령을 실행하기 전에 먼저 인증해야 한다.

az login
zerobig@win11vm-sa:/mnt/c/Users/zerobig$ az login
A web browser has been opened at https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize. Please continue the login in the web browser. If no 
web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`.
gio: https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize?client_id=04b07795-8ddb-461a-bbee-02f9e1bf7b46&response_type=code&redirect_uri=ht
tp%3A%2F%2Flocalhost%3A33813&scope=https%3A%2F%2Fmanagement.core.windows.net%2F%2F.default+offline_access+openid+profile&state=sRSeJQHEgLDIazxm&code_challen
ge=SzKDxFKRbItF4hlK-GNiaQFvTL1CRTRjFtvY8Fwqt8o&code_challenge_method=S256&nonce=72313ffce37bb860cae9b28ef5b4b486ec1a227d6550fb9395490f989e849085&client_info
=1&claims=%7B%22access_token%22%3A+%7B%22xms_cc%22%3A+%7B%22values%22%3A+%5B%22CP1%22%5D%7D%7D%7D&prompt=select_account: Operation not supported

 

az login 명령 수행 결과 리턴된 링크(gio: https://login.microsoftonline.com….)를 클릭하여 기본 브라우저를 띄우고 Azure 로그인을 시도한다.

로그인이 이루어 지면 브라우저에 다음과 같이 인증 성공 안내 메시지가 출력 된다. 확인 후 창을 닫는다.

 

터미널 창을 확인해 보면 다음과 같이 접속이 이루어진 구독 결과가 전시된다.

zerobig@win11vm-sa:/mnt/c/Users/zerobig$ az login
<이상 생략>
  {
    "cloudName": "AzureCloud",
    "homeTenantId": "917bfe84-0ca6-488d-ad3a-236e41ceafe9",
    "id": "1199b626-xxxx-xxxx-xxxx-xxxxxxxxxxxxx",
    "isDefault": false,
    "managedByTenants": [],
    "name": "AzureMVP",
    "state": "Enabled",
    "tenantId": "917bfe84-0ca6-488d-ad3a-236e41ceafe9",
    "user": {
      "name": "zerobig.kim@gmail.com",
      "type": "user"
    }
  },
  {
    "cloudName": "AzureCloud",
    "homeTenantId": "917bfe84-0ca6-488d-ad3a-236e41ceafe9",
    "id": "98483591-af84-4569-ba3d-0711ced77b32",
    "isDefault": false,
    "managedByTenants": [],
    "name": "VSE-MPN",
    "state": "Enabled",
    "tenantId": "917bfe84-0ca6-488d-ad3a-236e41ceafe9",
    "user": {
      "name": "zerobig.kim@gmail.com",
      "type": "user"
    }
  }
]
zerobig@win11vm-sa:/mnt/c/Users/zerobig$

 

 

만들기

만들기에 앞서 Azure를 처음 사용하는 경우 대부분은 단일 구독만 보유할 것이지만, 필자와 같이 여러 구독을 보유한 경우도 있을 것이며 이런 경우 특정 구독을 대상으로 명령을 실행하도록 Azure CLI를 구성할 수 있다.

하나의 구독만 보유할 경우 다음 내용은 참고만 한다.

전체 구독 목록을 가져와 특정 구독을 선택해보자.

1. az account show 명령을 사용하여 현재 선택된 구독을 확인한다.

az account show
zerobig@win11vm-sa:/mnt/c/Users/zerobig$ az account show
{
  "environmentName": "AzureCloud",
  "homeTenantId": "067db808-c3ee-4430-8135-05f15d0d242f",
  "id": "37ac1ac0-fcd4-41ba-b002-e7ba3274e34d",
  "isDefault": true,
  "managedByTenants": [],
  "name": "VSE_TDG - MPN",
  "state": "Enabled",
  "tenantId": "067db808-c3ee-4430-8135-05f15d0d242f",
  "user": {
    "name": "zerobig.kim@gmail.com",
    "type": "user"
  }
}
zerobig@win11vm-sa:/mnt/c/Users/zerobig$

 

2. 다음과 같이 az account set명령과 구독 정보를 전달하여 대상 구독을 선택한다.

az account set -s <Subscription Id>
zerobig@win11vm-sa:/mnt/c/Users/zerobig$ az account set -s 1199b626-xxxx-xxxx-xxxx-xxxxxxxxxxxxx
zerobig@win11vm-sa:/mnt/c/Users/zerobig$

 

3. az account show 명령을 다시 수행하여 현재 선택된 구독을 확인한다.

az account show
zerobig@win11vm-sa:/mnt/c/Users/zerobig$ az account show
{
  "environmentName": "AzureCloud",
  "homeTenantId": "917bfe84-0ca6-488d-ad3a-236e41ceafe9",
  "id": "1199b626-xxxx-xxxx-xxxx-xxxxxxxxxxxxx",
  "isDefault": true,
  "managedByTenants": [],
  "name": "AzureMVP",
  "state": "Enabled",
  "tenantId": "917bfe84-0ca6-488d-ad3a-236e41ceafe9",
  "user": {
    "name": "zerobig.kim@gmail.com",
    "type": "user"
  }
}

 

 

모든 리소스 그룹 목록 가져오기

활성 구독에서 모든 리소스 그룹 목록을 검색할 수 있다.

az group list
zerobig@win11vm-sa:/mnt/c/Users/zerobig$ az group list
[
  {
    "id": "/subscriptions/1199b626-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/RG-AOI",
    "location": "canadaeast",
    "managedBy": null,
    "name": "RG-AOI",
    "properties": {
      "provisioningState": "Succeeded"
    },
    "tags": null,
    "type": "Microsoft.Resources/resourceGroups"
  },
  {
    "id": "/subscriptions/1199b626-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/zerobigdnsrg",
    "location": "westus",
    "managedBy": null,
    "name": "zerobigdnsrg",
    "properties": {
      "provisioningState": "Succeeded"
    },
    "tags": null,
    "type": "Microsoft.Resources/resourceGroups"
  },
  
<이하 생략>

 

보다 간결하게 표시하려면 --output table 옵션을 추가한다.

az group list -o table
zerobig@win11vm-sa:/mnt/c/Users/zerobig$ az group list -o table
Name                                                    Location       Status
------------------------------------------------------  -------------  ---------
RG-AOI                                                  canadaeast     Succeeded
zerobigdnsrg                                            westus         Succeeded
cloud-shell-storage-southeastasia                       southeastasia  Succeeded
DefaultResourceGroup-SE                                 koreacentral   Succeeded                               koreacentral  Succeeded

<이하 생략>

 

 

리소스 그룹 만들기

az group create 명령을 사용하여 리소스 그룹을 만들 수 있다. 이 때 이름 및 위치를 지정해야 한다. 이름은 구독 내에서 고유해야 한다.

az group create --name <name> --location <location>
zerobig@win11vm-sa:/mnt/c/Users/zerobig$ az group create --name RG-AzCLIDemo --location koreacentral
{
  "id": "/subscriptions/1199b626-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/RG-AzCLIDemo",
  "location": "koreacentral",
  "managedBy": null,
  "name": "RG-AzCLIDemo",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": "Microsoft.Resources/resourceGroups"
}
zerobig@win11vm-sa:/mnt/c/Users/zerobig$

 

 

리소스 확인

다음과 같이 az group show 명령을 수행하여 리소스 그룹 생성이 성공적이었는지 확인한다.

az group show --name <name>
zerobig@win11vm-sa:/mnt/c/Users/zerobig$ az group show -n RG-AzCLIDemo
{
  "id": "/subscriptions/1199b626-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/RG-AzCLIDemo",
  "location": "koreacentral",
  "managedBy": null,
  "name": "RG-AzCLIDemo",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": "Microsoft.Resources/resourceGroups"
}
zerobig@win11vm-sa:/mnt/c/Users/zerobig$

 

 

리소스 제거

결과가 확인 되었으면 az group delete 명령을 수행하여 생성했던 리소스 그룹을 제거한다.

az group delete --name <name>
zerobig@win11vm-sa:/mnt/c/Users/zerobig$ az group delete -n RG-AzCLIDemo
Are you sure you want to perform this operation? (y/n): y
zerobig@win11vm-sa:/mnt/c/Users/zerobig$

 

이제 모든 실습이 마무리 되었다.

 

결과가 확인 되었으면 필히 VM을 중지하여 원치 않는 비용이 발생하지 않도록 한다.
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/04   »
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
글 보관함