Client Configuration and Authentication for the Kubernetes Client Library for .NET (C#)
The Kubernetes client library for .NET provides several options for configuring the client and authenticating to a Kubernetes cluster. This document outlines the possible options and provides examples for each option.
Client Configuration
The KubernetesClientConfiguration
class is used to configure the Kubernetes client. The following are the possible configuration options:
ConfigFile
The ConfigFile
option reads the Kubernetes configuration file (typically located at ~/.kube/config
) and sets up the client configuration based on the information in the file.
Example:
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
InClusterConfig
The InClusterConfig
option is used when running in a Kubernetes cluster and the client needs to authenticate using the service account associated with the pod.
Example:
var config = KubernetesClientConfiguration.InClusterConfig();
FromKubeconfigLoader
The FromKubeconfigLoader
option is used to load the Kubernetes configuration from a custom location.
Example:
var config = KubernetesClientConfiguration.FromKubeconfigLoader(@"/path/to/kubeconfig");
FromConnectionInfo
The FromConnectionInfo
option is used to configure the client with the connection information directly.
Example:
var config = KubernetesClientConfiguration.FromConnectionInfo(new KubernetesClientConfiguration.ConnectionInfo
{
Host = "https://my-cluster.com",
ApiKey = new ApiKey("token", "my-token"),
});
Authentication
The Kubernetes client library for .NET supports several authentication methods, including: