Azure CLI入门:建立三层网络
时间:2020-03-21 11:47:57 来源:igfitidea点击:
Azure CLI是旨在帮助您快速有效地管理Azure服务的工具。
我花了一些时间使用Azure CLI来了解它是否可以与AWS CLI相提并论。
Azure客户端配置
本文假定系统上已经安装了azure-cli
软件包。
配置客户端:
$az configure
登录到Azure:
$az login You have logged in. Now let us find all the subscriptions to which you have access... [ { "cloudName": "AzureCloud", "id": "abcdefgh-1234-5678", "isDefault": true, "name": "example Azure EMEA POS PAYG", "state": "Enabled", "tenantId": "1234567-1234-5678", "user": { "name": "[email protected]", "type": "user" } } ]
通过CLI调配Azure资源
我们将创建一个3层网络:DMZ,应用程序和数据库。
资源组
首先,创建一个名为" ux_group"的资源组。
UX代表用户体验。
$az group create --name ux_group --location uksouth
如果要删除资源组,请执行以下操作:
$az group delete --name ux_group
虚拟网络和子网
创建虚拟网络和DMZ子网:
$az network vnet create \ --name ux_vnet \ --resource-group ux_group \ --subnet-name ux_vlan_dmz \ --address-prefixes 10.1.0.0/22 \ --subnet-prefixes 10.1.1.0/24
为应用程序层创建一个新的子网:
$az network vnet subnet create \ --vnet-name ux_vnet \ --resource-group ux_group \ --name ux_vlan_app \ --address-prefixes 10.1.2.0/24
为数据库层创建一个新的子网:
$az network vnet subnet create \ --vnet-name ux_vnet \ --resource-group ux_group \ --name ux_vlan_secure \ --address-prefixes 10.1.3.0/24
安全组
创建三个安全组以用于我们的3层基础结构:
$az network nsg create \ --resource-group ux_group \ --name ux_nsg_dmz
$az network nsg create \ --resource-group ux_group \ --name ux_nsg_app
$az network nsg create \ --resource-group ux_group \ --name ux_nsg_secure
如果需要删除安全组,请执行以下操作
$az network nsg delete \ --resource-group ux_group \ --name ux_nsg_dmz
防火墙规则
为DMZ子网创建防火墙规则,以允许自定义SSH访问:
$az network nsg rule create \ --name Allow_custom_SSH \ --nsg-name ux_nsg_dmz \ --priority 100 \ --resource-group ux_group \ --access Allow \ --protocol Tcp \ --direction Inbound \ --destination-port-ranges 22 \ --source-address-prefixes 1.2.3.4/32 5.6.7.8/32
为DMZ子网创建防火墙规则以允许HTTP/S访问:
$az network nsg rule create \ --name Allow_HTTP_HTTPS \ --nsg-name ux_nsg_dmz \ --priority 101 \ --resource-group ux_group \ --access Allow \ --protocol Tcp \ --direction Inbound \ --destination-port-ranges 80 443
为应用程序子网创建防火墙规则以允许自定义SSH访问:
$az network nsg rule create \ --name Allow_custom_SSH \ --nsg-name ux_nsg_app \ --priority 100 \ --resource-group ux_group \ --access Allow \ --protocol Tcp \ --direction Inbound \ --destination-port-ranges 22 \ --source-address-prefixes 1.2.3.4/32 5.6.7.8/32
为数据库子网创建防火墙规则以允许自定义SSH访问:
$az network nsg rule create \ --name Allow_custom_SSH \ --nsg-name ux_nsg_secure \ --priority 100 \ --resource-group ux_group \ --access Allow \ --protocol Tcp \ --direction Inbound \ --destination-port-ranges 22 \ --source-address-prefixes 1.2.3.4/32 5.6.7.8/32
存储
创建一个存储帐户:
$az storage account create \ --sku Premium_LRS \ --kind StorageV2 \ --resource-group ux_group \ --name uxstorageaccount
为DMZ服务器创建磁盘(在VM设置过程中创建磁盘时不需要):
$az disk create \ --name mydmzserver1_disk1 \ --resource-group ux_group \ --size-gb 30 \ --sku Standard_LRS
创建DMZ VM
创建将由DMZ服务器使用的公共IP:
$az network public-ip create \ --name ux_ip1 \ --resource-group ux_group \ --allocation-method Static
为DMZ服务器创建一个NIC并附加公共IP:
$az network nic create \ --name mydmzserver1_nic1 \ --resource-group ux_group \ --vnet-name ux_vnet \ --subnet ux_vlan_dmz \ --network-security-group ux_nsg_dmz \ --public-ip-address ux_ip1 \ --private-ip-address 10.1.1.5
在DMZ子网中创建一个新的虚拟机。
指定现有的NIC时,请勿指定NSG,公共IP,ASG,VNet或者子网。
$az vm create \ --name mydmzserver1 \ --resource-group ux_group \ --nics mydmzserver1_nic1 \ --image centos \ --size Standard_B2ms \ --authentication-type ssh \ --ssh-key-value ~/.ssh/id_rsa.pub \ --os-disk-name mydmzserver1_disk1 \ --os-disk-size-gb 30 \ --os-disk-caching ReadWrite \ --storage-sku Standard_LRS
创建一个应用程序虚拟机
为应用程序服务器创建公共IP:
$az network public-ip create \ --name ux_ip2 \ --resource-group ux_group \ --allocation-method Static
为应用程序服务器创建NIC:
$az network nic create \ --name myappserver1_nic1 \ --resource-group ux_group \ --vnet-name ux_vnet \ --subnet ux_vlan_app \ --network-security-group ux_nsg_app \ --public-ip-address ux_ip2 \ --private-ip-address 10.1.2.5
创建应用程序VM。
指定现有的NIC时,请勿指定NSG,公共IP,ASG,VNet或者子网。
$az vm create \ --name myappserver1 \ --resource-group ux_group \ --nics myappserver1_nic1 \ --image centos \ --size Standard_D2s_v3 \ --authentication-type ssh \ --ssh-key-value ~/.ssh/id_rsa.pub \ --os-disk-name myappserver1_disk1 \ --os-disk-size-gb 64 \ --os-disk-caching ReadWrite \ --storage-sku Premium_LRS
创建数据库虚拟机
为数据库服务器创建一个公共IP:
$az network public-ip create \ --name ux_ip3 \ --resource-group ux_group \ --allocation-method Static
为数据库服务器创建一个NIC:
$az network nic create \ --name mydbserver1_nic1 \ --resource-group ux_group \ --vnet-name ux_vnet \ --subnet ux_vlan_secure \ --network-security-group ux_nsg_secure \ --public-ip-address ux_ip3 \ --private-ip-address 10.1.3.5
创建数据库VM。
指定现有的NIC时,请勿指定NSG,公共IP,ASG,VNet或者子网。
$az vm create \ --name mydbserver1 \ --resource-group ux_group \ --nics myappserver1_nic1 \ --image centos \ --size Standard_D2s_v3 \ --authentication-type ssh \ --ssh-key-value ~/.ssh/id_rsa.pub \ --os-disk-name myappserver1_disk1 \ --os-disk-size-gb 64 \ --os-disk-caching ReadWrite \ --storage-sku Premium_LRS
在这一阶段,我们应该在Azure云中运行3台服务器。