Java client for Kubernetes and OpenShift providing access to the full Kubernetes & OpenShift REST APIs via a fluent DSL
—
The Fabric8 Kubernetes Client provides flexible configuration options for connecting to Kubernetes clusters. Configuration can be auto-discovered from kubeconfig files, environment variables, or explicitly set through the Config and ConfigBuilder classes.
public class Config {
// Factory methods
public static Config empty();
public static Config autoConfigure(String context);
public static Config fromKubeconfig(String kubeconfigContents);
public static Config fromKubeconfig(File kubeconfigFile);
public static Config fromKubeconfig(String context, File kubeconfig);
public static ConfigBuilder builder();
// Connection settings
public String getMasterUrl();
public String getApiVersion();
public String getNamespace();
// Authentication
public String getUsername();
public String getPassword();
public String getOauthToken();
public String getCaCertFile();
public String getCaCertData();
public String getClientCertFile();
public String getClientCertData();
public String getClientKeyFile();
public String getClientKeyData();
public String getClientKeyAlgo();
public String getClientKeyPassphrase();
// Timeouts and limits
public int getConnectionTimeout();
public int getRequestTimeout();
public int getUploadRequestTimeout();
public int getWatchReconnectInterval();
public int getWatchReconnectLimit();
public long getWebsocketPingInterval();
// Security settings
public Boolean isTrustCerts();
public Boolean isDisableHostnameVerification();
// HTTP settings
public String getHttpProxy();
public String getHttpsProxy();
public String[] getNoProxy();
public String getProxyUsername();
public String getProxyPassword();
// Configuration management
public Config refresh();
public File getFile();
public File getFileWithCluster();
public File getFileWithAuthInfo();
public List<NamedContext> getContexts();
public NamedContext getCurrentContext();
public void setCurrentContext(NamedContext context);
}public class ConfigBuilder {
// Connection configuration
public ConfigBuilder withMasterUrl(String masterUrl);
public ConfigBuilder withApiVersion(String apiVersion);
public ConfigBuilder withNamespace(String namespace);
// Authentication
public ConfigBuilder withUsername(String username);
public ConfigBuilder withPassword(String password);
public ConfigBuilder withOauthToken(String oauthToken);
public ConfigBuilder withCaCertFile(String caCertFile);
public ConfigBuilder withCaCertData(String caCertData);
public ConfigBuilder withClientCertFile(String clientCertFile);
public ConfigBuilder withClientCertData(String clientCertData);
public ConfigBuilder withClientKeyFile(String clientKeyFile);
public ConfigBuilder withClientKeyData(String clientKeyData);
public ConfigBuilder withClientKeyAlgo(String clientKeyAlgo);
public ConfigBuilder withClientKeyPassphrase(String clientKeyPassphrase);
// Timeout configuration
public ConfigBuilder withConnectionTimeout(int connectionTimeout);
public ConfigBuilder withRequestTimeout(int requestTimeout);
public ConfigBuilder withUploadRequestTimeout(int uploadRequestTimeout);
public ConfigBuilder withWatchReconnectInterval(int watchReconnectInterval);
public ConfigBuilder withWatchReconnectLimit(int watchReconnectLimit);
public ConfigBuilder withWebsocketPingInterval(long websocketPingInterval);
// Security configuration
public ConfigBuilder withTrustCerts(Boolean trustCerts);
public ConfigBuilder withDisableHostnameVerification(Boolean disableHostnameVerification);
// HTTP proxy configuration
public ConfigBuilder withHttpProxy(String httpProxy);
public ConfigBuilder withHttpsProxy(String httpsProxy);
public ConfigBuilder withNoProxy(String[] noProxy);
public ConfigBuilder withProxyUsername(String proxyUsername);
public ConfigBuilder withProxyPassword(String proxyPassword);
// Build the configuration
public Config build();
}public class KubernetesClientBuilder {
public KubernetesClientBuilder withConfig(Config config);
public KubernetesClientBuilder withConfig(String config);
public KubernetesClientBuilder withConfig(InputStream config);
public KubernetesClientBuilder withHttpClientFactory(HttpClient.Factory factory);
public KubernetesClientBuilder withTaskExecutor(Executor executor);
public KubernetesClientBuilder withTaskExecutorSupplier(ExecutorSupplier executorSupplier);
public KubernetesClientBuilder withHttpClientBuilderConsumer(Consumer<HttpClient.Builder> consumer);
public KubernetesClientBuilder withKubernetesSerialization(KubernetesSerialization kubernetesSerialization);
public KubernetesClient build();
}The simplest way to create a client using auto-discovered configuration:
try (KubernetesClient client = new KubernetesClientBuilder().build()) {
// Client automatically discovers configuration from:
// 1. System properties
// 2. Environment variables
// 3. ~/.kube/config file
// 4. Service account token (when running in cluster)
String namespace = client.getConfiguration().getNamespace();
System.out.println("Connected to cluster, default namespace: " + namespace);
}Creating a client with explicit configuration:
Config config = new ConfigBuilder()
.withMasterUrl("https://kubernetes.example.com:6443")
.withNamespace("my-namespace")
.withOauthToken("my-token")
.withTrustCerts(true)
.withConnectionTimeout(10000)
.withRequestTimeout(30000)
.build();
try (KubernetesClient client = new KubernetesClientBuilder()
.withConfig(config)
.build()) {
// Use the client with custom configuration
PodList pods = client.pods().list();
}Loading configuration from a specific kubeconfig file:
Config config = Config.fromKubeconfig("/path/to/kubeconfig");
try (KubernetesClient client = new KubernetesClientBuilder()
.withConfig(config)
.build()) {
// Client uses the specified kubeconfig
NodeList nodes = client.nodes().list();
}Configuring client certificate authentication:
Config config = new ConfigBuilder()
.withMasterUrl("https://kubernetes.example.com:6443")
.withCaCertFile("/path/to/ca.crt")
.withClientCertFile("/path/to/client.crt")
.withClientKeyFile("/path/to/client.key")
.withClientKeyPassphrase("key-passphrase")
.build();
try (KubernetesClient client = new KubernetesClientBuilder()
.withConfig(config)
.build()) {
// Client uses certificate authentication
ServiceList services = client.services().list();
}Configuring HTTP proxy settings:
Config config = new ConfigBuilder()
.withMasterUrl("https://kubernetes.example.com:6443")
.withHttpProxy("http://proxy.example.com:8080")
.withHttpsProxy("https://proxy.example.com:8080")
.withNoProxy(new String[]{"localhost", "127.0.0.1", "*.local"})
.withProxyUsername("proxy-user")
.withProxyPassword("proxy-password")
.build();
try (KubernetesClient client = new KubernetesClientBuilder()
.withConfig(config)
.build()) {
// Client uses proxy settings
NamespaceList namespaces = client.namespaces().list();
}Configuring various timeout settings:
Config config = new ConfigBuilder()
.withConnectionTimeout(15000) // 15 seconds to establish connection
.withRequestTimeout(60000) // 60 seconds for regular requests
.withUploadRequestTimeout(300000) // 5 minutes for upload requests
.withWatchReconnectInterval(1000) // 1 second between watch reconnect attempts
.withWatchReconnectLimit(10) // Maximum 10 reconnect attempts
.withWebsocketPingInterval(30000) // 30 seconds between websocket pings
.build();
try (KubernetesClient client = new KubernetesClientBuilder()
.withConfig(config)
.build()) {
// Client uses custom timeout settings
ConfigMapList configMaps = client.configMaps().list();
}The client configuration is auto-discovered from multiple sources in the following order of precedence:
kubernetes.KUBERNETES_MASTER, KUBERNETES_NAMESPACE, etc.~/.kube/config or specified by KUBECONFIG environment variableCommon environment variables for configuration:
KUBERNETES_MASTER - Kubernetes API server URLKUBERNETES_NAMESPACE - Default namespaceKUBERNETES_AUTH_TOKEN - Authentication tokenKUBERNETES_CA_CERTIFICATE_FILE - CA certificate file pathKUBERNETES_CLIENT_CERTIFICATE_FILE - Client certificate file pathKUBERNETES_CLIENT_KEY_FILE - Client key file pathKUBERNETES_TRUST_CERTIFICATES - Trust all certificates (true/false)KUBERNETES_DISABLE_HOSTNAME_VERIFICATION - Disable hostname verification (true/false)Corresponding system properties use the kubernetes. prefix:
kubernetes.master - Kubernetes API server URLkubernetes.namespace - Default namespacekubernetes.auth.token - Authentication tokenkubernetes.certs.ca.file - CA certificate file pathkubernetes.certs.client.file - Client certificate file pathkubernetes.certs.client.key.file - Client key file pathkubernetes.trust.certificates - Trust all certificateskubernetes.disable.hostname.verification - Disable hostname verificationInstall with Tessl CLI
npx tessl i tessl/maven-io-fabric8--kubernetes-client