Azure Linux VM IPv6 + data disk

By | March 26, 2018

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et sapien purus. Sed augue ipsum, pellentesque eu sodales non, dictum ac metus. Maecenas a nibh pretium, eleifend orci sit amet, venenatis risus. Vestibulum sollicitudin eros a tortor luctus, sed eleifend enim hendrerit. Suspendisse in viverra lectus. Vivamus vitae orci tellus. Curabitur a ligula dolor. Phasellus malesuada non velit vitae rhoncus. Morbi mollis imperdiet vestibulum. Vivamus felis magna, efficitur eu metus eu, ultrices bibendum erat.

Login-AzureRmAccount

Get-AzureRmSubscription

#Personal -SubscriptionId
#Select-AzureRmSubscription -SubscriptionId 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

# Variables for common values

$rg = "LB-RG"
$location = "West Europe"
$vm = "server"
$vmName1 = $vm+"-01"
$DomainNameLabelipv4 = 'lbrgcloudipv4'
$DomainNameLabelipv6 = 'lbrgcloudipv6'

#$LoadBalancer
#$AvailablitySet
#$storageType = 'PremiumLRS'
#$dataDiskName1= $vmName1 + '_datadisk'

#$sshPublicKey = Get-Content "$env:USERPROFILE\.ssh\id_rsa.pub"
$ssh_key = "D:\my_key\id_rsa.pub"


New-AzureRmResourceGroup -Name $rg -location $location 

# Definer user name and blank password
$securePassword = ConvertTo-SecureString ' ' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("user", $securePassword)

$backendSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name LB-Subnet-BE -AddressPrefix 10.0.2.0/24
$vnet = New-AzureRmvirtualNetwork -Name VNet -ResourceGroupName $rg -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $backendSubnet

$publicIPv4 = New-AzureRmPublicIpAddress -Name 'pub-ipv4' -ResourceGroupName $rg -Location $location -AllocationMethod Static -IpAddressVersion IPv4 -DomainNameLabel $DomainNameLabelipv4
$publicIPv6 = New-AzureRmPublicIpAddress -Name 'pub-ipv6' -ResourceGroupName $rg -Location $location -AllocationMethod Dynamic -IpAddressVersion IPv6 -DomainNameLabel $DomainNameLabelipv6

$FEIPConfigv4 = New-AzureRmLoadBalancerFrontendIpConfig -Name "LB-Frontendv4" -PublicIpAddress $publicIPv4
$FEIPConfigv6 = New-AzureRmLoadBalancerFrontendIpConfig -Name "LB-Frontendv6" -PublicIpAddress $publicIPv6


$backendpoolipv4 = New-AzureRmLoadBalancerBackendAddressPoolConfig -Name "BackendPoolIPv4"
$backendpoolipv6 = New-AzureRmLoadBalancerBackendAddressPoolConfig -Name "BackendPoolIPv6"

$inboundNATRulev4 = New-AzureRmLoadBalancerInboundNatRuleConfig -Name "NatRulev4HTTPS" -FrontendIpConfiguration $FEIPConfigv4 -Protocol TCP -FrontendPort 443 -BackendPort 4443
$inboundNATRulev6 = New-AzureRmLoadBalancerInboundNatRuleConfig -Name "NatRulev6HTTPS" -FrontendIpConfiguration $FEIPConfigv6 -Protocol TCP -FrontendPort 443 -BackendPort 4443


$healthProbe = New-AzureRmLoadBalancerProbeConfig -Name 'HealthProbe-v4v6' -Protocol Tcp -Port 8080 -IntervalInSeconds 15 -ProbeCount 2
$SSHprobe = New-AzureRmLoadBalancerProbeConfig -Name 'SSHprobe' -Protocol Tcp -Port 22 -IntervalInSeconds 15 -ProbeCount 2
#$RDPprobe = New-AzureRmLoadBalancerProbeConfig -Name 'RDPprobe' -Protocol Tcp -Port 3389 -IntervalInSeconds 15 -ProbeCount 2

$lbrulev4 = New-AzureRmLoadBalancerRuleConfig -Name "HTTPv4" -FrontendIpConfiguration $FEIPConfigv4 -BackendAddressPool $backendpoolipv4 -Probe $healthProbe -Protocol Tcp -FrontendPort 80 -BackendPort 8080
$lbrulev6 = New-AzureRmLoadBalancerRuleConfig -Name "HTTPv6" -FrontendIpConfiguration $FEIPConfigv6 -BackendAddressPool $backendpoolipv6 -Probe $healthProbe -Protocol Tcp -FrontendPort 80 -BackendPort 8080
$SSHrule = New-AzureRmLoadBalancerRuleConfig -Name "SSHrule" -FrontendIpConfiguration $FEIPConfigv4 -BackendAddressPool $backendpoolipv4 -Probe $SSHprobe -Protocol Tcp -FrontendPort 22 -BackendPort 22

New-AzureRmLoadBalancer -ResourceGroupName $rg -Name "LoadBalancer" -Location $location -FrontendIpConfiguration $FEIPConfigv4,$FEIPConfigv6 -InboundNatRule $inboundNATRulev6,$inboundNATRulev4 -BackendAddressPool $backendpoolipv4,$backendpoolipv6 -Probe $healthProbe,$SSHprobe -LoadBalancingRule $lbrulev4,$lbrulev6,$SSHrule

$vnet = Get-AzureRmVirtualNetwork -Name VNet -ResourceGroupName $rg
$backendSubnet = Get-AzureRmVirtualNetworkSubnetConfig -Name LB-Subnet-BE -VirtualNetwork $vnet


$nic1IPv4 = New-AzureRmNetworkInterfaceIpConfig -Name "IPv4IPConfig" -PrivateIpAddressVersion "IPv4" -Subnet $backendSubnet -LoadBalancerBackendAddressPool $backendpoolipv4 -LoadBalancerInboundNatRule $inboundNATRulev4
$nic1IPv6 = New-AzureRmNetworkInterfaceIpConfig -Name "IPv6IPConfig" -PrivateIpAddressVersion "IPv6" -LoadBalancerBackendAddressPool $backendpoolipv6 -LoadBalancerInboundNatRule $inboundNATRulev6
$nic1 = New-AzureRmNetworkInterface -Name "Nic1" -IpConfiguration $nic1IPv4,$nic1IPv6 -ResourceGroupName $rg -Location $location


New-AzureRmAvailabilitySet -Name "AvailablitySet" -ResourceGroupName $rg -Sku Aligned -platformUpdateDomainCount 1 -platformFaultDomainCount 1 -location $location
$availabilitySet = Get-AzureRmAvailabilitySet -Name "AvailablitySet" -ResourceGroupName $rg
#Update-AzureRmAvailabilitySet -AvailabilitySet $availabilitySet -Sku Aligned

# Create a virtual machine configuration $availabilitySet.Id 
$vm1 = New-AzureRmVMConfig -VMName $vmName1 -VMSize Standard_B2s -AvailabilitySetId $availabilitySet.Id | `
Set-AzureRmVMOperatingSystem -Linux -ComputerName $vmName1 -Credential $cred -DisablePasswordAuthentication | `
Set-AzureRmVMSourceImage -PublisherName Canonical -Offer UbuntuServer -Skus 16.04-LTS -Version latest 
Add-AzureRmVMNetworkInterface -VM $vm1 -Id $nic1.Id


# Configure SSH Keys
$sshPublicKey = Get-Content $ssh_key | Out-String
Add-AzureRmVMSshPublicKey -VM $vm1 -KeyData $sshPublicKey -Path "/home/user/.ssh/authorized_keys"

New-AzureRmVM -ResourceGroupName $rg -Location $location -VM $vm1

#Attach a data disk
$diskConfig = New-AzureRmDiskConfig -AccountType $storageType -Location $location -CreateOption Empty -DiskSizeGB 128
$dataDisk1 = New-AzureRmDisk -DiskName $dataDiskName1 -Disk $diskConfig -ResourceGroupName $rg

$vm1 = Get-AzureRmVM -Name $vmName1 -ResourceGroupName $rg
$vm1 = Add-AzureRmVMDataDisk -VM $vm1 -Name $dataDiskName1 -CreateOption Attach -ManagedDiskId $dataDisk1.Id -Lun 1

Update-AzureRmVM -VM $vm1 -ResourceGroupName $rg

# Install APACHE + PHP.
$PublicSettings = '{"commandToExecute":" apt -y update && apt -y upgrade && apt -y install apache2 php libapache2-mod-php php-cli php-cgi php-common php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc php-mysqlnd && service apache2 restart"}'


Set-AzureRmVMExtension -ExtensionName "LAMP" -ResourceGroupName $rg -VMName $vmName1 `
-Publisher "Microsoft.Azure.Extensions" -ExtensionType "CustomScript" -TypeHandlerVersion 2.0 `
-SettingString $PublicSettings -Location $location

#############################################################################################

##Activate IPv6 address by Azure DHCP
#Create file and add:
#/etc/dhcp/dhclient6.conf  
#timeout 10;

##open  and add:
#/etc/network/interfaces.d/50-cloud-init.cfg
#iface eth0 inet6 auto
#up sleep 5
#up dhclient -1 -6 -cf /etc/dhcp/dhclient6.conf -lf /var/lib/dhcp/dhclient6.eth0.leases -v eth0 || true
##After
#sudo ifdown eth0 && sudo ifup eth0