CARVIEW |
Select Language
HTTP/2 200
date: Sun, 27 Jul 2025 06:23:59 GMT
content-type: text/html; charset=utf-8
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
etag: W/"ad48a345812398ac161d8d73896f6c2b"
cache-control: max-age=0, private, must-revalidate
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: no-referrer-when-downgrade
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
content-encoding: gzip
accept-ranges: bytes
set-cookie: _gh_sess=S4toK8HOrJdsRziVK9okoWp9Yzb9wpjFd8M1BMWKLeQLfKLFqvohR3JdnHrLisMYHZDiwNRZiDvEofnIXA8WuC5ZqOHgxNxhYVgSM9eS0JOV9IC4S321jnnUOCj505Y%2FsS6Ot20w8f3Tbu1foHDGWR5PwBO02%2BZiqL7qGJQNB2CdJZxOEt5w7SO9XThXgzOatTs%2FjU4%2FmPJY%2FMCoeqU9wUsox4ZAgZ0Ew961bOjc%2FbEHbv6IuYaIIdxQWStBkIFtzySuPyx9P0Tgg79pxGGANA%3D%3D--mQwjCrnaAsgobByT--jBSZ6szNIef6qT5ZXdg7MQ%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.1363313502.1753597438; Path=/; Domain=github.com; Expires=Mon, 27 Jul 2026 06:23:58 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Mon, 27 Jul 2026 06:23:58 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: AE58:1A1F38:B79E17:F3EF06:6885C5FE
3. Code Examples · kubernetes-client/java Wiki · GitHub
Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 2k
3. Code Examples
Min Jin edited this page Jul 17, 2023
·
10 revisions
Here's a few code snippets showing how to access Kubernetes cluster using the library:
list all pods:
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.util.Config;
import java.io.IOException;
public class Example {
public static void main(String[] args) throws IOException, ApiException{
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();
V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
for (V1Pod item : list.getItems()) {
System.out.println(item.getMetadata().getName());
}
}
}
watch on namespace object:
import com.google.gson.reflect.TypeToken;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Namespace;
import io.kubernetes.client.util.Config;
import io.kubernetes.client.util.Watch;
import java.io.IOException;
public class WatchExample {
public static void main(String[] args) throws IOException, ApiException{
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();
Watch<V1Namespace> watch = Watch.createWatch(
client,
api.listNamespaceCall(null, null, null, null, null, 5, null, null, Boolean.TRUE, null, null),
new TypeToken<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:
- InClusterClientExample: Configure a client while running inside the Kubernetes cluster.
- KubeConfigFileClientExample: Configure a client to access a Kubernetes cluster from outside.
-
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.
-
WatchExample:
Subscribe watch events from certain resources, equal to
-
Advanced: (NOTE: The following example requires
client-java-extended
module)- (5.0.0+) InformerExample: Build an informer which list-watches resources and reflects the notifications to a local cache.
- (5.0.0+) PagerExample: Support Pagination (only for the list request) to ease server-side loads/network congestion.
- (6.0.0+) ControllerExample: Build a controller reconciling the state of world by list-watching one or multiple resources.
- (6.0.0+) LeaderElectionExample: Leader election utilities to help implement HA controllers.
- (9.0.0+) SpringIntegrationControllerExample: Building a kubernetes controller based on spring framework's bean injection, see additional documentation here.
- (9.0.0+) GenericKubernetesClientExample: Construct a generic client interface for any kubernetes types, including CRDs.
- (12.0.0+) DynamicKubernetesClientExample: Manipulating either native Kubernetes types or custom resources without generating classes.
Clone this wiki locally
You can’t perform that action at this time.