Kubernetes Clients in Popular Programming Languages
Kubernetes is an open-source platform for automating, deploying, and managing containerized applications. It is one of the most popular and widely used container management systems in the cloud. Kubernetes clients are programming libraries that allow developers to interact with the Kubernetes cluster from their applications.
There are several Kubernetes clients available in various programming languages. We’ll review some of the most popular Kubernetes clients in different programming languages, including Python, JavaScript, Java, C++, C#, PHP, TypeScript, Rust, and Go Lang.
1.- Python
The official Kubernetes Python client is called kubernetes-client
. It provides a Pythonic way of interacting with the Kubernetes API. Here's an example of listing all the pods in a Kubernetes cluster using the Python client:
from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
print(f"{i.metadata.namespace}\t{i.metadata.name}")
To install the kubernetes-client
library, use the following command:
pip install kubernetes
2.- JavaScript
The official Kubernetes JavaScript client is called @kubernetes/client-node
. It provides a set of Node.js modules for interacting with the Kubernetes API. Here's an example of listing all the pods in a Kubernetes cluster using the JavaScript client:
const k8s = require('@kubernetes/client-node');
const kc = new k8s.KubeConfig();
kc.loadFromDefault();
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
k8sApi.listPodForAllNamespaces().then((res) => {
res.body.items.forEach((item) => {
console.log(`${item.metadata.namespace}\t${item.metadata.name}`);
});
}).catch((err) => {
console.error(err);
});
To install the @kubernetes/client-node
library, use the following command:
npm install @kubernetes/client-node
3.- Java
The official Kubernetes Java client is called fabric8io/kubernetes-client
. It provides a fluent API for interacting with the Kubernetes API. Here's an example of listing all the pods in a Kubernetes cluster using the Java client:
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
public class Main {
public static void main(String[] args) {
Config config = new ConfigBuilder().build();
KubernetesClient client = new DefaultKubernetesClient(config);
client.pods().inAnyNamespace().list().getItems().forEach(pod -> {
System.out.printf("%s\t%s%n", pod.getMetadata().getNamespace(), pod.getMetadata().getName());
});
client.close();
}
}
To install the fabric8io/kubernetes-client
library, add the following dependency to your pom.xml
file:
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>5.3.1</version>
</dependency>
4.- C++
The official Kubernetes C++ client is called kubernetes-cpp
. It provides a C++ API for interacting with the Kubernetes API. Here's an example of listing all the pods in a Kubernetes cluster using the C++ client:
#include <iostream>
#include "kubernetes/client/client.h"
int main() {
k8s::Client::Config config;
config.setUrl("https://<kubernetes-master-url>");
config.setToken("<access-token>");
k8s::Client client(config);
auto pods = client.listPodForAllNamespaces();
for (const auto& pod : pods.items()) {
std::cout << pod.metadata().namespace_() << "\t" << pod.metadata().name() << std::endl;
}
return 0;
}
To install the kubernetes-cpp
library, you can use the following commands:
git clone https://github.com/kubernetes-cpp/client.git
cd client
mkdir build && cd build
cmake ..
make
sudo make install
5.- C#
The official Kubernetes C# client is called KubernetesClient
. It provides a .NET API for interacting with the Kubernetes API. Here's an example of listing all the pods in a Kubernetes cluster using the C# client:
using KubernetesClient.Models;
using KubernetesClient.Client;
class Program {
static void Main(string[] args) {
var config = new Configuration {
Host = "<kubernetes-master-url>",
AccessToken = "<access-token>",
VerifySsl = false // only for testing
};
var client = new CoreV1Api(config);
var pods = client.ListPodForAllNamespaces();
foreach (var pod in pods.Items) {
Console.WriteLine($"{pod.Metadata.NamespaceProperty}\t{pod.Metadata.Name}");
}
}
}
To install the KubernetesClient
library, use the following NuGet command:
Install-Package KubernetesClient
6.- PHP
The official Kubernetes PHP client is called kubernetes-client/php
. It provides a PHP API for interacting with the Kubernetes API. Here's an example of listing all the pods in a Kubernetes cluster using the PHP client:
require __DIR__ . '/vendor/autoload.php';
use Kubernetes\Client as K8s;
$config = K8s::config([
'master' => '<kubernetes-master-url>',
'token' => '<access-token>'
]);
$client = new K8s($config);
foreach ($client->getPods() as $pod) {
echo "{$pod->metadata->namespace}\t{$pod->metadata->name}\n";
}
To install the kubernetes-client/php
library, use the following composer command:
composer require kubernetes/client-php
7.- TypeScript
The official Kubernetes TypeScript client is called @kubernetes/client-node
. It provides a set of TypeScript modules for interacting with the Kubernetes API. Here's an example of listing all the pods in a Kubernetes cluster using the TypeScript client:
import { CoreV1Api, KubeConfig } from '@kubernetes/client-node';
const kc = new KubeConfig();
kc.loadFromDefault();
const k8sApi = kc.makeApiClient(CoreV1Api);
k8sApi.listPodForAllNamespaces().then((res) => {
res.body.items.forEach((item) => {
console.log(`${item.metadata.namespace}\t${item.metadata.name}`);
});
}).catch((err) => {
console.error(err);
});
To install the @kubernetes/client-node
library, use the following command:
npm install @kubernetes/client-node
8.- Rust
The official Kubernetes Rust client is called kube-rs
.
use kube::{
api::{Api, ListParams},
Client,
};
use std::env;
#[tokio::main]
async fn main() -> Result<(), kube::Error> {
let config = kube::Config::new(
kube::ConfigOptions::default(),
kube::config::KubeconfigFile::FromHomeDir,
)
.await?;
let client = Client::new(config);
let pods: Api<kube::api::corev1::Pod> = Api::all(client);
let lp = ListParams::default();
let pods_list = pods.list(&lp).await?;
for pod in pods_list.items {
println!("{} \t {}", pod.metadata.namespace.unwrap(), pod.metadata.name.unwrap());
}
Ok(())
}
To install the kube-rs
library, add the following dependency to your Cargo.toml
file:
[dependencies]
kube = "0.53.0"
Conclusion
We reviewed some of the most popular Kubernetes clients in different programming languages, including Python, JavaScript, Java, C++, C#, PHP, TypeScript, Rust, and Go Lang. We also provided examples of how to list all the pods in a Kubernetes cluster using each of these clients.
Using these Kubernetes clients, developers can easily interact with the Kubernetes API and manage their containerized applications in the cloud.