简体   繁体   中英

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM