최신기술/도커 & 쿠버네티스

클라이언트 라이브러리로 Kubernetes 애플리케이션 개발하기

막형 2021. 3. 3. 14:13

API Machinery SIG(special interest group)에서 지원하는 두 개의 쿠버네티스 API 클라이언트 라이브러리가 있습니다.

 

kubernetes-client/python

Official Python client library for kubernetes. Contribute to kubernetes-client/python development by creating an account on GitHub.

github.com

그 외 아래와 같은 사용자 라이브러리도 존재합니다.

 

fabric8io/kubernetes-client

Java client for Kubernetes & OpenShift . Contribute to fabric8io/kubernetes-client development by creating an account on GitHub.

github.com


[Sample Code]

 

Fabric8 쿠버네티스 클라이언트를 사용해 자바 애플리케이션에서 서비스를 열거하는 방법입니다. 

import java.util.Arrays;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;

public class Test {
  public static void main(String[] args) throws Exception {
    KubernetesClient client = new DefaultKubernetesClient();

    // list pods in the default namespace
    PodList pods = client.pods().inNamespace("default").list();
    pods.getItems().stream()
      .forEach(s -> System.out.println("Found pod: " +
               s.getMetadata().getName()));
    
    // create a pod
    System.out.println("Creating a pod");
    Pod pod = client.pods().inNamespace("default")
       .createNew()
       .withNewMetadata()
         .withName("programmatically-created-pod")
       .endMetadata()
       .withNewSpec()
         .addNewContainer()
           .withName("main")
           .withImage("busybox")
           .withCommand(Arrays.asList("sleep", "99999"))
         .endContainer()
.endSpec()
       .done();
      System.out.println("Created pod: " + pod);
    
    // edit the pod (add a label to it)
    client.pods().inNamespace("default")
      .withName("programmatically-created-pod")
      .edit()
      .editMetadata()
.addToLabels("foo", "bar")
      .endMetadata()
      .done();
    System.out.println("Added label foo=bar to pod");

    System.out.println("Waiting 1 minute before deleting pod...");
    Thread.sleep(60000);

    // delete the pod
    client.pods().inNamespace("default")
      .withName("programmatically-created-pod")
      .delete();
    System.out.println("Deleted the pod");
    }
  }

[참고]

Kubernetes in Action

https://github.com/fabric8io/kubernetes-client

 

fabric8io/kubernetes-client

Java client for Kubernetes & OpenShift . Contribute to fabric8io/kubernetes-client development by creating an account on GitHub.

github.com