Kubernetes fabric8 java client - Wed, Feb 10, 2021
Using the fabric8 java client
Using the fabric8 java client
Since Java is the programming language I know best it appeared naturally to search for a Java Client for Kubernetes. This post contains a basic description for using the Java client from fabric8 . The use case is using the client form outside of the cluster.
Setup
Actually creating a client is straight forward if the client is used inside of a cluster where the default configuration fits:
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
...
KubernetesClient newClient(String namespace) {
return new DefaultKubernetesClient().inNamespace(namespace);
}
...
The default configuration lets the client connect to the standard address of the cluster https://kubernetes.default.svc
. It might be necessary to create a service account for the Pod and provide the appropriate permissions to access the API server. See here
for a detailed description on how to do that.
The client offers a very nice fluent DSL
for accessing or managing the corresponding resources inside a cluster.
Configuration options
As I used the client outside of the cluster, the configuration needed to be tweaked in order to get access to the cluster. There are a few dozen configuration options. Most of them are explained in the documentation.
For the complete list, a look at the class Config
might be helpful.
Most of these configuration options can either be set by environment variables or using system properties.
In order to use these, you have to create the config first and then configure it with these options. The Config class offers a helper method for that:
...
Config config = new ConfigBuilder().withMasterUrl(masterUrl)
.withOauthToken(System.getProperty("k8s.master.token"))
.build();
Config.configFromSysPropsOrEnvVars(config);
KubernetesClient client = new DefaultKubernetesClient(config);
...
In aboves code I create a configuration with the URL of the API server and a Oauth-Token. Then I call the configFromSysPropsOrEnvVars
to process possible environment settings.
Using it behind a proxy
Unfortunately the frabric8 client does not honor standard java proxy configuration. Setting system properties like http.proxyHost
or http.nonProxyHosts
as described here
have no effect.
Instead customer system properties http.proxy
and no.proxy
need to be set (see Config
class as well).
Conclusion
The frabric8 client is a nice java client for Kubernetes although a little bit hard to configure.