Java: Manage Azure Container Service (AKS) and more

Gepost op 7 december, 2017

Principal Program Manager, Azure Developer Experience

We released 1.4 of the Azure Management Libraries for Java. This release adds support for Azure Container Service (AKS) and more.

https://github.com/azure/azure-libraries-for-java

Getting started

Add the following dependency fragment to your Maven POM file to use 1.4 version of the libraries:

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure</artifactId>
    <version>1.4.0</version>
</dependency>

Create Kubernetes Cluster in Azure Container Service (AKS)

You can create a Kubernetes cluster by using a define() … create() method chain.

KubernetesCluster kubernetesCluster = azure.kubernetesClusters().define(aksName)
    .withRegion(region)
    .withNewResourceGroup(rgName)
    .withLatestVersion()
    .withRootUsername(rootUserName)
    .withSshKey(sshKeys.getSshPublicKey())
    .withServicePrincipalClientId(servicePrincipalClientId)
    .withServicePrincipalSecret(servicePrincipalSecret)
    .defineAgentPool("agentpool")
        .withVirtualMachineCount(1)
        .withVirtualMachineSize(ContainerServiceVMSizeTypes.STANDARD_D1_V2)
        .attach()
    .withDnsPrefix("dns-" + aksName)
    .create();

You can instantiate a Kubernetes client using a community developed Kubernetes client library.

ernetesClient kubernetesClient = new DefaultKubernetesClient(config);

Deploy from Container Registry to Kubernetes Cluster

You can deploy an image from Azure Container Registry to a Kubernetes cluster using the same community developed Kubernetes client library and an image pull secret associated with the Container Registry.

ReplicationController rc = new ReplicationControllerBuilder()
    .withNewMetadata()
        .withName("acrsample-rc")
        .withNamespace(aksNamespace)
        .addToLabels("acrsample-nginx", "nginx")
        .endMetadata()
    .withNewSpec()
        .withReplicas(2)
        .withNewTemplate()
             .withNewMetadata()
                  .addToLabels("acrsample-nginx", "nginx")
                   .endMetadata()
             .withNewSpec()
                  .addNewImagePullSecret(aksSecretName)
                  .addNewContainer()
                        .withName("acrsample-pod-nginx")
                      .withImage("acrdemo.azurecr.io/samples/acrsample-nginx")
                      .addNewPort()
                          .withContainerPort(80)
                          .endPort()
                      .endContainer()
                  .endSpec()
             .endTemplate()
         .endSpec()
    .build();
kubernetesClient.replicationControllers().inNamespace(aksNamespace).create(rc);

You can find the full sample code to deploy an image from container registry to Kubernetes cluster in Azure Container Service (AKS).

Apply Lock to Virtual Machine

You can create and apply a lock to a virtual machine by using a define() … create() method chain.

lockVirtualMachineRO = azure.managementLocks().define("virtualMachineLockRO")
    .withLockedResource(vm)
    .withLevel(LockLevel.READ_ONLY)
    .create();

You can find the full sample code to manage resource locks.

Create Express Route Circuit Peering

You can create an Express Route Circuit by using a define() … create() method chain.

ExpressRouteCircuit erc = azure.expressRouteCircuits().define(ercName)
    .withRegion(Region.US_NORTH_CENTRAL)
    .withNewResourceGroup(rgName)
    .withServiceProvider("Equinix")
    .withPeeringLocation("Silicon Valley")
    .withBandwidthInMbps(50)
    .withSku(ExpressRouteCircuitSkuType.PREMIUM_METEREDDATA)
    .create();

Then, you can create an Express Route Circuit Peering by using another define() … create() method chain.

erc.peerings().defineAzurePrivatePeering()
    .withPrimaryPeerAddressPrefix("123.0.0.0/30")
    .withSecondaryPeerAddressPrefix("123.0.0.4/30")
    .withVlanId(200)
    .withPeerAsn(100)
    .create();

You can find the full sample code to create and configure Express Route Circuits.

Try it

You can get more samples from our GitHub repo. Give it a try and let us know what you think (via e-mail or comments below).
 
You can find plenty of additional info about Java on Azure at https://docs.microsoft.com/en-us/java/azure/.