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:成功