
Kubernetes API 向けの Java クライアントライブラリで、認証、watch、exec、port-forward 操作をサポートし、クラスター、ポッド、デプロイメント、その他のリソースのプログラムによる管理を可能にします。
Kubernetes API の Java クライアント。
Java クライアントは Semantic Versioning を使用しています。メジャーバージョン番号は、新しい Kubernetes リリースバージョン(以下の表参照)のためにクライアントを再生成するたびに増加します。その際、新しい API が追加され、生成された Kubernetes API スタブに破壊的な変更が加えられる可能性があります。メジャーバージョンアップ時は、破壊的な変更に備えてください。
Java クライアントライブラリをローカルの Maven リポジトリにインストールするには、次のコマンドを実行します。
git clone --recursive https://github.com/kubernetes-client/java
cd java
mvn install
詳細は公式ドキュメントを参照してください。
プロジェクトの POM に次の依存関係を追加します。
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>8.0.2</version>
</dependency>
compile 'io.kubernetes:client-java:8.0.2'
まず、次のコマンドを実行して JAR を生成します。
git clone --recursive https://github.com/kubernetes-client/java
cd java/kubernetes
mvn package
その後、以下の JAR を手動でインストールします。
一般的なユースケース向けにいくつかの例を用意しました。以下に示します。
kubectl patch と同等。kubectl get <resource> -w と同等。kubectl logs と同等。kubectl exec と同等。kubectl port-forward と同等。kubectl attach と同等。kubectl cp と同等。client-java-extended モジュールが必要です)
すべてのポッドを一覧表示:
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.util.Config;
import java.io.IOException;
public class Example {
public static void main(String[] args) throws IOException, ApiException{
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();
V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
for (V1Pod item : list.getItems()) {
System.out.println(item.getMetadata().getName());
}
}
}
namespace オブジェクトの watch:
import com.google.gson.reflect.TypeToken;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Namespace;
import io.kubernetes.client.util.Config;
import io.kubernetes.client.util.Watch;
import java.io.IOException;
public class WatchExample {
public static void main(String[] args) throws IOException, ApiException{
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();