kubernetes client c# kubectl get services

2/20/2021

As I am new to kubernetes, struggling to get the list of deployments and other details by using the kubernetes client c#.

Like for

 $kubectl get services
 $kubectl get nodes

Any help would be appreciated...

-- dinesh
c#
client
kubernetes

1 Answer

2/21/2021

To do that, you first need to be authenticated and authorized to your Kubernetes namespace/cluster.

var config = await KubernetesClientConfiguration.BuildConfigFromConfigFileAsync(new FileInfo("C:\\Path\\To\\Your\\Kubeconfig\\file"));
var k8sClient = new Kubernetes(config);

Below is how you can get deployment/service

var deployments = await k8sClient.ListNamespacedDeploymentAsync("insert-your-namespace-here");
var services = await k8sClient.ListNamespacedServiceAsync("insert-your-namespace-here");

Example for listing out your deployment/service

foreach (var service in services.Items)
    Console.WriteLine(service.Metadata.Name);

foreach (var item in deployments.Items)
    Console.WriteLine(item.Metadata.Name);

For more details and examples, check out this repo: https://github.com/kubernetes-client/csharp

-- gadnandev
Source: StackOverflow