Skip to content

3. Code Examples

Brendan Burns edited this page Nov 5, 2025 · 11 revisions

Here's a few code snippets showing how to access Kubernetes cluster using the library:

list all pods:

importio.kubernetes.client.openapi.ApiClient;
importio.kubernetes.client.openapi.ApiException;
importio.kubernetes.client.openapi.Configuration;
importio.kubernetes.client.openapi.apis.CoreV1Api;
importio.kubernetes.client.openapi.models.V1Pod;
importio.kubernetes.client.openapi.models.V1PodList;
importio.kubernetes.client.util.Config;
importjava.io.IOException;
publicclassExample {
publicstaticvoidmain(String[] args) throwsIOException, ApiException{
ApiClientclient = Config.defaultClient();
Configuration.setDefaultApiClient(client);
CoreV1Apiapi = newCoreV1Api();
V1PodListlist = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
for (V1Poditem : list.getItems()) {
System.out.println(item.getMetadata().getName());
}
}
}

watch on namespace object:

importcom.google.gson.reflect.TypeToken;
importio.kubernetes.client.openapi.ApiClient;
importio.kubernetes.client.openapi.ApiException;
importio.kubernetes.client.openapi.Configuration;
importio.kubernetes.client.openapi.apis.CoreV1Api;
importio.kubernetes.client.openapi.models.V1Namespace;
importio.kubernetes.client.util.Config;
importio.kubernetes.client.util.Watch;
importjava.io.IOException;
publicclassWatchExample {
publicstaticvoidmain(String[] args) throwsIOException, ApiException{
ApiClientclient = Config.defaultClient();
Configuration.setDefaultApiClient(client);
CoreV1Apiapi = newCoreV1Api();
Watch<V1Namespace> watch = Watch.createWatch(
client,
api.listNamespaceCall(null, null, null, null, null, 5, null, null, Boolean.TRUE, null, null),
newTypeToken<Watch.Response<V1Namespace>>(){}.getType());
for (Watch.Response<V1Namespace> item : watch) {
System.out.printf("%s : %s%n", item.type, item.object.getMetadata().getName());
}
}
}

More examples can be found in examples folder. To run examples, run this command:

mvn exec:java -Dexec.mainClass="io.kubernetes.client.examples.Example"

Additionally, we prepared a few examples for common use-cases which are shown below:

  • Configuration:
  • Basics:
    • SimpleExample: Simple minimum example of how to use the client.
    • ProtoExample: Request/receive payloads in protobuf serialization protocol.
    • PatchExample: Patch resource objects in various supported patch formats, equal to kubectl patch.
    • FluentExample: Construct arbitrary resource in a fluent builder style.
    • YamlExample: Suggested way to load or dump resource in Yaml.
  • Streaming:
    • WatchExample: Subscribe watch events from certain resources, equal to kubectl get <resource> -w.
    • LogsExample: Fetch logs from running containers, equal to kubectl logs.
    • ExecExample: Establish an "exec" session with running containers, equal to kubectl exec.
    • PortForwardExample: Maps local port to a port on the pod, equal to kubectl port-forward.
    • AttachExample: Attach to a process that is already running inside an existing container, equal to kubectl attach.
    • CopyExample: Copy files and directories to and from containers, equal to kubectl cp.
    • WebSocketsExample: Establish an arbitrary web-socket session to certain resources.
  • Advanced: (NOTE: The following example requires client-java-extended module)

Clone this wiki locally