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做相关工作。操作起来还是比较简便的。
- kubernetes 客户端client-go 使用及常用api
- webview进度条设置之WebChromeClient常用API与功能使用详解
- smbclient 常用命令使用
- Practical Multithreading for Client Apps (.net客户端程序使用多线程的实践)
- 在C#中可以使用RegisterClientScriptBlock、RegisterStartupScript和RegisterClientScriptInclude来向客户端注册脚本,它们的区别
- 使用 ClientScriptManager 类的 RegisterClientScriptBlock方法添加客户端脚本
- (转载)DevExpress ASPxGridView 使用文档七:客户端API
- FastDFS的配置、部署与API使用解读(4)FastDFS配置详解之Client配置
- SQLite的使用与常用API
- ASP.NET 2.0中客户端脚本使用总结(ClientScriptManager)
- CXF之使用jaxws API 发布服务与进行客户端调用
- 用Instant Client,不安装oracle客户端使用sqlplus连接远程数据库
- 关于[使用 WCF 测试客户端 (WcfTestClient.exe)]
- HBase客户端Java API使用总结
- HttpClient Fluent API 的使用
- JAVA学习第三十九课(常用对象API)— Map集合及其子类对象(集合使用技巧)
- JQuery Ajax操作常用的API:serializeArray、serialize、param的区别和使用
- [Database] Oracle: Oracle 客户端 instantclient 的使用
- 使用SOCKET API写的客户端与服务端测试程序(C语言)
- Sharepoint2010应用开发三:使用客户端对象模型(Client Object Model)读取列表数据