Top Related Projects
Java client for Kubernetes & OpenShift
Official Python client library for kubernetes
Javascript client
Officially supported dotnet Kubernetes Client library
Quick Overview
The kubernetes-client/java repository is the official Java client library for Kubernetes. It provides a set of tools and APIs to interact with Kubernetes clusters programmatically, allowing developers to manage and automate Kubernetes resources using Java applications.
Pros
- Official and well-maintained library supported by the Kubernetes community
- Comprehensive coverage of Kubernetes API features
- Supports both synchronous and asynchronous operations
- Provides fluent interfaces for easier resource management
Cons
- Steep learning curve for developers new to Kubernetes
- Large dependency footprint
- Version compatibility issues may arise with rapid Kubernetes updates
- Documentation can be sparse for some advanced use cases
Code Examples
- Creating a Deployment:
ApiClient client = Config.defaultClient();
AppsV1Api appsApi = new AppsV1Api(client);
V1Deployment deployment = new V1Deployment()
.metadata(new V1ObjectMeta().name("nginx-deployment"))
.spec(new V1DeploymentSpec()
.replicas(3)
.selector(new V1LabelSelector().matchLabels(Collections.singletonMap("app", "nginx")))
.template(new V1PodTemplateSpec()
.metadata(new V1ObjectMeta().labels(Collections.singletonMap("app", "nginx")))
.spec(new V1PodSpec()
.containers(Collections.singletonList(new V1Container()
.name("nginx")
.image("nginx:1.14.2")
.ports(Collections.singletonList(new V1ContainerPort().containerPort(80))))))));
appsApi.createNamespacedDeployment("default", deployment, null, null, null);
- Listing Pods:
ApiClient client = Config.defaultClient();
CoreV1Api api = new CoreV1Api(client);
V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
for (V1Pod item : list.getItems()) {
System.out.println(item.getMetadata().getName());
}
- Watching for Pod events:
ApiClient client = Config.defaultClient();
CoreV1Api api = new CoreV1Api(client);
Watch<V1Pod> watch = Watch.createWatch(
client,
api.listPodForAllNamespacesCall(null, null, null, null, null, null, null, null, 10, Boolean.TRUE, null),
new TypeToken<Watch.Response<V1Pod>>(){}.getType());
for (Watch.Response<V1Pod> item : watch) {
System.out.printf("%s : %s%n", item.type, item.object.getMetadata().getName());
}
Getting Started
- Add the dependency to your project:
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>15.0.1</version>
</dependency>
- Create a simple client:
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.util.Config;
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
- Use the API classes to interact with the Kubernetes cluster:
import io.kubernetes.client.openapi.apis.CoreV1Api;
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());
}
Competitor Comparisons
Java client for Kubernetes & OpenShift
Pros of kubernetes-client
- More comprehensive API coverage, including support for Custom Resource Definitions (CRDs)
- Better documentation and examples
- More active community and frequent updates
Cons of kubernetes-client
- Steeper learning curve due to more complex API
- Larger dependency footprint
- May be overkill for simple use cases
Code Comparison
kubernetes-client:
KubernetesClient client = new DefaultKubernetesClient();
Pod pod = client.pods().inNamespace("default").create(new PodBuilder()
.withNewMetadata().withName("example").endMetadata()
.withNewSpec()
.addNewContainer().withName("example").withImage("nginx").endContainer()
.endSpec()
.build());
kubernetes-client/java:
ApiClient client = Config.defaultClient();
CoreV1Api api = new CoreV1Api(client);
V1Pod pod = new V1Pod()
.metadata(new V1ObjectMeta().name("example"))
.spec(new V1PodSpec().addContainersItem(new V1Container().name("example").image("nginx")));
api.createNamespacedPod("default", pod, null, null, null);
Both libraries provide Java clients for interacting with Kubernetes clusters. kubernetes-client offers a more feature-rich and actively maintained solution, while kubernetes-client/java provides a simpler, more straightforward API. The choice between them depends on the specific requirements of your project and your familiarity with Kubernetes concepts.
Official Python client library for kubernetes
Pros of python
- Simpler syntax and easier to read, especially for beginners
- Faster development time due to Python's concise nature
- Rich ecosystem of data science and machine learning libraries
Cons of python
- Generally slower execution speed compared to Java
- Lack of static typing can lead to runtime errors
- Less robust in large-scale enterprise applications
Code Comparison
python:
from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
pods = v1.list_pod_for_all_namespaces()
for pod in pods.items:
print(f"{pod.metadata.namespace}\t{pod.metadata.name}")
java:
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.util.Config;
ApiClient client = Config.defaultClient();
CoreV1Api api = new CoreV1Api(client);
V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null);
for (V1Pod item : list.getItems()) {
System.out.printf("%s\t%s%n", item.getMetadata().getNamespace(), item.getMetadata().getName());
}
The python code is more concise and readable, while the java code offers more explicit type declarations and method calls.
Javascript client
Pros of javascript
- Easier to set up and use for JavaScript developers
- Better suited for client-side applications and Node.js environments
- More active community and frequent updates
Cons of javascript
- Less comprehensive API coverage compared to the Java client
- May have performance limitations for large-scale server-side applications
- Documentation can be less detailed in some areas
Code Comparison
java:
ApiClient client = Config.defaultClient();
CoreV1Api api = new CoreV1Api(client);
V1PodList list = api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null);
javascript:
const k8s = require('@kubernetes/client-node');
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
const res = await k8sApi.listPodForAllNamespaces();
Both clients provide similar functionality, but the JavaScript version offers a more concise syntax and uses Promises for asynchronous operations. The Java client, while more verbose, provides stronger type safety and may be preferred in enterprise environments.
The javascript client is generally easier to integrate into web-based or Node.js projects, while the java client is better suited for large-scale, server-side applications that require robust performance and extensive API coverage.
Officially supported dotnet Kubernetes Client library
Pros of csharp
- Better integration with .NET ecosystem and tooling
- More idiomatic C# code, leveraging language-specific features
- Potentially easier for C# developers to use and maintain
Cons of csharp
- Smaller community and fewer contributors compared to java
- Less mature and potentially fewer features implemented
- May have slower adoption of new Kubernetes API features
Code Comparison
java:
V1Pod pod = new V1PodBuilder()
.withNewMetadata().withName("example").endMetadata()
.withNewSpec()
.addNewContainer()
.withName("example-container")
.withImage("nginx")
.endContainer()
.endSpec()
.build();
csharp:
var pod = new V1Pod
{
Metadata = new V1ObjectMeta { Name = "example" },
Spec = new V1PodSpec
{
Containers = new List<V1Container>
{
new V1Container
{
Name = "example-container",
Image = "nginx"
}
}
}
};
Both repositories provide client libraries for interacting with Kubernetes APIs, but they cater to different programming languages and ecosystems. The java repository has a larger community and more contributors, potentially leading to faster feature adoption and more comprehensive documentation. However, the csharp repository offers better integration with the .NET ecosystem and more idiomatic C# code, which may be preferable for C# developers.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Kubernetes Java Client
Java client for the kubernetes API.
To start using Kubernetes Java Client
See the wiki page and documentation here.
Release
Starting from 20.0.0
(Kubernetes 1.28), client-java-api
was introduced non-backward-compatible changes. Optional
parameters are now consolidated into a single object, and Java8 support has been removed. For Java8 users or those
preferring the old SDK interface, a legacy SDK module version is available with a "-legacy" suffix, like 20.0.0-legacy
.
Development
Support
If you need support, start with checking whether you're hitting known issues. If that doesn't work, please open an issue to describe the cases. Additionally, before you file an issue, please search existing issues to see if your issue is already covered.
You can also reach out to us via #kubernetes-client slack channel.
Top Related Projects
Java client for Kubernetes & OpenShift
Official Python client library for kubernetes
Javascript client
Officially supported dotnet Kubernetes Client library
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot