1
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
Hossein Golshani
  • 1,847
  • 5
  • 16
  • 27
shraddha
  • 853
  • 5
  • 9
  • 20

1 Answers1

2

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();
Rico
  • 58,485
  • 12
  • 111
  • 141
  • Hi Rico, This service creation works perfectly. But when I try to create deployment before service, it creates programatically, but I do not see in kubectl get deployments. Do you know if it is getting successfully created or not? How should I debug that? – shraddha Oct 10 '18 at 19:32
  • the code I am using : `Deployment createdDeployment = builderDeployment .withKind("Deployment") .withNewMetadata().withName("server-proxy").endMetadata() .withNewSpec() .withNewTemplate() .withNewSpec() .addNewContainer().withImage() .addNewPort().withContainerPort(5000).endPort() .endContainer() .endSpec() .endTemplate() .endSpec() .build(); System.out.println("Deployment: " + createdDeployment.toString());` – shraddha Oct 10 '18 at 19:33
  • It might be getting created on a different `namespace`. Can you check `kubectl get deployments --all-namespaces`? – Rico Oct 10 '18 at 19:35
  • I checked, I dont see any deployment with name `server-proxy` – shraddha Oct 10 '18 at 19:39
  • I don't see how your call would have succeeded then. Do you have any client logs? network traces? Can you look at the `kube-apiserver` logs on your cluster? – Rico Oct 10 '18 at 19:50
  • How to associate DeploymentBuilder with kubernetesClient? Is this a valid question? – shraddha Oct 10 '18 at 21:54