使用的k8s client包:
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>2.6.1</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-api</artifactId>
<version>2.3.5</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
使用方式k8sApiUrl为k8s apiserver地址:
Config config = new ConfigBuilder().withMasterUrl(k8sApiUrl).build();
DefaultKubernetesClient kubernetesClient = new DefaultKubernetesClient(config);
然后可以通过kubernetesClent进行RC创建、删除、scale;service创建删除等等操作,另外就是传入执行命令:
// 创建RC
kubernetesClient.replicationControllers().inNamespace(namespace).createOrReplace(rc);
//扩缩容
kubernetesClient.replicationControllers().inNamespace(namespace).withName(
rcName).scale(replicas);
// POD执行命令
kubernetesClient.pods().withName(podName).exec("command1","command2");
更多方法自行挖掘,比如RC、POD、Endpoints 监听......