Azure: работа с DNS из Azure CLI

Автор: | 03/26/2016
 

azure_logoЗадача: добавить зону для домена на DNS от 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 CBJ9C6EZ4 to authenticate.
-info:    Added subscription Free Trial
info:    Added subscription Pay-As-You-Go
info:    Setting subscription "Free Trial" as default
+
info:    login command OK

Переключаемся в ARM-режим:

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

Т.к. в моем аккаунте подключено несколько подписок — выбираем необходимую:

$ azure account list
info:    Executing command account list
data:    Name           Id                                    Current  State
data:    -------------  ------------------------------------  -------  --------
data:    Free Trial     97214f99-***-715556cd5906  true     Disabled
data:    Pay-As-You-Go  0a4f2b9c-***-40b17ef8c3ab  false    Enabled

Переключаемся на Pay-As-You-Go:

$ azure account set -v 0a4f2b9c-***-40b17ef8c3ab
info:    Executing command account set
info:    Setting subscription to "Pay-As-You-Go" with id "0a4f2b9c-***-40b17ef8c3ab".
info:    Changes saved
info:    account set command OK

Проверяем:

$ azure account list
info:    Executing command account list
data:    Name           Id                                    Current  State
data:    -------------  ------------------------------------  -------  --------
data:    Free Trial     97214f99-***-715556cd5906  false    Disabled
data:    Pay-As-You-Go  0a4f2b9c-***-40b17ef8c3ab  true     Enabled
info:    account list command OK

Далее для проекта требуется создать новую группу ресурсов. Хотя для DNS локация не имеет значения — но все ресурсы проекта будут в Европейской зоне.

Найти все доступные локации можно так:

$ azure location list
info:    Executing command location list
warn:    The "location list" commands is changed to list subscription's locations. For old information, use "provider list or show" commands.
info:    Getting locations...
data:    Name                Display Name         Latitude  Longitude
data:    ------------------  -------------------  --------  ---------
data:    eastasia            East Asia            22.267    114.188
data:    southeastasia       Southeast Asia       1.283     103.833
data:    centralus           Central US           41.5908   -93.6208
data:    eastus              East US              37.3719   -79.8164
data:    eastus2             East US 2            36.6681   -78.3889
data:    westus              West US              37.783    -122.417
data:    northcentralus      North Central US     41.8819   -87.6278
data:    southcentralus      South Central US     29.4167   -98.5
data:    northeurope         North Europe         53.3478   -6.2597
data:    westeurope          West Europe          52.3667   4.9
data:    japanwest           Japan West           34.6939   135.5022
data:    japaneast           Japan East           35.68     139.77
data:    brazilsouth         Brazil South         -23.55    -46.633
data:    australiaeast       Australia East       -33.86    151.2094
data:    australiasoutheast  Australia Southeast  -37.8136  144.9631
data:    southindia          South India          12.9822   80.1636
data:    centralindia        Central India        18.5822   73.9197
data:    westindia           West India           19.088    72.868

Создаем группу с именем DEV_init в локации West Europe:

$ azure group create -n DEV_init --location westeurope
info:    Executing command group create
+ Getting resource group DEV_init
+ Creating resource group DEV_init
info:    Created resource group DEV_init
data:    Id:                  /subscriptions/0a4f2b9c-***-40b17ef8c3ab/resourceGroups/DEV_init
data:    Name:                DEV_init
data:    Location:            westeurope
data:    Provisioning State:  Succeeded
data:    Tags: null
data:
info:    group create command OK

Проверяем:

azure_dns_jm_1

$ azure group list
info:    Executing command group list
+ Listing resource groups
data:    Name       Location    Provisioning State  Tags:
data:    ---------  ----------  ------------------  -----
data:    DEV_init   westeurope  Succeeded           null
data:    europe-jm  westeurope  Succeeded           null

Добавление зоны

Проверяем имеющиеся зоны в DNS:

$ azure network dns zone list DEV_init
info:    Executing command network dns zone list
+ Getting the dns zones
warn:    No dns zones found
info:    network dns zone list command OK

Для дальнейшей работы требуется зарегистрировать текущую подписку в провайдере Microsoft.Network:

$ azure provider register --namespace Microsoft.Network
info:    Executing command provider register
+ Registering provider Microsoft.Network with subscription 0a4f2b9c-***-40b17ef8c3ab
info:    provider register command OK

Теперь можно добавить зону. Добавляем домен domain.com:

$ azure network dns zone create -n domain.com -g DEV_init
info:    Executing command network dns zone create
+ Creating dns zone "domain.com"
+ Looking up the dns zone "domain.com"
data:    Id                              : /subscriptions/0a4f2b9c-***-40b17ef8c3ab/resourceGroups/dev_init/providers/Microsoft.Network/dnszones/domain.com
data:    Name                            : domain.com
data:    Type                            : Microsoft.Network/dnszones
data:    Location                        : global
data:    Number of record sets           : 2
data:    Max number of record sets       : 10000
info:    network dns zone create command OK

Проверяем:

$ azure network dns zone list DEV_init
info:    Executing command network dns zone list
+ Getting the dns zones
data:    Name            Resource group
data:    --------------  --------------
data:    domain.com  DEV_init
info:    network dns zone list command OK

azure_dns_jm_2

Для просмотра записей зоны — выполняем:

$ azure network dns record-set list -g DEV_init -z domain.com
info:    Executing command network dns record-set list
+ Looking up the DNS Record Sets
data:    Name  TTL     Type  Records                         Tags
data:    ----  ------  ----  ------------------------------  ----
data:    @     172800  NS    ns1-06.azure-dns.com.
data:                        ns2-06.azure-dns.net.
data:                        ns3-06.azure-dns.org.
data:                        ns4-06.azure-dns.info.
data:    @     3600    SOA   host:    ns1-06.azure-dns.com.
data:                        email:   msnhst.microsoft.com
data:                        refresh: 3600
data:                        retry:   300
data:                        expire:  2419200
data:                        minimum: 300
info:    network dns record-set list command OK

Или — посмотреть только один тип записей, например — все NS для этой зоны:

$ azure network dns record-set list -g DEV_init -z domain.com NS
info:    Executing command network dns record-set list
+ Looking up the DNS Record Sets
data:    Name  TTL     Type  Records                 Tags
data:    ----  ------  ----  ----------------------  ----
data:    @     172800  NS    ns1-06.azure-dns.com.
data:                        ns2-06.azure-dns.net.
data:                        ns3-06.azure-dns.org.
data:                        ns4-06.azure-dns.info.
info:    network dns record-set list command OK

Добавляем A-запись для зоны:

$ azure network dns record-set create -g DEV_init -z domain.com -n "@" -y A
info:    Executing command network dns record-set create
warn:    using default TTL of 3600 seconds
+ Creating DNS record set "@"
data:    Id                              : /subscriptions/0a4f2b9c-***-40b17ef8c3ab/resourceGroups/DEV_init/providers/Microsoft.Network/dnszones/domain.com/A/@
data:    Name                            : @
data:    Type                            : Microsoft.Network/dnszones/A
data:    Location                        : global
data:    TTL                             : 3600
info:    network dns record-set create command OK

Проверяем:

$ azure network dns record-set list -g DEV_init -z domain.com A
info:    Executing command network dns record-set list
+ Looking up the DNS Record Sets
data:    Name  TTL   Type  Records  Tags
data:    ----  ----  ----  -------  ----
data:    @     3600  A
info:    network dns record-set list command OK

Добавляем значение для A-записи:

$ azure network dns record-set add-record -g DEV_init -z domain.com -n "@" -y A -a 8.8.8.8
info:    Executing command network dns record-set add-record
+ Looking up the dns zone "domain.com"
+ Looking up the DNS Record Set "@" of type "A"
+ Updating record set "@"
data:    Id                              : /subscriptions/0a4f2b9c-***-40b17ef8c3ab/resourceGroups/DEV_init/providers/Microsoft.Network/dnszones/domain.com/A/@
data:    Name                            : @
data:    Type                            : Microsoft.Network/dnszones/A
data:    Location                        : global
data:    TTL                             : 3600
data:    A records:
data:        IPv4 address                : 8.8.8.8
data:
info:    network dns record-set add-record command OK

Проверяем:

$ azure network dns record-set list -g DEV_init -z domain.com A
info:    Executing command network dns record-set list
+ Looking up the DNS Record Sets
data:    Name  TTL   Type  Records  Tags
data:    ----  ----  ----  -------  ----
data:    @     3600  A     8.8.8.8
info:    network dns record-set list command OK

Добавим еще один IP в A-запись:

$ azure network dns record-set add-record -g DEV_init -z domain.com -n "@" -y A -a 8.8.8.9
info:    Executing command network dns record-set add-record
+	g up the dns zone "domain.com"
+ Looking up the DNS Record Set "@" of type "A"
+ Updating record set "@"
data:    Id                              : /subscriptions/0a4f2b9c-***-40b17ef8c3ab/resourceGroups/DEV_init/providers/Microsoft.Network/dnszones/domain.com/A/@
data:    Name                            : @
data:    Type                            : Microsoft.Network/dnszones/A
data:    Location                        : global
data:    TTL                             : 3600
data:    A records:
data:        IPv4 address                : 8.8.8.8
data:        IPv4 address                : 8.8.8.9

Удалим предыдущую запись:

$ azure nazure network dns record-set delete-record -g DEV_init -z domain.com -n "@" -y A -a 8.8.8.8
info:    Executing command network dns record-set delete-record
+ Looking up the DNS Record Set "@" of type "A"
Delete DNS Record? [y/n] y
+ Updating record set "@"
data:    Id                              : /subscriptions/0a4f2b9c-***-40b17ef8c3ab/resourceGroups/DEV_init/providers/Microsoft.Network/dnszones/domain.com/A/@
data:    Name                            : @
data:    Type                            : Microsoft.Network/dnszones/A
data:    Location                        : global
data:    TTL                             : 3600
data:    A records:
data:        IPv4 address                : 8.8.8.9

Проверяем:

$ azure network dns record-set list -g DEV_init -z domain.com A
info:    Executing command network dns record-set list
+ Looking up the DNS Record Sets
data:    Name  TTL   Type  Records  Tags
data:    ----  ----  ----  -------  ----
data:    @     3600  A     8.8.8.9
info:    network dns record-set list command OK

И проверим dig-ом.

Находим NS Azure, на которые была добавлена зона:

$ azure network dns record-set list -g DEV_init -z domain.com NS
info:    Executing command network dns record-set list
+ Looking up the DNS Record Sets
data:    Name  TTL     Type  Records                 Tags
data:    ----  ------  ----  ----------------------  ----
data:    @     172800  NS    ns1-06.azure-dns.com.
data:                        ns2-06.azure-dns.net.
data:                        ns3-06.azure-dns.org.
data:                        ns4-06.azure-dns.info.
info:    network dns record-set list command OK

Проверяем:

$ dig @ns1-06.azure-dns.com domain.com

; <<>> DiG 9.9.5-3ubuntu0.8-Ubuntu <<>> @ns1-06.azure-dns.com domain.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 17304
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4000
;; QUESTION SECTION:
;domain.com.			IN	A

;; ANSWER SECTION:
domain.com.		3600	IN	A	8.8.8.9

;; Query time: 44 msec
;; SERVER: 40.90.4.6#53(40.90.4.6)
;; WHEN: Thu Mar 24 13:45:28 EET 2016
;; MSG SIZE  rcvd: 59

Что бы удалить A-запись вообще — выполняем:

$ azure network dns record-set delete -g DEV_init -z domain.com -n "@" -y A
info:    Executing command network dns record-set delete
Delete DNS record set "@"? [y/n] y
+ Deleting DNS record set "@"
info:    network dns record-set delete command OK

Проверяем:

$ azure network dns record-set list -g DEV_init -z domain.com
info:    Executing command network dns record-set list
+ Looking up the DNS Record Sets
data:    Name  TTL     Type  Records                         Tags
data:    ----  ------  ----  ------------------------------  ----
data:    @     172800  NS    ns1-06.azure-dns.com.
data:                        ns2-06.azure-dns.net.
data:                        ns3-06.azure-dns.org.
data:                        ns4-06.azure-dns.info.
data:    @     3600    SOA   host:    ns1-06.azure-dns.com.
data:                        email:   msnhst.microsoft.com
data:                        refresh: 3600
data:                        retry:   300
data:                        expire:  2419200
data:                        minimum: 300
info:    network dns record-set list command OK
$ azure network dns record-set list -g DEV_init -z domain.com A
info:    Executing command network dns record-set list
+ Looking up the DNS Record Sets
warn:    No DNS records sets found
info:    network dns record-set list command OK

Что бы удалить зону с DNS вообще — выполняем:

$ azure network dns zone delete -g DEV_init -n domain.com
info:    Executing command network dns zone delete
Delete dns zone "domain.com"? [y/n] y
+ Deleting dns zone "domain.com"
info:    network dns zone delete command OK

Ссылки по теме

Get started with Azure DNS using CLI
How to manage DNS records using CLI
DiG HOWTO