k8s client-java创建一个deployment
yaml创建参考client-java创建@Datapublic class DeploymentDTO {private String namespace;private String deploymentName;private Integer replicas;private String metadataLabelsApp;private String image;private Stri
·
1 yaml创建
2 client-java创建
本质上就是用代码创建一个包含了yaml中必要的参数的对象调用接口来进行创建。
使用AppsV1Api apiInstance = new AppsV1Api()下的createNamespacedDeployment接口
DeploymentDTO.java
@Data
public class DeploymentDTO {
private String namespace;
private String deploymentName;
private Integer replicas;
private String metadataLabelsApp;
private String image;
private String portName;
private Integer containerPort;
}
初始化工具类 K8sInit.java
@Component
public class K8sInit {
public ApiClient getConnection() {
ApiClient client = new ClientBuilder().
setBasePath(https://192.168.2.3:6443). // 自己的地址
setVerifyingSsl(false).
setAuthentication(new AccessTokenAuthentication(token)). // 自己去获取token,通过安装kuboard或dashboard可以获取
build();
Configuration.setDefaultApiClient(client);
return client;
}
}
DeploymentController.java
(这个代码歪歪扭扭的风格是为了好看各个层次的参数哈哈哈哈)
/**
* 创建一个deployment
* @param deploymentDTO
* @return
*/
@PostMapping("/createNamespacedDeployment")
public ResultUtil createNamespacedDeployment(@RequestBody DeploymentDTO deploymentDTO) {
Gson gson = new Gson();
AppsV1Api apiInstance = new AppsV1Api(k8sInit.getConnection());
V1Deployment result;
// labels
Map<String,String> matchLabels = new HashMap<>();
matchLabels.put("app", deploymentDTO.getMetadataLabelsApp());
// ports
List<V1ContainerPort> portList = new ArrayList<>();
V1ContainerPort port = new V1ContainerPort();
port.setName(deploymentDTO.getPortName());
port.setContainerPort(81);
portList.add(port);
// 使用对象封装deployment
V1Deployment body = new V1DeploymentBuilder()
.withApiVersion("apps/v1")
.withKind("Deployment")
.withNewMetadata()
.withName(deploymentDTO.getDeploymentName())
.withNamespace(deploymentDTO.getNamespace())
.endMetadata()
.withNewSpec()
.withReplicas(deploymentDTO.getReplicas())
.withNewSelector()
.withMatchLabels(matchLabels)
.endSelector()
.withNewTemplate()
.withNewMetadata()
.withLabels(matchLabels)
.endMetadata()
.withNewSpec()
.withContainers(
new V1Container()
.name(deploymentDTO.getMetadataLabelsApp())
.image(deploymentDTO.getImage())
.imagePullPolicy("IfNotPresent")
.ports(portList)
)
.endSpec()
.endTemplate()
.endSpec()
.build();
try {
result = apiInstance.createNamespacedDeployment(
deploymentDTO.getNamespace(),
body,
"true",
null,
null);
System.out.println(result);
} catch (ApiException e) {
return ResultUtil.error(String.valueOf(e.getCode()), e.getMessage());
}
return ResultUtil.success(gson.toJson(result));
}
postman请求
地址
POST http://localhost:8080/api/v1/deployment/createNamespacedDeployment/
参数
{
"namespace": "default",
"deploymentName": "create-deployment-test",
"replicas": 1,
"metadataLabelsApp": "nginx-test",
"image": "nginx:1.7.9",
"portName": "httpd",
"containerPort": 81
}
kuboard上查看的结果
参考
创建时必要的参数
其他参考
- kubernetes中使用java-client完成deployment的部署以及删除
- 使用Kubernetes的java-client实现Deployment的部署及更新操作
- Kubernetes client-java方式创建命名空间,创建pod容器,创建serice服务等功能
- 使用 Java 操作 Kubernetes API
- 汇总了k8s的api,以及部分范例
- 有kubernetes-client/java和fabric8io/kubernetes-client对比
- 该博客中关于deployment的api ExtensionsV1beta1Api 在官方文档中没有,在编程时可以调用,会报错。可以用AppsV1Api下的api
- 其他内容
3 java-client通过yaml文件创建
还未实践
参考
更多推荐
已为社区贡献6条内容
所有评论(0)