阅读背景:

如何使用Kubernetes Azure和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.

我得到客户端和节点。现在,我有yaml文件,我想以编程方式部署yaml(创建服务,部署和pod)。

I can do following

我可以做到以下

kubectl create -f pod-sample.yaml 

but I want to do same thing using JAVA SDK.

但我想用JAVA SDK做同样的事情。

I am using following java libraries for kubernetes:

我正在为kubernetes使用以下java库:

io.fabric8.kubernetes

1 个解决方案

#1


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

我相信您可以解析部署定义的YAML或JSON。例如,对于YAML,您可以在此处使用任何Java库

  • JvYaml # Java port of RbYaml
  • JvYaml#RbYaml的Java端口

  • SnakeYAML # Java 5 / YAML 1.1
  • SnakeYAML#Java 5 / YAML 1.1

  • YamlBeans # To/from JavaBeans
  • YamlBeans#来自/来自JavaBeans

  • JYaml # Original Java Implementation
  • JYaml#原始Java实现

  • Camel # YAML 1.2 for Java. A user-friendly OOP library.
  • Camel#YAML 1.2 for Java。用户友好的OOP库。

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

杰克逊似乎更受JSON欢迎,它也支持YAML扩展。

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

分享到: