实例介绍
Kubernetes客户端是一个为.NET开发者提供的官方支持库,使得开发者可以更加便捷地在.NET应用中管理和操作Kubernetes集群资源。
使用方法
通过Nuget包添加Kubernetes客户端库:
dotnet add package KubernetesClient
使用Visual Studio生成项目:
dotnet msbuild /t:slngen
认证/配置
您可以使用标准的KubeConfig文件与此库一起使用,参见下面的BuildConfigFromConfigFile函数。大多数认证方法目前都得到支持。
监控
内置了可选的Prometheus客户端指标生成。导出的指标包括:
- k8s_dotnet_request_total - 按HTTP方法细分的请求计数器
- k8s_dotnet_response_code_total - 按HTTP方法和响应代码细分的响应计数器
- k8s_request_latency_seconds - 按方法、API组、API版本和资源种类细分的延迟直方图
示例代码
创建客户端并列出、创建、删除对象的示例:
// 加载默认kubeconfig var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); // 从指定文件加载: var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(Environment.GetEnvironmentVariable("KUBECONFIG")); // 使用配置对象创建客户端。 var client = new Kubernetes(config); // 列出命名空间 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); } } // 创建和删除对象 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());
本项目包含了如何使用Kubernetes .NET客户端库进行基本的集群管理操作,适合需要在.NET环境下操作Kubernetes集群的开发者。
【实例截图】
【核心代码】
文件清单
└── csharp-2ed87b5173ccde579c8f61be1241b6cc59cb0fb1
├── CodeCoverage.runsettings
├── code-of-conduct.md
├── CONTRIBUTING.md
├── csharp.settings
├── Directory.Build.props
├── Directory.Build.targets
├── doc
│ ├── docfx.json
│ ├── index.md
│ └── toc.yml
├── examples
│ ├── aot
│ │ ├── aot.csproj
│ │ └── Program.cs
│ ├── attach
│ │ ├── Attach.cs
│ │ └── attach.csproj
│ ├── cp
│ │ ├── Cp.cs
│ │ └── cp.csproj
│ ├── csrApproval
│ │ ├── csrApproval.csproj
│ │ └── Program.cs
│ ├── customResource
│ │ ├── config
│ │ │ ├── crd.yaml
│ │ │ └── yaml-cr-instance.yaml
│ │ ├── cResource.cs
│ │ ├── customResource.csproj
│ │ ├── CustomResourceDefinition.cs
│ │ ├── Program.cs
│ │ ├── README.md
│ │ └── Utils.cs
│ ├── Directory.Build.props
│ ├── Directory.Build.targets
│ ├── exec
│ │ ├── Exec.cs
│ │ └── exec.csproj
│ ├── generic
│ │ ├── Generic.cs
│ │ └── generic.csproj
│ ├── labels
│ │ ├── labels.csproj
│ │ └── PodList.cs
│ ├── logs
│ │ ├── Logs.cs
│ │ └── logs.csproj
│ ├── metrics
│ │ ├── metrics.csproj
│ │ └── Program.cs
│ ├── namespace
│ │ ├── namespace.csproj
│ │ └── NamespaceExample.cs
│ ├── openTelemetryConsole
│ │ ├── openTelemetryConsole.csproj
│ │ └── Program.cs
│ ├── patch
│ │ ├── patch.csproj
│ │ └── Program.cs
│ ├── portforward
│ │ ├── PortForward.cs
│ │ └── portforward.csproj
│ ├── prometheus
│ │ ├── Prometheus.cs
│ │ └── prometheus.csproj
│ ├── restart
│ │ ├── Program.cs
│ │ └── restart.csproj
│ ├── simple
│ │ ├── PodList.cs
│ │ └── simple.csproj
│ ├── watch
│ │ ├── Program.cs
│ │ └── watch.csproj
│ ├── webApiDependencyInjection
│ │ ├── appsettings.Development.json
│ │ ├── appsettings.json
│ │ ├── Controllers
│ │ │ ├── ExampleDependencyInjectionOnConstructorController.cs
│ │ │ └── ExampleDependencyInjectionOnMethodController.cs
│ │ ├── Program.cs
│ │ └── webApiDependencyInjection.csproj
│ ├── workerServiceDependencyInjection
│ │ ├── appsettings.Development.json
│ │ ├── appsettings.json
│ │ ├── Program.cs
│ │ ├── Worker.cs
│ │ └── workerServiceDependencyInjection.csproj
│ └── yaml
│ ├── Program.cs
│ └── yaml.csproj
├── global.json
├── kubernetes-client.proj
├── kubernetes-client.ruleset
├── LICENSE
├── logo.png
├── nuget.config
├── OWNERS
├── README.md
├── SECURITY_CONTACTS
├── src
│ ├── KubernetesClient
│ │ ├── AbstractKubernetes.cs
│ │ ├── AssemblyInfo.cs
│ │ ├── Authentication
│ │ │ ├── BasicAuthenticationCredentials.cs
│ │ │ ├── ExecTokenProvider.cs
│ │ │ ├── ITokenProvider.cs
│ │ │ ├── OidcTokenProvider.cs
│ │ │ ├── ServiceClientCredentials.cs
│ │ │ ├── StringTokenProvider.cs
│ │ │ ├── TokenCredentials.cs
│ │ │ └── TokenFileAuth.cs
│ │ ├── Autorest
│ │ │ ├── HttpExtensions.cs
│ │ │ ├── HttpMessageWrapper.cs
│ │ │ ├── HttpOperationException.cs
│ │ │ ├── HttpOperationResponse.cs
│ │ │ ├── HttpRequestMessageWrapper.cs
│ │ │ └── HttpResponseMessageWrapper.cs
│ │ ├── ByteBuffer.cs
│ │ ├── CertUtils.cs
│ │ ├── ChannelIndex.cs
│ │ ├── Exceptions
│ │ │ ├── KubeConfigException.cs
│ │ │ └── KubernetesClientException.cs
│ │ ├── ExecAsyncCallback.cs
│ │ ├── Extensions.cs
│ │ ├── FileSystem.cs
│ │ ├── FloatEmitter.cs
│ │ ├── GeneratedApiVersion.cs
│ │ ├── GenericClient.cs
│ │ ├── Global.cs
│ │ ├── IItems.cs
│ │ ├── IKubernetes.cs
│ │ ├── IKubernetes.Exec.cs
│ │ ├── IKubernetesObject.cs
│ │ ├── IKubernetes.WebSocket.cs
│ │ ├── IMetadata.cs
│ │ ├── ISpec.cs
│ │ ├── IStatus.cs
│ │ ├── IStreamDemuxer.cs
│ │ ├── IValidate.cs
│ │ ├── KubeConfigModels
│ │ │ ├── AuthProvider.cs
│ │ │ ├── Cluster.cs
│ │ │ ├── ClusterEndpoint.cs
│ │ │ ├── Context.cs
│ │ │ ├── ContextDetails.cs
│ │ │ ├── ExecCredentialResponse.cs
│ │ │ ├── ExternalExecution.cs
│ │ │ ├── K8SConfiguration.cs
│ │ │ ├── NamedExtension.cs
│ │ │ ├── UserCredentials.cs
│ │ │ └── User.cs
│ │ ├── KubernetesClientConfiguration.ConfigFile.cs
│ │ ├── KubernetesClientConfiguration.cs
│ │ ├── KubernetesClientConfiguration.InCluster.cs
│ │ ├── KubernetesClient.csproj
│ │ ├── Kubernetes.ConfigInit.cs
│ │ ├── Kubernetes.cs
│ │ ├── KubernetesException.cs
│ │ ├── Kubernetes.Exec.cs
│ │ ├── KubernetesJson.cs
│ │ ├── KubernetesMetricsExtensions.cs
│ │ ├── KubernetesObject.cs
│ │ ├── KubernetesRequestDigest.cs
│ │ ├── Kubernetes.WebSocket.cs
│ │ ├── KubernetesYaml.cs
│ │ ├── LeaderElection
│ │ │ ├── ILock.cs
│ │ │ ├── LeaderElectionConfig.cs
│ │ │ ├── LeaderElectionRecord.cs
│ │ │ ├── LeaderElector.cs
│ │ │ └── ResourceLock
│ │ │ ├── ConfigMapLock.cs
│ │ │ ├── EndpointsLock.cs
│ │ │ ├── LeaseLock.cs
│ │ │ ├── MetaObjectAnnotationLock.cs
│ │ │ ├── MetaObjectLock.cs
│ │ │ └── MultiLock.cs
│ │ ├── LineSeparatedHttpContent.cs
│ │ ├── Models
│ │ │ ├── ContainerMetrics.cs
│ │ │ ├── GeneratedModelVersion.cs
│ │ │ ├── IntOrStringJsonConverter.cs
│ │ │ ├── IntOrStringYamlConverter.cs
│ │ │ ├── IntstrIntOrString.cs
│ │ │ ├── KubernetesEntityAttribute.cs
│ │ │ ├── KubernetesList.cs
│ │ │ ├── ModelExtensions.cs
│ │ │ ├── ModelVersionConverter.cs
│ │ │ ├── NodeMetrics.cs
│ │ │ ├── NodeMetricsList.cs
│ │ │ ├── PodMetrics.cs
│ │ │ ├── PodMetricsList.cs
│ │ │ ├── ResourceQuantity.cs
│ │ │ ├── ResourceQuantityJsonConverter.cs
│ │ │ ├── ResourceQuantityYamlConverter.cs
│ │ │ ├── V1Patch.cs
│ │ │ ├── V1PatchJsonConverter.cs
│ │ │ ├── V1PodTemplateSpec.cs
│ │ │ ├── V1Status.cs
│ │ │ └── V1Status.ObjectView.cs
│ │ ├── MuxedStream.cs
│ │ ├── PrometheusHandler.cs
│ │ ├── StreamDemuxer.cs
│ │ ├── StreamType.cs
│ │ ├── StringQuotingEmitter.cs
│ │ ├── Utilities.cs
│ │ ├── Watcher.cs
│ │ ├── WatcherExt.cs
│ │ ├── WebSocketBuilder.cs
│ │ └── WebSocketProtocol.cs
│ ├── KubernetesClient.Aot
│ │ ├── Global.cs
│ │ ├── KubeConfigModels
│ │ │ ├── AuthProvider.cs
│ │ │ ├── Cluster.cs
│ │ │ ├── ClusterEndpoint.cs
│ │ │ ├── Context.cs
│ │ │ ├── ContextDetails.cs
│ │ │ ├── ExecCredentialResponse.cs
│ │ │ ├── ExternalExecution.cs
│ │ │ ├── K8SConfiguration.cs
│ │ │ ├── StaticContext.cs
│ │ │ ├── UserCredentials.cs
│ │ │ └── User.cs
│ │ ├── KubernetesClient.Aot.csproj
│ │ ├── KubernetesClientConfiguration.ConfigFile.cs
│ │ ├── KubernetesJson.cs
│ │ ├── KubernetesYaml.cs
│ │ ├── SourceGenerationContext.cs
│ │ └── V1PatchJsonConverter.cs
│ ├── KubernetesClient.Classic
│ │ ├── AssemblyInfo.cs
│ │ ├── CertUtils.cs
│ │ ├── Global.cs
│ │ ├── KubernetesClient.Classic.csproj
│ │ └── Kubernetes.Websocket.Netstandard.cs
│ ├── KubernetesClient.Kubectl
│ │ ├── Beta
│ │ │ ├── AsyncKubectl.Cordon.cs
│ │ │ ├── AsyncKubectl.cs
│ │ │ ├── AsyncKubectl.Version.cs
│ │ │ ├── Kubectl.Cordon.cs
│ │ │ ├── Kubectl.cs
│ │ │ └── Kubectl.Version.cs
│ │ └── KubernetesClient.Kubectl.csproj
│ ├── KubernetesClient.ModelConverter
│ │ ├── AssemblyInfo.cs
│ │ ├── AutoMapper
│ │ │ ├── AutoMapperModelVersionConverter.cs
│ │ │ ├── KubernetesVersionComparer.cs
│ │ │ └── VersionConverter.cs
│ │ └── KubernetesClient.ModelConverter.csproj
│ ├── LibKubernetesGenerator
│ │ ├── ApiGenerator.cs
│ │ ├── ClassNameHelper.cs
│ │ ├── EmbedResource.cs
│ │ ├── GeneralNameHelper.cs
│ │ ├── GeneratorExecutionContextExt.cs
│ │ ├── generators
│ │ │ ├── LibKubernetesGenerator
│ │ │ │ └── LibKubernetesGenerator.csproj
│ │ │ └── LibKubernetesGenerator.Automapper
│ │ │ └── LibKubernetesGenerator.Automapper.csproj
│ │ ├── INustacheHelper.cs
│ │ ├── KubernetesClientSourceGenerator.cs
│ │ ├── LibKubernetesGenerator.target
│ │ ├── MetaHelper.cs
│ │ ├── ModelExtGenerator.cs
│ │ ├── ModelGenerator.cs
│ │ ├── ParamHelper.cs
│ │ ├── PluralHelper.cs
│ │ ├── StringHelpers.cs
│ │ ├── templates
│ │ │ ├── AbstractKubernetes.cs.template
│ │ │ ├── IBasicKubernetes.cs.template
│ │ │ ├── IOperations.cs.template
│ │ │ ├── Model.cs.template
│ │ │ ├── ModelExtensions.cs.template
│ │ │ ├── Operations.cs.template
│ │ │ └── OperationsExtensions.cs.template
│ │ ├── TypeHelper.cs
│ │ ├── UtilHelper.cs
│ │ ├── VersionConverterAutoMapperGenerator.cs
│ │ ├── VersionConverterStubGenerator.cs
│ │ └── VersionGenerator.cs
│ └── nuget.proj
├── stylecop.json
├── swagger.json
├── tests
│ ├── E2E.Aot.Tests
│ │ ├── E2E.Aot.Tests.csproj
│ │ └── MinikubeTests.cs
│ ├── E2E.Tests
│ │ ├── E2E.Tests.csproj
│ │ ├── KubectlTests.cs
│ │ ├── MinikubeFactAttribute.cs
│ │ ├── MinikubeTests.cs
│ │ └── Onebyone.cs
│ ├── Kubectl.Tests
│ │ ├── KubectlTests.cs
│ │ ├── Kubectl.Tests.csproj
│ │ └── KubectlTests.Version.cs
│ ├── KubernetesClient.Classic.Tests
│ │ ├── KubernetesClient.Classic.Tests.csproj
│ │ └── SimpleTests.cs
│ ├── KubernetesClient.Tests
│ │ ├── AssemblyInfo.cs
│ │ ├── assets
│ │ │ ├── apiserver-pfx-data.txt
│ │ │ ├── ca2.crt
│ │ │ ├── ca3.crt
│ │ │ ├── ca-bundle-correct.crt
│ │ │ ├── ca-bundle.crt
│ │ │ ├── ca-bundle-incorrect.crt
│ │ │ ├── ca-bundle-intermediate.crt
│ │ │ ├── ca-bundle-root.crt
│ │ │ ├── ca.crt
│ │ │ ├── ca-data.txt
│ │ │ ├── client-certificate-data.txt
│ │ │ ├── client.crt
│ │ │ ├── client.key
│ │ │ ├── client-key-data.txt
│ │ │ ├── elliptic-client.key
│ │ │ ├── elliptic.crt
│ │ │ ├── gcloud-config-helper.json
│ │ │ ├── kubeconfig.additional-properties.yml
│ │ │ ├── kubeconfig.as-user-extra.yml
│ │ │ ├── kubeconfig.cluster-extensions.yml
│ │ │ ├── kubeconfig.cluster-missmatch.yml
│ │ │ ├── kubeconfig.no-cluster.yml
│ │ │ ├── kubeconfig.no-context-details.yml
│ │ │ ├── kubeconfig.no-context.yml
│ │ │ ├── kubeconfig.no-credentials.yml
│ │ │ ├── kubeconfig.no-current-context.yml
│ │ │ ├── kubeconfig.no-server.yml
│ │ │ ├── kubeconfig.no-user.yml
│ │ │ ├── kubeconfig.preferences-extensions.yml
│ │ │ ├── kubeconfig.relative.yml
│ │ │ ├── kubeconfig.tls-no-skip.yml
│ │ │ ├── kubeconfig.tls-servername.yml
│ │ │ ├── kubeconfig.tls-skip-http.yml
│ │ │ ├── kubeconfig.tls-skip.yml
│ │ │ ├── kubeconfig.user-not-found.yml
│ │ │ ├── kubeconfig.user-oidc.yml
│ │ │ ├── kubeconfig.user-pass.yml
│ │ │ ├── kubeconfig.wildcard-ipv4.yml
│ │ │ ├── kubeconfig.wildcard-ipv6_2.yml
│ │ │ ├── kubeconfig.wildcard-ipv6.yml
│ │ │ ├── kubeconfig.yml
│ │ │ ├── mock-gcloud.cmd
│ │ │ ├── mock-gcloud.sh
│ │ │ ├── token1
│ │ │ └── token2
│ │ ├── AuthTests.cs
│ │ ├── AutoMapperVersionConverterTests.cs
│ │ ├── ByteBufferTests.cs
│ │ ├── CertificateValidationTests.cs
│ │ ├── CertUtilsTests.cs
│ │ ├── ExternalExecutionTests.cs
│ │ ├── IntOrStringTests.cs
│ │ ├── ItemsEnumTests.cs
│ │ ├── KubernetesClientConfigurationTests.cs
│ │ ├── KubernetesClient.Tests.csproj
│ │ ├── KubernetesExecTests.cs
│ │ ├── KubernetesJsonTests.cs
│ │ ├── KubernetesMetricsTests.cs
│ │ ├── KubernetesYamlTests.cs
│ │ ├── LeaderElection
│ │ │ └── LeaderElectionTests.cs
│ │ ├── Logging
│ │ │ ├── TestOutputLogger.cs
│ │ │ ├── TestOutputLoggerProvider.cs
│ │ │ └── TestOutputLoggingExtensions.cs
│ │ ├── Mock
│ │ │ ├── MockKubeApiServer.cs
│ │ │ ├── MockWebSocketBuilder.cs
│ │ │ ├── MockWebSocket.cs
│ │ │ └── Server
│ │ │ ├── Controllers
│ │ │ │ ├── PodExecController.cs
│ │ │ │ └── PodPortForwardController.cs
│ │ │ ├── Startup.cs
│ │ │ └── WebSocketTestAdapter.cs
│ │ ├── ModelExtensionTests.cs
│ │ ├── OidcAuthTests.cs
│ │ ├── OperatingSystemDependentFactAttribute.cs
│ │ ├── OperatingSystems.cs
│ │ ├── PodExecTests.cs
│ │ ├── QuantityValueTests.cs
│ │ ├── SerializationTests.cs
│ │ ├── StreamDemuxerTests.cs
│ │ ├── TaskAssert.cs
│ │ ├── TokenFileAuthTests.cs
│ │ ├── UtilityTests.cs
│ │ ├── V1StatusObjectViewTests.cs
│ │ ├── WatchTests.cs
│ │ └── WebSocketTestBase.cs
│ └── SkipTestLogger
│ ├── SkipTestLogger.cs
│ └── SkipTestLogger.csproj
└── version.json
60 directories, 346 files
标签:
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论