通过java 接口 在k8s中创建命名空间

1:k8s版本:1.16.1,centos7 服务器

2:导入k8s依赖

 <dependency>
            <groupId>io.kubernetes</groupId>
            <artifactId>client-java</artifactId>
            <version>5.0.0</version>
            <scope>compile</scope>
        </dependency>

3: 创建连接,需要到k8s安装路径,cd /etc/kubernetes/  拿到用户信息配置文件

4:将文件复制到resources路径下

5:读取文件,连接k8s,创建k8s客户端

public void createConnection() {
        ApiClient client=new ApiClient();
        try {
            client = Config.fromConfig(this.getClass().getResourceAsStream("/admin.conf"));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        Configuration.setDefaultApiClient(client);
        ApiClient defaultClient = Configuration.getDefaultApiClient();
        ApiKeyAuth BearerToken = (ApiKeyAuth) defaultClient.getAuthentication("BearerToken");
        BearerToken.setApiKey("YOUR API KEY");
    }

6:创建命名空间

public Object createNamespace(String name) {
        createConnection();
        CoreV1Api api = new CoreV1Api();
        AppsV1Api appsV1Api=new AppsV1Api();
        V1Namespace body=new V1Namespace();
        V1ObjectMeta meta=new V1ObjectMeta();
        meta.setNamespace(name);
        meta.setName(name);
        body.setMetadata(meta);
//        body.setApiVersion("apps/v1");
//        body.setKind("Deployment");    
        /*V1Deployment body=new V1Deployment();
        body.setApiVersion("apps/v1");
        body.setKind("Deployment");
        V1ObjectMeta meta=new V1ObjectMeta();
        meta.setNamespace(name);
        meta.setName(name);
        body.setMetadata(meta);
        V1DeploymentSpec spec=new V1DeploymentSpec();
        spec.setReplicas(1);
        body.setSpec(spec);*/
        try {
//            appsV1Api.createNamespacedDeployment(name, body, true, null, null);
            V1Namespace result =api.createNamespace(body,true, null, null);
             System.out.println(result);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return body;
    }

 

7:成功

 

8:创建 部署 

通过postman传参 

{
   "namespace": "test",
  "replicas":2,
  "containers":[{"name":"support","image":"registry.cn-beijing.aliyuncs.com/xxxxx:xxxx","imagePullPolicy":"Always"}]
}

后台接口

public Object createdeployment(String namespace,int replicas,List<V1Container> containers) {
        createConnection();
        AppsV1Api appsV1Api=new AppsV1Api();
        V1Deployment body=new V1Deployment();
        body.setApiVersion("apps/v1");
        body.setKind("Deployment");
        V1ObjectMeta meta=new V1ObjectMeta();
        meta.setNamespace(namespace);
        meta.setName(namespace);
        body.setMetadata(meta);
        V1DeploymentSpec spec=new V1DeploymentSpec();
        spec.setReplicas(replicas);
        V1PodTemplateSpec template=new V1PodTemplateSpec();
        V1ObjectMeta v1ObjectMeta=new V1ObjectMeta();
        Map<String, String> map=new HashMap<>();
        map.put("app", "support");
        map.put("version", "version");
        v1ObjectMeta.setLabels(map);
        template.setMetadata(v1ObjectMeta);
        V1PodSpec v1PodSpec=new V1PodSpec();
        List<V1LocalObjectReference> imagePullSecrets=new ArrayList<>();
        V1LocalObjectReference reference=new V1LocalObjectReference();
        reference.setName("regsecret");
        imagePullSecrets.add(reference);
        v1PodSpec.setImagePullSecrets(imagePullSecrets);
//        V1Container v1Container=new V1Container();
//        v1Container.setName(containerName);
//        v1Container.setImage(image);
        v1PodSpec.setContainers(containers);
        template.setSpec(v1PodSpec);
        spec.setTemplate(template);
        V1LabelSelector selector=new V1LabelSelector();
        map=new HashMap<>();
        map.put("app", "support");
        selector.setMatchLabels(map);
        spec.setSelector(selector);
        body.setSpec(spec);
        System.out.println(body);
        try {
            body=appsV1Api.createNamespacedDeployment(namespace, body, true, null, null);
             System.out.println(body);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return true;
    }

 

9:成功

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值