kubernetes 客户端KubeClient使用及常用api
KubeClient是kubernetes 的C#語言客戶端簡單易用,KubeClient是.NET Core(目標netstandard1.4)的可擴展Kubernetes API客戶端, github地址:https://github.com/tintoy/dotnet-kube-client/,還有一個官方的SDK?https://github.com/kubernetes-client/csharp/? ,這兩個sdk的設計哲學上是不一樣的, 官方的客戶端使用代碼生成,代碼生成的使用是有限的; 生成的客戶端傾向于非慣用,并且對于像Kubernetes那樣大的Swagger規范,最終會在客戶端類上直接放置太多方法。KubeClient的方法是生成模型類并手動編寫實際操作方法,以提供改進的開發使用體驗(即有用且一致的異常類型)。
Kubernetes API中的某些操作可以根據傳入的參數返回不同的響應。例如,刪除a的請求如果調用者指定則v1/Pod返回現有v1/Pod(作為PodV1模型)DeletePropagationPolicy.Foreground但是如果任何其他類型則返回v1/Status(作為StatusV1模型)的DeletePropagationPolicy指定。
為了處理這種類型的多態響應,KubeClient使用KubeResultV1模型(及其派生的實現,KubeResourceResultV1<TResource>和KubeResourceListResultV1<TResource>)。
KubeResourceResultV1<TResource>可以隱式地轉換為a?TResource或a?StatusV1,因此消費代碼可以繼續使用客戶端,就好像它期望操作只返回資源或期望它只返回StatusV1:
PodV1 existingPod = await client.PodsV1().Delete("mypod", propagationPolicy: DeletePropagationPolicy.Foreground);
// OR: StatusV1 deleteStatus = await client.PodsV1().Delete("mypod", propagationPolicy: DeletePropagationPolicy.Background);
KubeClient設計也易于擴展。它的?KubeApiClient提供了Kubernetes API的頂級入口點,擴展方法用于公開更具體的資源客戶端。Ocelot的kubernetes 集成模塊就是使用KubeClient ,具體代碼參見https://github.com/ThreeMammals/Ocelot/tree/develop/src/Ocelot.Provider.Kubernetes,這個模塊是我們已經在生產環境下使用過了,最近合并進入Ocelot的主干代碼,文檔參見:https://ocelot.readthedocs.io/en/latest/features/kubernetes.html
簡單代碼示例參見
using KubeClient;
using KubeClient.Models;
using Ocelot.Logging;
using Ocelot.ServiceDiscovery.Providers;
using Ocelot.Values;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Ocelot.Provider.Kubernetes
{
???? public class Kube : IServiceDiscoveryProvider
???? {
???????? private KubeRegistryConfiguration kubeRegistryConfiguration;
???????? private IOcelotLogger logger;
???????? private IKubeApiClient kubeApi;
?
??????? public Kube(KubeRegistryConfiguration kubeRegistryConfiguration, IOcelotLoggerFactory factory, IKubeApiClientFactory kubeClientFactory)
???????? {
???????????? this.kubeRegistryConfiguration = kubeRegistryConfiguration;
???????????? this.logger = factory.CreateLogger<Kube>();
???????????? this.kubeApi = kubeClientFactory.Get(kubeRegistryConfiguration);
???????? }
?
??????? public async Task<List<Service>> Get()
???????? {
???????????? var service = await kubeApi.ServicesV1()
???????????????? .Get(kubeRegistryConfiguration.KeyOfServiceInK8s, kubeRegistryConfiguration.KubeNamespace);
???????????? var services = new List<Service>();
???????????? if (IsValid(service))
???????????? {
???????????????? services.Add(BuildService(service));
???????????? }
???????????? else
???????????? {
???????????????? logger.LogWarning($"namespace:{kubeRegistryConfiguration.KubeNamespace }service:{kubeRegistryConfiguration.KeyOfServiceInK8s} Unable to use ,it is invalid. Address must contain host only e.g. localhost and port must be greater than 0");
???????????? }
???????????? return services;
???????? }
?
??????? private bool IsValid(ServiceV1 service)
???????? {
???????????? if (string.IsNullOrEmpty(service.Spec.ClusterIP) || service.Spec.Ports.Count <= 0)
???????????? {
???????????????? return false;
???????????? }
??????????? return true;
???????? }
?
??????? private Service BuildService(ServiceV1 serviceEntry)
???????? {
???????????? var servicePort = serviceEntry.Spec.Ports.FirstOrDefault();
???????????? return new Service(
???????????????? serviceEntry.Metadata.Name,
???????????????? new ServiceHostAndPort(serviceEntry.Spec.ClusterIP, servicePort.Port),
???????????????? serviceEntry.Metadata.Uid,
???????????????? string.Empty,
???????????????? Enumerable.Empty<string>());
???????? }
???? }
}
常用api
1.deployment
https://github.com/tintoy/dotnet-kube-client/blob/develop/samples/DeploymentWithRollback/Program.cs
2.service
參看上面ocelot 的代碼
3.pod
https://github.com/tintoy/dotnet-kube-client/blob/develop/samples/noob-exec/Program.cs
總結
一般操作kubernetes ,二次開發的時候只需要對deployment、service做相關工作。KubeClient操作起來還是比較簡便的。
總結
以上是生活随笔為你收集整理的kubernetes 客户端KubeClient使用及常用api的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 构建现代Web应用时究竟是选择传统web
- 下一篇: ASP.NET Core 3.0 上的g