歡迎光臨
每天分享高質量文章

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模型(及其派生的實現,KubeResourceResultV1KubeResourceListResultV1)。

KubeResourceResultV1可以隱式地轉換為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();
this.kubeApi = kubeClientFactory.Get(kubeRegistryConfiguration);
}

 

        public async Task> Get()
{
var service = await kubeApi.ServicesV1()
.Get(kubeRegistryConfiguration.KeyOfServiceInK8s, kubeRegistryConfiguration.KubeNamespace);
var services = new List();
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());
}
}
}

常用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操作起來還是比較簡便的。

贊(0)

分享創造快樂