Engin Diri
_CLOUD

_CLOUD

Pulumi Java: Deploying a Minecraft Server on Azure AKS

Pulumi Java: Deploying a Minecraft Server on Azure AKS

Engin Diri's photo
Engin Diri
·May 4, 2022·

2 min read

Table of contents

  • TL;DR Code
  • Introduction
  • Create your Pulumi program
  • Deploy your application
  • Play the game
  • Destroy your application

TL;DR Code

Here you go:

Introduction

This is a very short introduction to the new Pulumi Java language support. Pulumi supports writing your infrastructure as code using the Java language. Java 11 or later is required.

We are going to create an AKS cluster and deploy a Minecraft server via the use of a Helm chart. All in Pulumi and all in Java.

Create your Pulumi program

We use the new java template to create the scaffolding for our application.

pulumi new java  --force

# It may be necessary to install the java language plugin
pulumi plugin install language java

Add the java dependency for Azure and Kubernetes to your projects pom.xml file:

...
<dependencies>
    <dependency>
        <groupId>com.pulumi</groupId>
        <artifactId>pulumi</artifactId>
        <version>0.1.0</version>
    </dependency>
    <dependency>
        <groupId>com.pulumi</groupId>
        <artifactId>azure-native</artifactId>
        <version>1.63.0</version>
    </dependency>
    <dependency>
        <groupId>com.pulumi</groupId>
        <artifactId>azuread</artifactId>
        <version>5.21.0</version>
    </dependency>
    <dependency>
        <groupId>com.pulumi</groupId>
        <artifactId>kubernetes</artifactId>
        <version>3.18.3</version>
    </dependency>
</dependencies>
        ...

You can find the details about the providers in the registry documentation:

Here are some snippets of the code, you can find the full code in the repo link on the top of this article:

package minecraft;

import com.pulumi.Pulumi;
import com.pulumi.azuread.*;
import com.pulumi.azurenative.containerservice.ContainerserviceFunctions;
import com.pulumi.azurenative.containerservice.ManagedCluster;
....

    public static void main(String[] args) {
        Pulumi.run(ctx -> {
            ResourceGroup minecraftGroup = new ResourceGroup("minecraft-java-resource-group",
                    ResourceGroupArgs.builder()
                            .resourceGroupName("minecraft-java-rg")
                            .location("westeurope")
                            .build());

            Application application = new Application("minecraft-app",
                    ApplicationArgs.builder()
                            .displayName("minecraft").build());

            ServicePrincipal servicePrincipal = new ServicePrincipal("minecraft-java-sp",
                    ServicePrincipalArgs.builder()
                            .applicationId(application.applicationId()).build());

            ServicePrincipalPassword ServicePrincipalPassword = new ServicePrincipalPassword("minecraft-java-sp-pw",
                    ServicePrincipalPasswordArgs.builder()
                            .servicePrincipalId(servicePrincipal.getId())
                            .endDate("2099-01-01T00:00:00Z").build());


            ManagedCluster mc = new ManagedCluster("minecraft-aks",
                    ManagedClusterArgs.builder()
                            .kubernetesVersion("1.23.5")
                            .location(minecraftGroup.location())
                            .resourceGroupName(minecraftGroup.name())
                            .resourceName("minecraft-aks")
                            .nodeResourceGroup("minecraft-aks-nodes")
                            .dnsPrefix(minecraftGroup.name())
                            .enableRBAC(true)
                            .servicePrincipalProfile(ManagedClusterServicePrincipalProfileArgs.builder()
                                    .clientId(application.applicationId())
                                    .secret(ServicePrincipalPassword.value()).build())
                            .agentPoolProfiles(new ManagedClusterAgentPoolProfileArgs[]
                                    {ManagedClusterAgentPoolProfileArgs.builder()
                                            .count(3)
                                            .vmSize("Standard_B2ms")
                                            .osType("Linux")
                                            .osDiskSizeGB(30)
                                            .mode("System")
                                            .name("agentpool").build()}).build());
...

            ctx.export("kubeconfig", ctx.output(kubeconfig).asSecret());

            Provider provider = new Provider("minecraft-k8s-provider",
                    ProviderArgs.builder()
                            .kubeconfig(kubeconfig).build(),
                    CustomResourceOptions.builder()
                            .parent(mc).build());

            new Release("minecraft",
                    ReleaseArgs.builder()
                            .chart("minecraft")
                            .namespace("minecraft")
                            .createNamespace(true)
                            .version("3.10.0")
                            .valueYamlFiles(new com.pulumi.asset.FileAsset("./values.yaml"))
                            .repositoryOpts(RepositoryOptsArgs.builder()
                                    .repo("https://itzg.github.io/minecraft-server-charts").build()).build(),
                    CustomResourceOptions.builder()
                            .provider(provider)
                            .parent(provider).build());

        });
    }
}

Deploy your application

Now you can run our program via the call:

pulumi up
...

Previewing update (dev)

View Live: https://app.pulumi.com/dirien/pulumi-java-azure-kubernetes/dev/previews/0b6ffbd0-9311-482b-9834-5039bab277df

     Type                                             Name                              Plan       
 +   pulumi:pulumi:Stack                              pulumi-java-azure-kubernetes-dev  create     
 +   ├─ azuread:index:Application                     minecraft-app                     create     
 +   ├─ azure-native:resources:ResourceGroup          minecraft-java-resource-group     create     
 +   ├─ azuread:index:ServicePrincipal                minecraft-java-sp                 create     
 +   ├─ azuread:index:ServicePrincipalPassword        minecraft-java-sp-pw              create     
 +   └─ azure-native:containerservice:ManagedCluster  minecraft-aks                     create     
 +      └─ pulumi:providers:kubernetes                minecraft-k8s-provider            create     
 +         └─ kubernetes:helm.sh/v3:Release           minecraft                         create     

Resources:
    + 8 to create

image.png

The kubeconfig you can export via the commmand:

pulumi stack output kubeconfig --show-secrets  > kubeconfig.yaml

Play the game

Check for the external ip and use this to enter in your Minecraft client:

image.png

Have fun!

image.png

Destroy your application

Of course we going to destroy our application, with the following command:

pulumi destroy
 
Share this