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作相关工做。操做起来仍是比较简便的。