先导入 k8s 官方 jdk 7.0.0 版本
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>7.0.0</version>
</dependency>
创建 Deployment 操作 简单示例
@Test
public void createNamespacedDeployment(){
AppsV1Api apiInstance = new AppsV1Api(DeploymentTest.getApiClient());
String namespace = "test-namespace"; // namespace
String name = "test-deployment";
//插入 端口暴露服务的 对应的是 Service.Spec.Selector 下的值
Map<String,String> selectLabels = new HashMap<>();
selectLabels.put("select-name","test-deployment");
V1Deployment body = new V1DeploymentBuilder()
.withMetadata(new V1ObjectMetaBuilder()
.withName(name)
.withNamespace(namespace)
.withLabels(selectLabels)
.build())
.withSpec(new V1DeploymentSpecBuilder()
//设置默认副本数
.withReplicas(1)
//设置选择器
.withSelector(new V1LabelSelectorBuilder()
.withMatchLabels(selectLabels)
.build())
//设置docker模板
.withTemplate(new V1PodTemplateSpecBuilder()
.withMetadata(new V1ObjectMetaBuilder()
.withLabels(selectLabels)
.build())
.withSpec(new V1PodSpecBuilder()
.withContainers(new V1ContainerBuilder()
.withName("test-nginx")//设置docker名
.withImage("nginx")//设置 docker镜像名
.withPorts(new V1ContainerPortBuilder()
.withContainerPort(80)//设置内部端口
.withProtocol("TCP")//连接方式
.build())
.build())
.build())
.build())
.build())
.build(); // V1Deployment |
String pretty = null; // 是否会漂亮输出
String dryRun = null; //
String fieldManager = null; //
try {
V1Deployment result = apiInstance.createNamespacedDeployment(namespace, body, pretty, dryRun, fieldManager);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling AppsV1Api#createNamespacedDeployment");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
}
还有
listNamespacedDeployment //根据namespace 查询其下所有 deployment
readNamespacedDeployment //根据namespace 与 name 查询指定的 deployment
deleteNamespacedDeployment//根据namespace 与 name 删除指定deployment
patchNamespacedDeployment//根据namespace 与 name 修复填充新的参数或设置项
replaceNamespaceDeployment//根据 namespce 与 name 替换相关配置
注:以上就不 赘述了 看看 官方 api 文档截止到怎么操作了
重点来了
众所周知 deployment 不管有着以上操作 还有一下内容
- 版本回退
- 弹性伸缩
- 设置卷(物理卷或虚拟卷)
- 重新部署
仔细一看可以发现 官方的 AppsV1Api 中没有相关操作
经过我仔细研究与发现找到 还不是正式版本的Api 锵! 锵 !锵!ExtensionsV1beta1Api 该api虽然还没有正式版但是 该api 下的
ExtensionsV1beta1Deployment中的ExtensionsV1beta1DeploymentSpec有着 相关操作 下面附上官方md

ExtensionsV1beta1DeploymentSpec.jpg
可以看到 这里面就有咱们需要用到的 ,例如:
- 暂停与开始 paused
- 版本回退 rollbackTo
- 升级策略 strategy(滚动更新或替换更新)
其实这个 api 中我觉的最有用的 就是 上面这三个
废话不多说 上代码
@Test
public void replaceNamespaceDeployment() {
//获取对象操作
ExtensionsV1beta1Api apiInstance = new ExtensionsV1beta1Api(DeploymentTest.getApiClient());
String name = "test-deployment";
String namespace = "test-namespace"; // namespace
String storeName = "test-store";//卷名 虚拟卷名
String pretty = null; // String | If 'true', then the output is pretty printed.
Boolean exact = null; // Boolean | Should the export be exact. Exact export maintains cluster-specific fields like 'Namespace'. Deprecated. Planned for removal in 1.18.
Boolean export = null; // Boolean | Should this value be exported. Export strips fields that a user can not specify. Deprecated. Planned for removal in 1.18.
String dryRun = null; // String | When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed
String fieldManager = null; // String | fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint.
try {
//获取需要修改的 deployment 对象
ExtensionsV1beta1Deployment body = apiInstance.readNamespacedDeployment(name, namespace, pretty, exact, export);
//输出 deployment 对象
System.out.println("body = " + body);
//暂停/继续 deployment
// body.getSpec().setPaused(true);
// body.getSpec().setPaused(false);
//添加环境变量
// body.getSpec().getTemplate().getSpec().getContainers().forEach(i->{
// List<V1EnvVar> list = new ArrayList<>();
// list.add(new V1EnvVarBuilder()
// .withName("TEST-ENV")
// .withValue("There is only one truth")//真想只有一个 致敬死神小学生
// .build());
// //添加环境到每一个模板中
// i.setEnv(list);
// });
//重新部署
// body.getSpec().getTemplate().getMetadata().getAnnotations().put("kubesphere.io/restartedAt",new DateTime().toString());
//指定卷
// List<V1Volume> v1Volumes = new ArrayList<>();
// v1Volumes.add(new V1VolumeBuilder()
// //指定虚拟卷
// .withName(storeName)
// //指定实体卷
//// .withEmptyDir()
// .build());
// body.getSpec().getTemplate().getSpec().setVolumes(v1Volumes);
// body.getSpec().getTemplate().getSpec().getContainers().forEach(i->{
// List<V1VolumeMount> v1VolumeMounts = new ArrayList<>();
// v1VolumeMounts.add(new V1VolumeMountBuilder()
// .withName(storeName)
// //挂载路径
// .withMountPath("/date")
// .build());
// i.setVolumeMounts(v1VolumeMounts);
// });
//指定版本回退
body.getSpec().setRollbackTo(new ExtensionsV1beta1RollbackConfigBuilder()
//回滚到指定版本
.withRevision(1L)
.build());
//怎么查看历史版本号请 查看官方文档 有关 AppsV1Api 下的 listNamespaceReplicaSet 方法 具体操作 官方文档中有
ExtensionsV1beta1Deployment result = apiInstance.replaceNamespacedDeployment(name, namespace, body, pretty, dryRun, fieldManager);
//输出修改后的 deployment 对象
System.out.println("result = " + result);
} catch (ApiException e) {
System.err.println("Exception when calling ExtensionsV1beta1Api#readNamespacedDeployment");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
}