k8s client-java创建一个deployment

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上查看的结果
在这里插入图片描述
参考

创建时必要的参数

其他参考

3 java-client通过yaml文件创建

还未实践
参考

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值