use of com.microsoft.azure.management.compute.VirtualMachineScaleSet in project azure-sdk-for-java by Azure.
the class ManageManagedDisks method runSample.
/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
final Region region = Region.US_EAST;
final String rgName = Utils.createRandomName("rgCOMV");
final String userName = "tirekicker";
final String sshkey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD azjava@javalib.com";
try {
// ::==Create a VM
// Create a virtual machine with an implicit Managed OS disk and explicit Managed data disk
System.out.println("Creating VM [with an implicit Managed OS disk and explicit Managed data disk]");
final String linuxVM1Name = SdkContext.randomResourceName("vm" + "-", 18);
final String linuxVM1Pip = SdkContext.randomResourceName("pip" + "-", 18);
VirtualMachine linuxVM1 = azure.virtualMachines().define(linuxVM1Name).withRegion(region).withNewResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(linuxVM1Pip).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withSsh(sshkey).withNewDataDisk(100).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
System.out.println("Created VM [with an implicit Managed OS disk and explicit Managed data disk]");
// Creation is simplified with implicit creation of managed disks without specifying all the disk details. You will notice that you do not require storage accounts
// ::== Update the VM
// Create a VMSS with implicit managed OS disks and explicit managed data disks
System.out.println("Creating VMSS [with implicit managed OS disks and explicit managed data disks]");
final String vmScaleSetName = SdkContext.randomResourceName("vmss" + "-", 18);
VirtualMachineScaleSet vmScaleSet = azure.virtualMachineScaleSets().define(vmScaleSetName).withRegion(region).withExistingResourceGroup(rgName).withSku(VirtualMachineScaleSetSkuTypes.STANDARD_D5_V2).withExistingPrimaryNetworkSubnet(prepareNetwork(azure, region, rgName), "subnet1").withExistingPrimaryInternetFacingLoadBalancer(prepareLoadBalancer(azure, region, rgName)).withoutPrimaryInternalLoadBalancer().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername("tirekicker").withSsh(sshkey).withNewDataDisk(100).withNewDataDisk(100, 1, CachingTypes.READ_WRITE).withNewDataDisk(100, 2, CachingTypes.READ_ONLY).withCapacity(3).create();
System.out.println("Created VMSS [with implicit managed OS disks and explicit managed data disks]");
// Create an empty disk and attach to a VM (Manage Virtual Machine With Disk)
System.out.println("Creating empty data disk [to attach to a VM]");
final String diskName = SdkContext.randomResourceName("dsk" + "-", 18);
Disk dataDisk = azure.disks().define(diskName).withRegion(region).withExistingResourceGroup(rgName).withData().withSizeInGB(50).create();
System.out.println("Created empty data disk [to attach to a VM]");
System.out.println("Creating VM [with new managed data disks and disk attached]");
final String linuxVM2Name = SdkContext.randomResourceName("vm" + "-", 10);
final String linuxVM2Pip = SdkContext.randomResourceName("pip" + "-", 18);
VirtualMachine linuxVM2 = azure.virtualMachines().define(linuxVM2Name).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(linuxVM2Pip).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withSsh(sshkey).withNewDataDisk(100).withNewDataDisk(100, 1, CachingTypes.READ_WRITE).withExistingDataDisk(dataDisk).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
System.out.println("Created VM [with new managed data disks and disk attached]");
// Update a VM
System.out.println("Updating VM [by detaching a disk and adding empty disk]");
linuxVM2.update().withoutDataDisk(2).withNewDataDisk(200).apply();
System.out.println("Updated VM [by detaching a disk and adding empty disk]");
// Create a VM from an image (Create Virtual Machine Using Custom Image from VM)
System.out.println("Preparing specialized virtual machine with un-managed disk");
final VirtualMachine linuxVM = prepareSpecializedUnmanagedVirtualMachine(azure, region, rgName);
System.out.println("Prepared specialized virtual machine with un-managed disk");
System.out.println("Creating custom image from specialized virtual machine");
final String customImageName = SdkContext.randomResourceName("cimg" + "-", 10);
VirtualMachineCustomImage virtualMachineCustomImage = azure.virtualMachineCustomImages().define(customImageName).withRegion(region).withExistingResourceGroup(rgName).fromVirtualMachine(// from a deallocated and generalized VM
linuxVM).create();
System.out.println("Created custom image from specialized virtual machine");
System.out.println("Creating VM [from custom image]");
final String linuxVM3Name = SdkContext.randomResourceName("vm" + "-", 10);
VirtualMachine linuxVM3 = azure.virtualMachines().define(linuxVM3Name).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withLinuxCustomImage(virtualMachineCustomImage.id()).withRootUsername(userName).withSsh(sshkey).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
System.out.println("Created VM [from custom image]");
// Create a VM from a VHD (Create Virtual Machine Using Specialized VHD)
final String linuxVMName4 = SdkContext.randomResourceName("vm" + "-", 10);
final String specializedVhd = linuxVM.osUnmanagedDiskVhdUri();
azure.virtualMachines().deleteById(linuxVM.id());
System.out.println("Creating VM [by attaching un-managed disk]");
VirtualMachine linuxVM4 = azure.virtualMachines().define(linuxVMName4).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withSpecializedOSUnmanagedDisk(specializedVhd, OperatingSystemTypes.LINUX).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
System.out.println("Created VM [by attaching un-managed disk]");
// Create a Snapshot (Create Virtual Machine using specialized disks from snapshot)
System.out.println("Preparing specialized virtual machine with managed disks");
final VirtualMachine linuxVM5 = prepareSpecializedManagedVirtualMachine(azure, region, rgName);
Disk osDisk = azure.disks().getById(linuxVM5.osDiskId());
List<Disk> dataDisks = new ArrayList<>();
for (VirtualMachineDataDisk disk : linuxVM5.dataDisks().values()) {
Disk d = azure.disks().getById(disk.id());
dataDisks.add(d);
}
System.out.println("Prepared specialized virtual machine with managed disks");
System.out.println("Deleting VM: " + linuxVM5.id());
azure.virtualMachines().deleteById(linuxVM5.id());
System.out.println("Deleted the VM: " + linuxVM5.id());
System.out.println("Creating snapshot [from managed OS disk]");
// Create a managed snapshot for an OS disk
final String managedOSSnapshotName = SdkContext.randomResourceName("snp" + "-", 10);
Snapshot osSnapshot = azure.snapshots().define(managedOSSnapshotName).withRegion(region).withExistingResourceGroup(rgName).withLinuxFromDisk(osDisk).create();
System.out.println("Created snapshot [from managed OS disk]");
System.out.println("Creating managed OS disk [from snapshot]");
// Create a managed disk from the managed snapshot for the OS disk
final String managedNewOSDiskName = SdkContext.randomResourceName("dsk" + "-", 10);
Disk newOSDisk = azure.disks().define(managedNewOSDiskName).withRegion(region).withExistingResourceGroup(rgName).withLinuxFromSnapshot(osSnapshot).withSizeInGB(100).create();
System.out.println("Created managed OS disk [from snapshot]");
System.out.println("Creating managed data snapshot [from managed data disk]");
// Create a managed snapshot for a data disk
final String managedDataDiskSnapshotName = SdkContext.randomResourceName("dsk" + "-", 10);
Snapshot dataSnapshot = azure.snapshots().define(managedDataDiskSnapshotName).withRegion(region).withExistingResourceGroup(rgName).withDataFromDisk(dataDisks.get(0)).withSku(DiskSkuTypes.STANDARD_LRS).create();
System.out.println("Created managed data snapshot [from managed data disk]");
System.out.println("Creating managed data disk [from managed snapshot]");
// Create a managed disk from the managed snapshot for the data disk
final String managedNewDataDiskName = SdkContext.randomResourceName("dsk" + "-", 10);
Disk newDataDisk = azure.disks().define(managedNewDataDiskName).withRegion(region).withExistingResourceGroup(rgName).withData().fromSnapshot(dataSnapshot).create();
System.out.println("Created managed data disk [from managed snapshot]");
System.out.println("Creating VM [with specialized OS managed disk]");
final String linuxVM6Name = SdkContext.randomResourceName("vm" + "-", 10);
VirtualMachine linuxVM6 = azure.virtualMachines().define(linuxVM6Name).withRegion(region).withExistingResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withSpecializedOSDisk(newOSDisk, OperatingSystemTypes.LINUX).withExistingDataDisk(newDataDisk).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
System.out.println("Created VM [with specialized OS managed disk]");
// ::== Migrate a VM to managed disks with a single reboot
System.out.println("Creating VM [with un-managed disk for migration]");
final String linuxVM7Name = SdkContext.randomResourceName("vm" + "-", 10);
final String linuxVM7Pip = SdkContext.randomResourceName("pip" + "-", 18);
VirtualMachine linuxVM7 = azure.virtualMachines().define(linuxVM7Name).withRegion(region).withNewResourceGroup(rgName).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(linuxVM7Pip).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername("tirekicker").withSsh(sshkey).withUnmanagedDisks().withNewUnmanagedDataDisk(100).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).create();
System.out.println("Created VM [with un-managed disk for migration]");
System.out.println("De-allocating VM :" + linuxVM7.id());
linuxVM7.deallocate();
System.out.println("De-allocated VM :" + linuxVM7.id());
System.out.println("Migrating VM");
linuxVM7.convertToManaged();
System.out.println("Migrated VM");
return true;
} catch (Exception f) {
System.out.println(f.getMessage());
f.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().deleteByName(rgName);
System.out.println("Deleted Resource Group: " + rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}
return false;
}
use of com.microsoft.azure.management.compute.VirtualMachineScaleSet in project azure-sdk-for-java by Azure.
the class ManageVirtualMachineScaleSetWithUnmanagedDisks method runSample.
/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
final Region region = Region.US_WEST_CENTRAL;
final String rgName = SdkContext.randomResourceName("rgCOVS", 15);
final String vnetName = SdkContext.randomResourceName("vnet", 24);
final String loadBalancerName1 = SdkContext.randomResourceName("intlb" + "-", 18);
final String publicIpName = "pip-" + loadBalancerName1;
final String frontendName = loadBalancerName1 + "-FE1";
final String backendPoolName1 = loadBalancerName1 + "-BAP1";
final String backendPoolName2 = loadBalancerName1 + "-BAP2";
final String httpProbe = "httpProbe";
final String httpsProbe = "httpsProbe";
final String httpLoadBalancingRule = "httpRule";
final String httpsLoadBalancingRule = "httpsRule";
final String natPool50XXto22 = "natPool50XXto22";
final String natPool60XXto23 = "natPool60XXto23";
final String vmssName = SdkContext.randomResourceName("vmss", 24);
final String storageAccountName1 = SdkContext.randomResourceName("stg1", 24);
final String storageAccountName2 = SdkContext.randomResourceName("stg2", 24);
final String storageAccountName3 = SdkContext.randomResourceName("stg3", 24);
final String userName = "tirekicker";
final String sshKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD azjava@javalib.com";
final String apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/azure-samples/src/main/resources/install_apache.sh";
final String installCommand = "bash install_apache.sh";
List<String> fileUris = new ArrayList<>();
fileUris.add(apacheInstallScript);
try {
//=============================================================
// Create a virtual network with a frontend subnet
System.out.println("Creating virtual network with a frontend subnet ...");
Network network = azure.networks().define(vnetName).withRegion(region).withNewResourceGroup(rgName).withAddressSpace("172.16.0.0/16").defineSubnet("Front-end").withAddressPrefix("172.16.1.0/24").attach().create();
System.out.println("Created a virtual network");
// Print the virtual network details
Utils.print(network);
//=============================================================
// Create a public IP address
System.out.println("Creating a public IP address...");
PublicIPAddress publicIPAddress = azure.publicIPAddresses().define(publicIpName).withRegion(region).withExistingResourceGroup(rgName).withLeafDomainLabel(publicIpName).create();
System.out.println("Created a public IP address");
// Print the virtual network details
Utils.print(publicIPAddress);
//=============================================================
// Create an Internet facing load balancer with
// One frontend IP address
// Two backend address pools which contain network interfaces for the virtual
// machines to receive HTTP and HTTPS network traffic from the load balancer
// Two load balancing rules for HTTP and HTTPS to map public ports on the load
// balancer to ports in the backend address pool
// Two probes which contain HTTP and HTTPS health probes used to check availability
// of virtual machines in the backend address pool
// Three inbound NAT rules which contain rules that map a public port on the load
// balancer to a port for a specific virtual machine in the backend address pool
// - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23
System.out.println("Creating a Internet facing load balancer with ...");
System.out.println("- A frontend IP address");
System.out.println("- Two backend address pools which contain network interfaces for the virtual\n" + " machines to receive HTTP and HTTPS network traffic from the load balancer");
System.out.println("- Two load balancing rules for HTTP and HTTPS to map public ports on the load\n" + " balancer to ports in the backend address pool");
System.out.println("- Two probes which contain HTTP and HTTPS health probes used to check availability\n" + " of virtual machines in the backend address pool");
System.out.println("- Two inbound NAT rules which contain rules that map a public port on the load\n" + " balancer to a port for a specific virtual machine in the backend address pool\n" + " - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23");
LoadBalancer loadBalancer1 = azure.loadBalancers().define(loadBalancerName1).withRegion(region).withExistingResourceGroup(rgName).definePublicFrontend(frontendName).withExistingPublicIPAddress(publicIPAddress).attach().defineBackend(backendPoolName1).attach().defineBackend(backendPoolName2).attach().defineHttpProbe(httpProbe).withRequestPath("/").withPort(80).attach().defineHttpProbe(httpsProbe).withRequestPath("/").withPort(443).attach().defineLoadBalancingRule(httpLoadBalancingRule).withProtocol(TransportProtocol.TCP).withFrontend(frontendName).withFrontendPort(80).withProbe(httpProbe).withBackend(backendPoolName1).attach().defineLoadBalancingRule(httpsLoadBalancingRule).withProtocol(TransportProtocol.TCP).withFrontend(frontendName).withFrontendPort(443).withProbe(httpsProbe).withBackend(backendPoolName2).attach().defineInboundNatPool(natPool50XXto22).withProtocol(TransportProtocol.TCP).withFrontend(frontendName).withFrontendPortRange(5000, 5099).withBackendPort(22).attach().defineInboundNatPool(natPool60XXto23).withProtocol(TransportProtocol.TCP).withFrontend(frontendName).withFrontendPortRange(6000, 6099).withBackendPort(23).attach().create();
// Print load balancer details
System.out.println("Created a load balancer");
Utils.print(loadBalancer1);
//=============================================================
// Create a virtual machine scale set with three virtual machines
// And, install Apache Web servers on them
System.out.println("Creating virtual machine scale set with three virtual machines" + " in the frontend subnet ...");
Date t1 = new Date();
VirtualMachineScaleSet virtualMachineScaleSet = azure.virtualMachineScaleSets().define(vmssName).withRegion(region).withExistingResourceGroup(rgName).withSku(VirtualMachineScaleSetSkuTypes.STANDARD_D3_V2).withExistingPrimaryNetworkSubnet(network, "Front-end").withExistingPrimaryInternetFacingLoadBalancer(loadBalancer1).withPrimaryInternetFacingLoadBalancerBackends(backendPoolName1, backendPoolName2).withPrimaryInternetFacingLoadBalancerInboundNatPools(natPool50XXto22, natPool60XXto23).withoutPrimaryInternalLoadBalancer().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withSsh(sshKey).withUnmanagedDisks().withNewStorageAccount(storageAccountName1).withNewStorageAccount(storageAccountName2).withNewStorageAccount(storageAccountName3).withCapacity(3).defineNewExtension("CustomScriptForLinux").withPublisher("Microsoft.OSTCExtensions").withType("CustomScriptForLinux").withVersion("1.4").withMinorVersionAutoUpgrade().withPublicSetting("fileUris", fileUris).withPublicSetting("commandToExecute", installCommand).attach().create();
Date t2 = new Date();
System.out.println("Created a virtual machine scale set with " + "3 Linux VMs & Apache Web servers on them: (took " + ((t2.getTime() - t1.getTime()) / 1000) + " seconds) ");
System.out.println();
// Print virtual machine scale set details
// Utils.print(virtualMachineScaleSet);
//=============================================================
// List virtual machine scale set network interfaces
System.out.println("Listing scale set network interfaces ...");
PagedList<VirtualMachineScaleSetNetworkInterface> vmssNics = virtualMachineScaleSet.listNetworkInterfaces();
for (VirtualMachineScaleSetNetworkInterface vmssNic : vmssNics) {
System.out.println(vmssNic.id());
}
//=============================================================
// List virtual machine scale set instance network interfaces and SSH connection string
System.out.println("Listing scale set virtual machine instance network interfaces and SSH connection string...");
for (VirtualMachineScaleSetVM instance : virtualMachineScaleSet.virtualMachines().list()) {
System.out.println("Scale set virtual machine instance #" + instance.instanceId());
System.out.println(instance.id());
PagedList<VirtualMachineScaleSetNetworkInterface> networkInterfaces = instance.listNetworkInterfaces();
// Pick the first NIC
VirtualMachineScaleSetNetworkInterface networkInterface = networkInterfaces.get(0);
for (VirtualMachineScaleSetNicIPConfiguration ipConfig : networkInterface.ipConfigurations().values()) {
if (ipConfig.isPrimary()) {
List<LoadBalancerInboundNatRule> natRules = ipConfig.listAssociatedLoadBalancerInboundNatRules();
for (LoadBalancerInboundNatRule natRule : natRules) {
if (natRule.backendPort() == 22) {
System.out.println("SSH connection string: " + userName + "@" + publicIPAddress.fqdn() + ":" + natRule.frontendPort());
break;
}
}
break;
}
}
}
//=============================================================
// Stop the virtual machine scale set
System.out.println("Stopping virtual machine scale set ...");
virtualMachineScaleSet.powerOff();
System.out.println("Stopped virtual machine scale set");
//=============================================================
// Start the virtual machine scale set
System.out.println("Starting virtual machine scale set ...");
virtualMachineScaleSet.start();
System.out.println("Started virtual machine scale set");
//=============================================================
// Update the virtual machine scale set
// - double the no. of virtual machines
System.out.println("Updating virtual machine scale set " + "- double the no. of virtual machines ...");
virtualMachineScaleSet.update().withCapacity(6).apply();
System.out.println("Doubled the no. of virtual machines in " + "the virtual machine scale set");
//=============================================================
// re-start virtual machine scale set
System.out.println("re-starting virtual machine scale set ...");
virtualMachineScaleSet.restart();
System.out.println("re-started virtual machine scale set");
return true;
} catch (Exception f) {
System.out.println(f.getMessage());
f.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().deleteByName(rgName);
System.out.println("Deleted Resource Group: " + rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}
return false;
}
use of com.microsoft.azure.management.compute.VirtualMachineScaleSet in project azure-sdk-for-java by Azure.
the class ManageVirtualMachineScaleSet method runSample.
/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
final Region region = Region.US_WEST_CENTRAL;
final String rgName = SdkContext.randomResourceName("rgCOVS", 15);
final String vnetName = SdkContext.randomResourceName("vnet", 24);
final String loadBalancerName1 = SdkContext.randomResourceName("intlb" + "-", 18);
final String publicIpName = "pip-" + loadBalancerName1;
final String frontendName = loadBalancerName1 + "-FE1";
final String backendPoolName1 = loadBalancerName1 + "-BAP1";
final String backendPoolName2 = loadBalancerName1 + "-BAP2";
final String httpProbe = "httpProbe";
final String httpsProbe = "httpsProbe";
final String httpLoadBalancingRule = "httpRule";
final String httpsLoadBalancingRule = "httpsRule";
final String natPool50XXto22 = "natPool50XXto22";
final String natPool60XXto23 = "natPool60XXto23";
final String vmssName = SdkContext.randomResourceName("vmss", 24);
final String userName = "tirekicker";
final String sshKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD azjava@javalib.com";
final String apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/azure-samples/src/main/resources/install_apache.sh";
final String installCommand = "bash install_apache.sh";
List<String> fileUris = new ArrayList<>();
fileUris.add(apacheInstallScript);
try {
//=============================================================
// Create a virtual network with a frontend subnet
System.out.println("Creating virtual network with a frontend subnet ...");
Network network = azure.networks().define(vnetName).withRegion(region).withNewResourceGroup(rgName).withAddressSpace("172.16.0.0/16").defineSubnet("Front-end").withAddressPrefix("172.16.1.0/24").attach().create();
System.out.println("Created a virtual network");
// Print the virtual network details
Utils.print(network);
//=============================================================
// Create a public IP address
System.out.println("Creating a public IP address...");
PublicIPAddress publicIPAddress = azure.publicIPAddresses().define(publicIpName).withRegion(region).withExistingResourceGroup(rgName).withLeafDomainLabel(publicIpName).create();
System.out.println("Created a public IP address");
// Print the virtual network details
Utils.print(publicIPAddress);
//=============================================================
// Create an Internet facing load balancer with
// One frontend IP address
// Two backend address pools which contain network interfaces for the virtual
// machines to receive HTTP and HTTPS network traffic from the load balancer
// Two load balancing rules for HTTP and HTTPS to map public ports on the load
// balancer to ports in the backend address pool
// Two probes which contain HTTP and HTTPS health probes used to check availability
// of virtual machines in the backend address pool
// Three inbound NAT rules which contain rules that map a public port on the load
// balancer to a port for a specific virtual machine in the backend address pool
// - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23
System.out.println("Creating a Internet facing load balancer with ...");
System.out.println("- A frontend IP address");
System.out.println("- Two backend address pools which contain network interfaces for the virtual\n" + " machines to receive HTTP and HTTPS network traffic from the load balancer");
System.out.println("- Two load balancing rules for HTTP and HTTPS to map public ports on the load\n" + " balancer to ports in the backend address pool");
System.out.println("- Two probes which contain HTTP and HTTPS health probes used to check availability\n" + " of virtual machines in the backend address pool");
System.out.println("- Two inbound NAT rules which contain rules that map a public port on the load\n" + " balancer to a port for a specific virtual machine in the backend address pool\n" + " - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23");
LoadBalancer loadBalancer1 = azure.loadBalancers().define(loadBalancerName1).withRegion(region).withExistingResourceGroup(rgName).definePublicFrontend(frontendName).withExistingPublicIPAddress(publicIPAddress).attach().defineBackend(backendPoolName1).attach().defineBackend(backendPoolName2).attach().defineHttpProbe(httpProbe).withRequestPath("/").withPort(80).attach().defineHttpProbe(httpsProbe).withRequestPath("/").withPort(443).attach().defineLoadBalancingRule(httpLoadBalancingRule).withProtocol(TransportProtocol.TCP).withFrontend(frontendName).withFrontendPort(80).withProbe(httpProbe).withBackend(backendPoolName1).attach().defineLoadBalancingRule(httpsLoadBalancingRule).withProtocol(TransportProtocol.TCP).withFrontend(frontendName).withFrontendPort(443).withProbe(httpsProbe).withBackend(backendPoolName2).attach().defineInboundNatPool(natPool50XXto22).withProtocol(TransportProtocol.TCP).withFrontend(frontendName).withFrontendPortRange(5000, 5099).withBackendPort(22).attach().defineInboundNatPool(natPool60XXto23).withProtocol(TransportProtocol.TCP).withFrontend(frontendName).withFrontendPortRange(6000, 6099).withBackendPort(23).attach().create();
// Print load balancer details
System.out.println("Created a load balancer");
Utils.print(loadBalancer1);
//=============================================================
// Create a virtual machine scale set with three virtual machines
// And, install Apache Web servers on them
System.out.println("Creating virtual machine scale set with three virtual machines" + " in the frontend subnet ...");
Date t1 = new Date();
VirtualMachineScaleSet virtualMachineScaleSet = azure.virtualMachineScaleSets().define(vmssName).withRegion(region).withExistingResourceGroup(rgName).withSku(VirtualMachineScaleSetSkuTypes.STANDARD_D3_V2).withExistingPrimaryNetworkSubnet(network, "Front-end").withExistingPrimaryInternetFacingLoadBalancer(loadBalancer1).withPrimaryInternetFacingLoadBalancerBackends(backendPoolName1, backendPoolName2).withPrimaryInternetFacingLoadBalancerInboundNatPools(natPool50XXto22, natPool60XXto23).withoutPrimaryInternalLoadBalancer().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withSsh(sshKey).withNewDataDisk(100).withNewDataDisk(100, 1, CachingTypes.READ_WRITE).withNewDataDisk(100, 2, CachingTypes.READ_WRITE, StorageAccountTypes.STANDARD_LRS).withCapacity(3).defineNewExtension("CustomScriptForLinux").withPublisher("Microsoft.OSTCExtensions").withType("CustomScriptForLinux").withVersion("1.4").withMinorVersionAutoUpgrade().withPublicSetting("fileUris", fileUris).withPublicSetting("commandToExecute", installCommand).attach().create();
Date t2 = new Date();
System.out.println("Created a virtual machine scale set with " + "3 Linux VMs & Apache Web servers on them: (took " + ((t2.getTime() - t1.getTime()) / 1000) + " seconds) ");
System.out.println();
// Print virtual machine scale set details
// Utils.print(virtualMachineScaleSet);
//=============================================================
// List virtual machine scale set network interfaces
System.out.println("Listing scale set network interfaces ...");
PagedList<VirtualMachineScaleSetNetworkInterface> vmssNics = virtualMachineScaleSet.listNetworkInterfaces();
for (VirtualMachineScaleSetNetworkInterface vmssNic : vmssNics) {
System.out.println(vmssNic.id());
}
//=============================================================
// List virtual machine scale set instance network interfaces and SSH connection string
System.out.println("Listing scale set virtual machine instance network interfaces and SSH connection string...");
for (VirtualMachineScaleSetVM instance : virtualMachineScaleSet.virtualMachines().list()) {
System.out.println("Scale set virtual machine instance #" + instance.instanceId());
System.out.println(instance.id());
PagedList<VirtualMachineScaleSetNetworkInterface> networkInterfaces = instance.listNetworkInterfaces();
// Pick the first NIC
VirtualMachineScaleSetNetworkInterface networkInterface = networkInterfaces.get(0);
for (VirtualMachineScaleSetNicIPConfiguration ipConfig : networkInterface.ipConfigurations().values()) {
if (ipConfig.isPrimary()) {
List<LoadBalancerInboundNatRule> natRules = ipConfig.listAssociatedLoadBalancerInboundNatRules();
for (LoadBalancerInboundNatRule natRule : natRules) {
if (natRule.backendPort() == 22) {
System.out.println("SSH connection string: " + userName + "@" + publicIPAddress.fqdn() + ":" + natRule.frontendPort());
break;
}
}
break;
}
}
}
//=============================================================
// Stop the virtual machine scale set
System.out.println("Stopping virtual machine scale set ...");
virtualMachineScaleSet.powerOff();
System.out.println("Stopped virtual machine scale set");
//=============================================================
// Deallocate the virtual machine scale set
System.out.println("De-allocating virtual machine scale set ...");
virtualMachineScaleSet.deallocate();
System.out.println("De-allocated virtual machine scale set");
//=============================================================
// Update the virtual machine scale set by removing and adding disk
System.out.println("Updating virtual machine scale set managed data disks...");
virtualMachineScaleSet.update().withoutDataDisk(0).withoutDataDisk(200).apply();
System.out.println("Updated virtual machine scale set");
//=============================================================
// Start the virtual machine scale set
System.out.println("Starting virtual machine scale set ...");
virtualMachineScaleSet.start();
System.out.println("Started virtual machine scale set");
//=============================================================
// Update the virtual machine scale set
// - double the no. of virtual machines
System.out.println("Updating virtual machine scale set " + "- double the no. of virtual machines ...");
virtualMachineScaleSet.update().withCapacity(6).apply();
System.out.println("Doubled the no. of virtual machines in " + "the virtual machine scale set");
//=============================================================
// re-start virtual machine scale set
System.out.println("re-starting virtual machine scale set ...");
virtualMachineScaleSet.restart();
System.out.println("re-started virtual machine scale set");
return true;
} catch (Exception f) {
System.out.println(f.getMessage());
f.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().deleteByName(rgName);
System.out.println("Deleted Resource Group: " + rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}
return false;
}
use of com.microsoft.azure.management.compute.VirtualMachineScaleSet in project azure-sdk-for-java by Azure.
the class ManageVirtualMachineScaleSetAsync method runSample.
/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(final Azure azure) {
final Region region = Region.US_WEST_CENTRAL;
final String rgName = SdkContext.randomResourceName("rgCOVS", 15);
final String vnetName = SdkContext.randomResourceName("vnet", 24);
final String loadBalancerName1 = SdkContext.randomResourceName("intlb" + "-", 18);
final String publicIpName = "pip-" + loadBalancerName1;
final String frontendName = loadBalancerName1 + "-FE1";
final String backendPoolName1 = loadBalancerName1 + "-BAP1";
final String backendPoolName2 = loadBalancerName1 + "-BAP2";
final String httpProbe = "httpProbe";
final String httpsProbe = "httpsProbe";
final String httpLoadBalancingRule = "httpRule";
final String httpsLoadBalancingRule = "httpsRule";
final String natPool50XXto22 = "natPool50XXto22";
final String natPool60XXto23 = "natPool60XXto23";
final String vmssName = SdkContext.randomResourceName("vmss", 24);
final String userName = "tirekicker";
final String sshKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD azjava@javalib.com";
final String apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/azure-samples/src/main/resources/install_apache.sh";
final String installCommand = "bash install_apache.sh";
List<String> fileUris = new ArrayList<>();
fileUris.add(apacheInstallScript);
try {
//=============================================================
// Create a virtual network with a frontend subnet
System.out.println("Creating virtual network with a frontend subnet ...");
System.out.println("Creating a public IP address...");
System.out.println("Creating a load balancer");
final List<Indexable> createdResources = new ArrayList<>();
Observable.merge(azure.networks().define(vnetName).withRegion(region).withNewResourceGroup(rgName).withAddressSpace("172.16.0.0/16").defineSubnet("Front-end").withAddressPrefix("172.16.1.0/24").attach().createAsync(), azure.publicIPAddresses().define(publicIpName).withRegion(region).withExistingResourceGroup(rgName).withLeafDomainLabel(publicIpName).createAsync().flatMap(new Func1<Indexable, Observable<Indexable>>() {
@Override
public Observable<Indexable> call(Indexable indexable) {
if (indexable instanceof PublicIPAddress) {
PublicIPAddress publicIp = (PublicIPAddress) indexable;
//=============================================================
// Create an Internet facing load balancer with
// One frontend IP address
// Two backend address pools which contain network interfaces for the virtual
// machines to receive HTTP and HTTPS network traffic from the load balancer
// Two load balancing rules for HTTP and HTTPS to map public ports on the load
// balancer to ports in the backend address pool
// Two probes which contain HTTP and HTTPS health probes used to check availability
// of virtual machines in the backend address pool
// Three inbound NAT rules which contain rules that map a public port on the load
// balancer to a port for a specific virtual machine in the backend address pool
// - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23
System.out.println("Creating a Internet facing load balancer with ...");
System.out.println("- A frontend IP address");
System.out.println("- Two backend address pools which contain network interfaces for the virtual\n" + " machines to receive HTTP and HTTPS network traffic from the load balancer");
System.out.println("- Two load balancing rules for HTTP and HTTPS to map public ports on the load\n" + " balancer to ports in the backend address pool");
System.out.println("- Two probes which contain HTTP and HTTPS health probes used to check availability\n" + " of virtual machines in the backend address pool");
System.out.println("- Two inbound NAT rules which contain rules that map a public port on the load\n" + " balancer to a port for a specific virtual machine in the backend address pool\n" + " - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23");
return Observable.merge(Observable.just(indexable), azure.loadBalancers().define(loadBalancerName1).withRegion(region).withExistingResourceGroup(rgName).definePublicFrontend(frontendName).withExistingPublicIPAddress(publicIp).attach().defineBackend(backendPoolName1).attach().defineBackend(backendPoolName2).attach().defineHttpProbe(httpProbe).withRequestPath("/").withPort(80).attach().defineHttpProbe(httpsProbe).withRequestPath("/").withPort(443).attach().defineLoadBalancingRule(httpLoadBalancingRule).withProtocol(TransportProtocol.TCP).withFrontend(frontendName).withFrontendPort(80).withProbe(httpProbe).withBackend(backendPoolName1).attach().defineLoadBalancingRule(httpsLoadBalancingRule).withProtocol(TransportProtocol.TCP).withFrontend(frontendName).withFrontendPort(443).withProbe(httpsProbe).withBackend(backendPoolName2).attach().defineInboundNatPool(natPool50XXto22).withProtocol(TransportProtocol.TCP).withFrontend(frontendName).withFrontendPortRange(5000, 5099).withBackendPort(22).attach().defineInboundNatPool(natPool60XXto23).withProtocol(TransportProtocol.TCP).withFrontend(frontendName).withFrontendPortRange(6000, 6099).withBackendPort(23).attach().createAsync());
}
return Observable.just(indexable);
}
})).toBlocking().subscribe(new Action1<Indexable>() {
@Override
public void call(Indexable indexable) {
createdResources.add(indexable);
}
});
Network network = null;
PublicIPAddress publicIPAddress = null;
LoadBalancer loadBalancer1 = null;
for (Indexable indexable : createdResources) {
if (indexable instanceof PublicIPAddress) {
publicIPAddress = (PublicIPAddress) indexable;
System.out.println("Created a public IP address");
// Print the virtual network details
Utils.print(publicIPAddress);
} else if (indexable instanceof Network) {
network = (Network) indexable;
System.out.println("Created a virtual network");
// Print the virtual network details
Utils.print(network);
} else if (indexable instanceof LoadBalancer) {
loadBalancer1 = (LoadBalancer) indexable;
// Print load balancer details
System.out.println("Created a load balancer");
Utils.print(loadBalancer1);
}
}
//=============================================================
// Create a virtual machine scale set with three virtual machines
// And, install Apache Web servers on them
System.out.println("Creating virtual machine scale set with three virtual machines" + " in the frontend subnet ...");
final Date t1 = new Date();
VirtualMachineScaleSet virtualMachineScaleSet = (VirtualMachineScaleSet) azure.virtualMachineScaleSets().define(vmssName).withRegion(region).withExistingResourceGroup(rgName).withSku(VirtualMachineScaleSetSkuTypes.STANDARD_D3_V2).withExistingPrimaryNetworkSubnet(network, "Front-end").withExistingPrimaryInternetFacingLoadBalancer(loadBalancer1).withPrimaryInternetFacingLoadBalancerBackends(backendPoolName1, backendPoolName2).withPrimaryInternetFacingLoadBalancerInboundNatPools(natPool50XXto22, natPool60XXto23).withoutPrimaryInternalLoadBalancer().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withSsh(sshKey).withNewDataDisk(100).withNewDataDisk(100, 1, CachingTypes.READ_WRITE).withNewDataDisk(100, 2, CachingTypes.READ_WRITE, StorageAccountTypes.STANDARD_LRS).withCapacity(3).defineNewExtension("CustomScriptForLinux").withPublisher("Microsoft.OSTCExtensions").withType("CustomScriptForLinux").withVersion("1.4").withMinorVersionAutoUpgrade().withPublicSetting("fileUris", fileUris).withPublicSetting("commandToExecute", installCommand).attach().createAsync().map(new Func1<Indexable, Indexable>() {
@Override
public Indexable call(Indexable indexable) {
Date t2 = new Date();
System.out.println("Created a virtual machine scale set with " + "3 Linux VMs & Apache Web servers on them: (took " + ((t2.getTime() - t1.getTime()) / 1000) + " seconds) ");
System.out.println();
return indexable;
}
}).toBlocking().last();
final String pipFqdn = publicIPAddress.fqdn();
//=============================================================
// List virtual machine scale set instance network interfaces and SSH connection string
System.out.println("Listing scale set virtual machine instance network interfaces and SSH connection string...");
virtualMachineScaleSet.virtualMachines().listAsync().map(new Func1<VirtualMachineScaleSetVM, VirtualMachineScaleSetVM>() {
@Override
public VirtualMachineScaleSetVM call(VirtualMachineScaleSetVM instance) {
System.out.println("Scale set virtual machine instance #" + instance.instanceId());
System.out.println(instance.id());
PagedList<VirtualMachineScaleSetNetworkInterface> networkInterfaces = instance.listNetworkInterfaces();
// Pick the first NIC
VirtualMachineScaleSetNetworkInterface networkInterface = networkInterfaces.get(0);
for (VirtualMachineScaleSetNicIPConfiguration ipConfig : networkInterface.ipConfigurations().values()) {
if (ipConfig.isPrimary()) {
List<LoadBalancerInboundNatRule> natRules = ipConfig.listAssociatedLoadBalancerInboundNatRules();
for (LoadBalancerInboundNatRule natRule : natRules) {
if (natRule.backendPort() == 22) {
System.out.println("SSH connection string: " + userName + "@" + pipFqdn + ":" + natRule.frontendPort());
break;
}
}
break;
}
}
return instance;
}
}).toBlocking().subscribe();
//=============================================================
// Stop the virtual machine scale set
System.out.println("Updating virtual machine scale set ...");
// Stop the virtual machine scale set
virtualMachineScaleSet.powerOffAsync().concatWith(virtualMachineScaleSet.deallocateAsync()).concatWith(virtualMachineScaleSet.startAsync()).toObservable().concatWith(virtualMachineScaleSet.update().withCapacity(6).withoutDataDisk(0).withoutDataDisk(200).applyAsync().flatMap(new Func1<VirtualMachineScaleSet, Observable<Void>>() {
@Override
public Observable<Void> call(VirtualMachineScaleSet virtualMachineScaleSet) {
System.out.println("Updated virtual machine scale set");
// Restart the virtual machine scale set
return virtualMachineScaleSet.restartAsync().toObservable();
}
})).toBlocking().subscribe();
return true;
} catch (Exception f) {
System.out.println(f.getMessage());
f.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().deleteByName(rgName);
System.out.println("Deleted Resource Group: " + rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}
return false;
}
Aggregations