Search code examples
c#kubernetesclient

kubernetes client c# kubectl get services


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...


Solution

  • 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