pom.xml添加
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>10.0.1</version>
<scope>compile</scope>
</dependency>
/**
* 方式一:默认方式,从系统配置 $HOME/.kube/config 读取配置文件连接 Kubernetes 集群
*/
public static void connectFromSystemConfig() throws IOException {
// 设置默认客户端配置
ApiClient apiClient = Config.defaultClient();
// 设置默认 Api 客户端到配置
Configuration.setDefaultApiClient(apiClient);
}
/**
* 方式二:从指定文件读取配置文件连接 Kubernetes 集群
*/
public static void connectFromConfig() throws IOException {
// 从文件读取配置文件
ApiClient apiClient = Config.fromConfig("d:/config");
// 设置默认 Api 客户端到配置
Configuration.setDefaultApiClient(apiClient);
}
/**
* 方式三:Spring 中利用 @Value 读取 Resources 配置
*/
public static void connectFromResourcesConfig() throws IOException {
// 输入流从 resources 中读取 config
InputStream in = new FileInputStream(new File("src/main/resources/config"));
// 配置客户端
ApiClient apiClient = Config.fromConfig(in);
// 设置默认 Api 客户端到配置
Configuration.setDefaultApiClient(apiClient);
}
/**
* 方式四:通过 Token 连接 Kubernetes 集群
*/
public static void connectFromToken() {
/**创建默认 Api 客户端**/
// 定义连接集群的 Token
String token = "eyJhbGciOiJSUzI1NiIsImtpZCI6IkllX0lZNlR3eGpycVFPclh5dnlhd3lzS0MzZnV3WnhKeHNTUHl1WmdCeXcifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJvYXJkLXVzZXItdG9rZW4tcHp2eG4iLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoia3Vib2FyZC11c2VyIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiOGVmNjk4NWEtZmE3My00M2I0LTg2MDEtMDk1Mzk2MmE0MWNiIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmt1Ym9hcmQtdXNlciJ9.y9B2kL-wJauHF8GyI7HxeqNScI9nrJAJyeIzXyDxdLw2brfaFS4LBj3gZ28q7IWeZxzR9qKZ9ofjXAw-prTNHq-kL2_DlwCzhMp2kUVSKLYiREJVbZlJlFpw4bcobOQMgd7LXAj45RgZ7h7o_fUTpr3uqimu_euQSDmLAd3roiwahorf2tTW4WqivtSE0iXark0dszHoeB0Q8ZhJvUSXXSVrz3VzbA0CNQHOXxgtSoLxewtmvvLwEqaZDpqCN6lFT1_MZo83EHFQIbpiJhLsDyDlmNgxj3IplrXovqtTBIrjNQwuws4eleqTOTxu0IQJCOfTHfP-fnTxdHIWXL537A";
// 定义 Kubernetes 集群地址
String url = "http://192.168.111.64:6443";
// 配置客户端
ApiClient apiClient = Config.fromToken(url, token, false);
// 设置默认 Api 客户端到配置
Configuration.setDefaultApiClient(apiClient);
}
/**
* 方式五:读取配置文件,通过 Token 连接 Kubernetes 集群
*
* 先创建一个 auth.properties 文件,然后里面设置两个参数:
* - (1)、url:kubernentes 集群地址
* - (2)、token:连接 kubernetes 的 token
*/
public static void connectFromTokenByFile() throws IOException {
/**创建默认 Api 客户端**/
Properties properties = new Properties();
// 加载properties文件
InputStream is = CloudDeploymentController.class.getClassLoader().getResourceAsStream("auth.properties");
// 加载输入流
properties.load(is);
// 读取token
properties.getProperty("token");
// 获取 Url 参数并定义 Kubernetes 集群地址
String url = properties.getProperty("url");
// 获取 Token 参数并定义连接集群的 Token
String token = properties.getProperty("token");
// 配置客户端
ApiClient apiClient = Config.fromToken(url, token, false);
// 设置默认 Api 客户端到配置
Configuration.setDefaultApiClient(apiClient);
}