0
点赞
收藏
分享

微信扫一扫

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


举报

相关推荐

0 条评论