title | description | author | manager | ms.assetid | ms.devlang | ms.topic | ms.service | ms.date | ms.author | ms.reviewer |
---|---|---|---|---|---|---|---|---|---|---|
Manage virtual machine scale sets with Java | Microsoft Docs |
Sample code to manage Azure virtual machine scale sets using the Azure SDK for Java |
rloutlaw |
douge |
b55923b7-d60a-460d-b77c-af5fac67f1cc |
java |
article |
azure |
3/30/2017 |
brendm |
asirveda |
This sample creates a virtual machine scale set using the Java management libraries.
Create an authentication file and set an environment variable AZURE_AUTH_LOCATION
with the full path to the file on your computer. Then run:
git clone https://github.com/Azure-Samples/compute-java-manage-virtual-machine-scale-sets.git
cd compute-java-manage-virtual-machine-scale-sets
mvn clean compile exec:java
View the complete code sample on GitHub.
[!INCLUDE auth-include]
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();
Set up a virtual network and load balancer before creating the scale set definition. The scale set uses these resources for its initial configuration.
LoadBalancer loadBalancer1 = azure.loadBalancers().define(loadBalancerName1)
.withRegion(region)
.withExistingResourceGroup(rgName)
// assign a public IP address to the load balancer
.definePublicFrontend(frontendName)
.withExistingPublicIPAddress(publicIPAddress)
.attach()
// Add two backend address pools
.defineBackend(backendPoolName1)
.attach()
.defineBackend(backendPoolName2)
.attach()
// Add two health probes on 80 and 443
.defineHttpProbe(httpProbe)
.withRequestPath("/")
.withPort(80)
.attach()
.defineHttpProbe(httpsProbe)
.withRequestPath("/")
.withPort(443)
.attach()
// balance HTTP and HTTPs traffic
.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()
// Add NAT definitions to enable SSH and telnet to the VMs
.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();
The load balancer defines two backend network address pools-one to balance load across HTTP (backendPoolName1
) and the other to balance load across HTTPS (backendPoolName2
). The defineHttpProbe()
methods set up health probe endpoints on the load balancers. NAT rules expose ports 22 and 23 on the scale set virtual machines for telnet and SSH access.
// Create a virtual machine scale set with three virtual machines
// And, install Apache Web servers on them
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)
// Use a VM extension to install Apache Web servers
.defineNewExtension("CustomScriptForLinux")
.withPublisher("Microsoft.OSTCExtensions")
.withType("CustomScriptForLinux")
.withVersion("1.4")
.withMinorVersionAutoUpgrade()
.withPublicSetting("fileUris", fileUris)
.withPublicSetting("commandToExecute", installCommand)
.attach()
.create();
Use the virtual network definition and load balancer definitions created in the previous step to create a scale set with three Linux instances (withCapacity(3)
) and three 100GB data disks each. The defineNewExtension()
method chain installs the Apache web server on each VM.
// List network interfaces on the scale set and iterate through them
PagedList<VirtualMachineScaleSetNetworkInterface> vmssNics =
virtualMachineScaleSet.listNetworkInterfaces();
for (VirtualMachineScaleSetNetworkInterface vmssNic : vmssNics) {
System.out.println(vmssNic.id());
}
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 on the instance and use its primary IP address
VirtualMachineScaleSetNetworkInterface networkInterface = networkInterfaces.get(0);
for (VirtualMachineScaleSetNicIPConfiguration ipConfig : networkInterface.ipConfigurations().values()) {
if (ipConfig.isPrimary()) {
List<LoadBalancerInboundNatRule> natRules = ipConfig.listAssociatedLoadBalancerInboundNatRules();
for (LoadBalancerInboundNatRule natRule : natRules) {
// find rule matching the inbound SSH port on the backend for the IP address
// and return the SSH connection string to that port on the load balancer
if (natRule.backendPort() == 22) {
System.out.println("SSH connection string: " + userName +
"@" + publicIPAddress.fqdn() + ":" + natRule.frontendPort());
break;
}
}
break;
}
}
}
The NAT pool created earlier mapped the SSH and telnet ports (22 and 23, respectively) on the virtual machines to ports on the load balancer. This code builds the SSH connection string for each virtual machine.
// stop (not deallocate) all scale set instances
virtualMachineScaleSet.powerOff();
Stopped virtual machines continue to consume reserved resources. Use deallocate()
to stop the operating system on the virtual machines and release their compute resources.
// deallocate the virtual machine scale set
virtualMachineScaleSet.deallocate();
Deallocate() shuts down the operating system on the virtual machines and frees up the compute and network resources (such as IP addresses) used by the scale set instances. You continue to accrue storage charges for any disks (including the OS) attached to the virtual machines.
// start a deallocated or stopped virtual machine scale set
virtualMachineScaleSet.start();
// increase the number of virtual machine scale set instances from three to six
virtualMachineScaleSet.update()
.withCapacity(6)
.apply();
Scale the number of virtual machines in the scale set using withCapacity()
and scale the capacity of each virtual machine using withSku()
.
The sample code first creates a virtual network for the scale set to communicate across and a load balancer to distribute traffic across the virtual machines. The azure.virtualMachineScaleSets().define()...create()
method chain creates the scale set with three Linux instances running the Apache web server.
Class used in sample | Notes |
---|---|
VirtualMachineScaleSet | Query, start, stop, update and delete all virtual machines in the scale set. |
VirtualMachineScaleSetVM | Retrieved from virtualMachineScaleSet.virtualMachines().get() or list() , allows you to query, start, stop, configure and delete virtual machines in the scale set. |
VirtualMachineScaleSetNetworkInterface | Returned from virtualMachineScaleSet.listNetworkInterfaces() , read-only representation of a network interface on a virtual machine in a scale set. |
VirtualMachineScaleSetSkuTypes | Class of static fields used to set the virtual machine scale set tier used to define how much resources scale set members can consume. |
VirtualMachineScaleSetNicIpConfiguration | Used to query the IP configuration associated with a network interface on a scale set virtual machine. |
[!INCLUDE next-steps]