Search code examples
javaazurekubernetesazure-sdk

How to deploy an app using Kubernetes Azure and AWS SDK for java


public void runKubernetes() {
    KubernetesCluster k8sCluster = this.getKubernetesCluster("xyz-aks");
    System.out.println("___________________________________________");
    System.out.println("Kubernetes Cluster String: " + k8sCluster.name());

    DefaultKubernetesClient kubeclient = new DefaultKubernetesClient();
    System.out.println("Kube client Master URL :"+kubeclient.getMasterUrl());

    NodeList kubenodes = kubeclient.nodes().list();
    for (Node node : kubenodes.getItems()) {
        System.out.println( node.getKind() + " => " + node.getMetadata().getName() +": " + node.getMetadata().getClusterName());
    }
}

I get Client and nodes. Now, I have yaml file and I want to deploy that yaml (create service, deployment and pods) programatically.

I can do following

kubectl create -f pod-sample.yaml 

but I want to do same thing using JAVA SDK.

I am using following java libraries for kubernetes:

io.fabric8.kubernetes

Solution

  • I believe you can parse the YAML or JSON of the deployment definition. For example, for YAML you can use any of the Java libraries here

    • JvYaml # Java port of RbYaml
    • SnakeYAML # Java 5 / YAML 1.1
    • YamlBeans # To/from JavaBeans
    • JYaml # Original Java Implementation
    • Camel # YAML 1.2 for Java. A user-friendly OOP library.

    Jackson seems to be the more popular for JSON which also supports a YAML extension.

    Then once you parse say the name, for example to create a service:

    Service myservice = client.services().inNamespace(parsedNamespaceStr).createNew()
                         .withNewMetadata()
                           .withName(parsedServiceName)
                           .addToLabels(parsedLabel1, parseLabel2)
                         .endMetadata()
                         .done();