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的方法是生成模型類並手動編寫實際操做方法,以提供改進的開發使用體驗(即有用且一致的異常類型)。html
Kubernetes API中的某些操做能夠根據傳入的參數返回不一樣的響應。例如,刪除a的請求若是調用者指定則v1/Pod
返回現有v1/Pod
(做爲PodV1
模型)DeletePropagationPolicy.Foreground
可是若是任何其餘類型則返回v1/Status
(做爲StatusV1
模型)的DeletePropagationPolicy
指定。git
爲了處理這種類型的多態響應,KubeClient使用KubeResultV1
模型(及其派生的實現,KubeResourceResultV1<TResource>
和KubeResourceListResultV1<TResource>
)。github
KubeResourceResultV1<TResource>
能夠隱式地轉換爲a TResource
或a StatusV1
,所以消費代碼能夠繼續使用客戶端,就好像它指望操做只返回資源或指望它只返回StatusV1
:api
PodV1 existingPod = await client.PodsV1().Delete("mypod", propagationPolicy: DeletePropagationPolicy.Foreground); async
// OR: StatusV1 deleteStatus = await client.PodsV1().Delete("mypod", propagationPolicy: DeletePropagationPolicy.Background);ide
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.htmlui
簡單代碼示例參見this
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;spa
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>());
}
}
}
https://github.com/tintoy/dotnet-kube-client/blob/develop/samples/DeploymentWithRollback/Program.cs
參看上面ocelot 的代碼
https://github.com/tintoy/dotnet-kube-client/blob/develop/samples/noob-exec/Program.cs
通常操做kubernetes ,二次開發的時候只須要對deployment、service作相關工做。操做起來仍是比較簡便的。