Monitor client metrics
masterHttpClient using System.Diagnostics.DiagnosticsSource. These metrics can be consumed or exposed by your application using standard .NET metrics collection patterns.repository·master·Indexed 22 days ago
https://github.com/kubernetes-client/csharpA .NET client library for interacting with the Kubernetes API server. It provides strongly-typed models and API wrappers for managing Kubernetes resources, including support for Custom Resource Definitions (CRDs) via a generic client. The library supports multiple configuration methods, including local kubeconfig, in-cluster configuration, and authentication to AKS using kubelogin and Managed Identity (MSI). Available as the KubernetesClient NuGet package.
HttpClient using System.Diagnostics.DiagnosticsSource. These metrics can be consumed or exposed by your application using standard .NET metrics collection patterns.BuildConfigFromConfigFile() function. This method automatically detects your Kubernetes configuration. If the KUBECONFIG environment variable is set, the client will use the path specified in that variable to load the configuration.The Kubernetes C# Client provides a generic mechanism to create, get, list, and delete custom resources. This allows you to interact with any resource defined by a CRD without needing pre-generated strongly-typed classes for every specific custom type.
Typical workflow:
BuildConfigFromConfigFile().customresources.csharp.com).Spec and Labels of the retrieved resource.Before interacting with custom resources via the client, you must ensure the Custom Resource Definition (CRD) is registered in your Kubernetes cluster.
kubectl:kubectl create -f ./config/crd.yamlkubectl create -f ./config/yaml-cr-instance.yaml
kubectl get customresources.csharp.comkubectl create -f ./config/crd.yaml
kubectl create -f ./config/yaml-cr-instance.yaml
kubectl get customresources.csharp.comTo use the Kubernetes C# Client library, add the KubernetesClient NuGet package to your project using the dotnet CLI.
dotnet add package KubernetesClientTo use the Kubernetes C# client in your project, add the KubernetesClient NuGet package using the dotnet CLI.
dotnet add package KubernetesClientYou can initialize the client using several configuration methods depending on your environment:
KUBECONFIG environment variable or a provided path.Once configured, pass the configuration object to the Kubernetes class to create the client.
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
// Load from a specific file:
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(Environment.GetEnvironmentVariable("KUBECONFIG"));
// Load from in-cluster configuration:
var config = KubernetesClientConfiguration.InClusterConfig();
// Use the config object to create a client.
var client = new Kubernetes(config);This example demonstrates how to use the kubelogin tool to authenticate against an Azure Kubernetes Service (AKS) cluster using Managed Identities (MSI) within a C# application.
Before running the example, ensure the following Azure and local environment configurations are met:
Azure Kubernetes Service RBAC Cluster Admin role (or appropriate RBAC permissions) on the AKS cluster.Note: The code must be executed on a VM that has the Managed Identity assigned.
server: The API server address of your AKS cluster.clientid: The Client ID of your managed identity.kubelogin: The local file path to the kubelogin executable.dotnet runIf you encounter an authentication provider that is not yet supported by BuildConfigFromConfigFile, you can use kubectl proxy as a workaround.
kubectl proxyhttp://127.0.0.1:8001):var config = new KubernetesClientConfiguration { Host = "http://127.0.0.1:8001" };
var client = new Kubernetes(config);Note: This is a workaround and is not recommended for production use.var config = new KubernetesClientConfiguration { Host = "http://127.0.0.1:8001" };Use the API groups (e.g., CoreV1) to list resources. For example, you can iterate through all namespaces and then list the pods within each namespace.
var namespaces = client.CoreV1.ListNamespace();
foreach (var ns in namespaces.Items) {
Console.WriteLine(ns.Metadata.Name);
var list = client.CoreV1.ListNamespacedPod(ns.Metadata.Name);
foreach (var item in list.Items)
{
Console.WriteLine(item.Metadata.Name);
}
}You can create and delete resources by passing the appropriate model objects (like V1Namespace) to the corresponding API methods.
var ns = new V1Namespace
{
Metadata = new V1ObjectMeta
{
Name = "test"
}
};
var result = client.CoreV1.CreateNamespace(ns);
Console.WriteLine(result);
var status = client.CoreV1.DeleteNamespace(ns.Metadata.Name, new V1DeleteOptions());The SDK follows a version skew policy. Generally, an SDK version is compatible with Kubernetes clusters within an n-2 or n-3 version range (depending on the specific version).
| SDK Version | Kubernetes Version | .NET Targeting |
|---|---|---|
| 20.0 | 1.36 | net8.0;net9.0;net10.0;net48*;netstandard2.0* |
| 19.0 | 1.35 | net8.0;net9.0;net10.0;net48*;netstandard2.0* |
| 18.0 | 1.34 | net8.0;net9.0;net10.0;net48*;netstandard2.0* |
| 17.0 | 1.33 | net8.0;net9.0;net48* |
| 16.0 | 1.32 | net8.0;net9.0;net48* |
| 15.0 | 1.31 | net6.0;net8.0;net48*;netstandard2.0* |
| 14.0 | 1.30 | net6.0;net8.0;net48*;netstandard2.0* |
| 13.0 | 1.29 | net6.0;net7.0;net8.0;net48* |
| 12.0 | 1.28 | net6.0;net7.0;net48* |
| 11.0 | 1.27 | net6.0;net7.0;net48* |
| 10.0 | 1.26 | net6.0;net7.0;net48* |
| 9.1 | 1.25 | netstandard2.1;net6.0;net48*;netstandard2.0* |
| 9.0 | 1.25 | netstandard2.1;net5.0;net6.0;net48*;netstandard2.0* |
| 8.0 | 1.24 | netstandard2.1;net5.0;net6.0;net48*;netstandard2.0* |
| 7.2 | 1.23 | netstandard2.1;net5.0;net6.0;net48*;netstandard2.0* |
| 7.0 | 1.23 | netstandard2.1;net5.0;net6.0 |
| 6.0 | 1.22 | netstandard2.1;net5.0 |
| 5.0 | 1.21 | netstandard2.1;net5 |
| 4.0 | 1.20 | netstandard2.0;netstandard2.1 |
| 3.0 | 1.19 | netstandard2.0;net452 |
| 2.0 | 1.18 | netstandard2.0;net452 |
| 1.6 | 1.16 | netstandard1.4;netstandard2.0;net452 |
| 1.4 | 1.13 | netstandard1.4;net451 |
| 1.3 | 1.12 | netstandard1.4;net452 |