https://github.com/zq2599/blog_demosjava
內容:全部原創文章分類彙總及配套源碼,涉及Java、Docker、Kubernetes、DevOPS等;git
名稱 | 連接 | 備註 |
---|---|---|
項目主頁 | https://github.com/zq2599/blog_demos | 該項目在GitHub上的主頁 |
git倉庫地址(https) | https://github.com/zq2599/blog_demos.git | 該項目源碼的倉庫地址,https協議 |
git倉庫地址(ssh) | git@github.com:zq2599/blog_demos.git | 該項目源碼的倉庫地址,ssh協議 |
名爲OutsideclusterApplication的應用並未部署在K8S環境,該應用可以訪問到K8S環境的關鍵,就是將K8S環境的config文件複製一份,而後放在OutsideclusterApplication可以訪問到的位置:程序員
<?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>outsidecluster</artifactId> <version>0.0.1-SNAPSHOT</version> <name>outsidecluster</name> <description>Demo project for Spring Boot</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.outsidecluster; import com.google.gson.GsonBuilder; import io.kubernetes.client.openapi.ApiClient; import io.kubernetes.client.openapi.Configuration; import io.kubernetes.client.openapi.apis.CoreV1Api; 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.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.io.FileReader; @SpringBootApplication @RestController @Slf4j public class OutsideclusterApplication { public static void main(String[] args) { SpringApplication.run(OutsideclusterApplication.class, args); } @RequestMapping(value = "/hello") public V1PodList hello() 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); CoreV1Api api = new CoreV1Api(); // 調用客戶端API取得全部pod信息 V1PodList v1PodList = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null); // 使用jackson將集合對象序列化成JSON,在日誌中打印出來 log.info("pod info \n{}", new GsonBuilder().setPrettyPrinting().create().toJson(v1PodList)); return v1PodList; } }
運行上述代碼,在瀏覽器訪問http://localhost:8080/hello ,便可取得K8S全部pod的詳情,以下所示(爲了讓返回數據更加整齊美觀,我用的是Firefox瀏覽器):
github
查看控制檯,可見日誌也將詳情打印出來:
web
微信搜索「程序員欣宸」,我是欣宸,期待與您一同暢遊Java世界...
https://github.com/zq2599/blog_demosspring