这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
名称 | 链接 | 备注 |
---|---|---|
项目主页 | 该项目在GitHub上的主页 | |
git仓库地址(https) | 该项目源码的仓库地址,https协议 | |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bolingcavalry</groupId>
<artifactId>kubernetesclient</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.bolingcavalry</groupId>
<artifactId>openapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>openapi</name>
<description>Demo project for openapi client</description>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.0.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
package com.bolingcavalry.openapi;
import com.google.gson.GsonBuilder;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Namespace;
import io.kubernetes.client.openapi.models.V1NamespaceBuilder;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.KubeConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import java.io.FileReader;
@SpringBootApplication
@RestController
@Slf4j
public class OpenAPIDemoApplication {
public static void main(String[] args) {
SpringApplication.run(OpenAPIDemoApplication.class, args);
}
/**
* 默认的全局设置
* @return
* @throws Exception
*/
@PostConstruct
private void setDefaultApiClient() throws Exception {
// 存放K8S的config文件的全路径
String kubeConfigPath = "/Users/zhaoqin/temp/202007/05/config";
// 以config作为入参创建的client对象,可以访问到K8S的API Server
ApiClient client = ClientBuilder
.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath)))
.build();
// 创建操作类
Configuration.setDefaultApiClient(client);
}
@RequestMapping(value = "/openapi/createnamespace/{namespace}", method = RequestMethod.GET)
public V1Namespace createnamespace(@PathVariable("namespace") String namespace) throws Exception {
CoreV1Api coreV1Api = new CoreV1Api();
V1Namespace v1Namespace = new V1NamespaceBuilder()
.withNewMetadata()
.withName(namespace)
.endMetadata()
.build();
V1Namespace ns = coreV1Api.createNamespace(v1Namespace, null, null, null);
// 使用Gson将集合对象序列化成JSON,在日志中打印出来
log.info("ns info \n{}", new GsonBuilder().setPrettyPrinting().create().toJson(ns));
return ns;
}
@RequestMapping(value = "/openapi/pods/{namespace}", method = RequestMethod.GET)
public V1PodList pods(@PathVariable("namespace") String namespace) throws ApiException {
CoreV1Api apiInstance = new CoreV1Api();
// String | If 'true', then the output is pretty printed.
String pretty = null;
// 订阅事件相关的参数,这里用不上
Boolean allowWatchBookmarks = false;
// 连续查找的标志,类似于翻页
String _continue = null;
// 字段选择器
String fieldSelector = "status.phase=Running";
// 根据标签过滤
// String labelSelector = "component=kube-apiserver";
String labelSelector = null;
Integer limit = null;
String resourceVersion = null;
Integer timeoutSeconds = null;
Boolean watch = false;
V1PodList v1PodList = apiInstance.listNamespacedPod(namespace,
pretty,
allowWatchBookmarks,
_continue,
fieldSelector,
labelSelector,
limit,
resourceVersion,
timeoutSeconds,
watch);
// 使用Gson将集合对象序列化成JSON,在日志中打印出来
log.info("pod info \n{}", new GsonBuilder().setPrettyPrinting().create().toJson(v1PodList));
return v1PodList;
}
}
如果您不想自己搭建kubernetes环境,推荐使用腾讯云容器服务TKE:无需自建,即可在腾讯云上使用稳定, 安全,高效,灵活扩展的 Kubernetes 容器平台;
如果您希望自己的镜像可以通过外网上传和下载,推荐腾讯云容器镜像服务TCR:像数据加密存储,大镜像多节点快速分发,跨地域镜像同步
微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。