说明

kubernetes 估计会成为 linux 一样的存在,client-go 是它的 go sdk,client-go/examples/ 给出了一些用例,但是数量比较少。

api

Resource 的定义不在client-go中,而是在一个名为 api 的项目中,这个项目中的内容同步自 kubernetes 项目的目录 staging/src/k8s.io/api,可以用下面的方式获取:

go get k8s.io/api

api的目录结构如下:

▾ api/
▸ admission/
▸ admissionregistration/
▸ apps/
▸ authentication/
▸ authorization/
▸ autoscaling/
▸ batch/
▸ certificates/
▸ core/
▸ extensions/
▸ Godeps/
▸ imagepolicy/
▸ networking/
▸ policy/
▸ rbac/
▸ scheduling/
▸ settings/
▸ storage/
▸ vendor/
LICENSE
OWNERS
README.md

client-go

client-go是kubernetes官方的项目,go语言实现,使用示例源码位于: go-code-example/k8sclient

获取 client-go:

go get k8s.io/client-go
go get k8s.io/apimachinery
go get k8s.io/api
go get k8s.io/kube-openapi

创建 Clientset

Clientset 包含 kubernetes 的所有资源的操作句柄,通过”k8s.io/client-go/kubernetes”中NewForConfig()创建。

创建 Clientset 需要提供一个 rest.Config,Config 可以用后面提到的 tools/clientcmd 生成,也可以自己填写:

config := rest.Config{
Host: "https://10.39.0.105:6443",
APIPath: "/",
Prefix: "",
BearerToken: "af8cbdf725efadf8c4",
TLSClientConfig: rest.TLSClientConfig{Insecure: true},
}

创建方法很简单:

clientset, err := kubernetes.NewForConfig(&config)

使用 Clientset

Clientset,意如其名,是client 的集合,在client-go/kubernetes/clientset.go中定义。

type Clientset struct {
*discovery.DiscoveryClient
admissionregistrationV1alpha1 *admissionregistrationv1alpha1.AdmissionregistrationV1alpha1Client
appsV1beta1 *appsv1beta1.AppsV1beta1Client
appsV1beta2 *appsv1beta2.AppsV1beta2Client
appsV1 *appsv1.AppsV1Client
authenticationV1 *authenticationv1.AuthenticationV1Client
authenticationV1beta1 *authenticationv1beta1.AuthenticationV1beta1Client
authorizationV1 *authorizationv1.AuthorizationV1Client
authorizationV1beta1 *authorizationv1beta1.AuthorizationV1beta1Client
autoscalingV1 *autoscalingv1.AutoscalingV1Client
autoscalingV2beta1 *autoscalingv2beta1.AutoscalingV2beta1Client
batchV1 *batchv1.BatchV1Client
batchV1beta1 *batchv1beta1.BatchV1beta1Client
batchV2alpha1 *batchv2alpha1.BatchV2alpha1Client
certificatesV1beta1 *certificatesv1beta1.CertificatesV1beta1Client
coreV1 *corev1.CoreV1Client
extensionsV1beta1 *extensionsv1beta1.ExtensionsV1beta1Client
networkingV1 *networkingv1.NetworkingV1Client
policyV1beta1 *policyv1beta1.PolicyV1beta1Client
rbacV1 *rbacv1.RbacV1Client
rbacV1beta1 *rbacv1beta1.RbacV1beta1Client
rbacV1alpha1 *rbacv1alpha1.RbacV1alpha1Client
schedulingV1alpha1 *schedulingv1alpha1.SchedulingV1alpha1Client
settingsV1alpha1 *settingsv1alpha1.SettingsV1alpha1Client
storageV1beta1 *storagev1beta1.StorageV1beta1Client
storageV1 *storagev1.StorageV1Client
}

用 Clientset 的方法获得不同资源的操作句柄,Clientset有以下的方法可供使用:

+Admissionregistration() : admissionregistrationv1alpha1.AdmissionregistrationV1alpha1Interface
+AdmissionregistrationV1alpha1() : admissionregistrationv1alpha1.AdmissionregistrationV1alpha1Interface
+Apps() : appsv1.AppsV1Interface
+AppsV1() : appsv1.AppsV1Interface
+AppsV1beta1() : appsv1beta1.AppsV1beta1Interface
+AppsV1beta2() : appsv1beta2.AppsV1beta2Interface
+Authentication() : authenticationv1.AuthenticationV1Interface
+AuthenticationV1() : authenticationv1.AuthenticationV1Interface
+AuthenticationV1beta1() : authenticationv1beta1.AuthenticationV1beta1Interface
+Authorization() : authorizationv1.AuthorizationV1Interface
+AuthorizationV1() : authorizationv1.AuthorizationV1Interface
+AuthorizationV1beta1() : authorizationv1beta1.AuthorizationV1beta1Interface
+Autoscaling() : autoscalingv1.AutoscalingV1Interface
+AutoscalingV1() : autoscalingv1.AutoscalingV1Interface
+AutoscalingV2beta1() : autoscalingv2beta1.AutoscalingV2beta1Interface
+Batch() : batchv1.BatchV1Interface
+BatchV1() : batchv1.BatchV1Interface
+BatchV1beta1() : batchv1beta1.BatchV1beta1Interface
+BatchV2alpha1() : batchv2alpha1.BatchV2alpha1Interface
+Certificates() : certificatesv1beta1.CertificatesV1beta1Interface
+CertificatesV1beta1() : certificatesv1beta1.CertificatesV1beta1Interface
+Core() : corev1.CoreV1Interface
+CoreV1() : corev1.CoreV1Interface
+Discovery() : discovery.DiscoveryInterface
+Extensions() : extensionsv1beta1.ExtensionsV1beta1Interface
+ExtensionsV1beta1() : extensionsv1beta1.ExtensionsV1beta1Interface
+Networking() : networkingv1.NetworkingV1Interface
+NetworkingV1() : networkingv1.NetworkingV1Interface
+Policy() : policyv1beta1.PolicyV1beta1Interface
+PolicyV1beta1() : policyv1beta1.PolicyV1beta1Interface
+Rbac() : rbacv1.RbacV1Interface
+RbacV1() : rbacv1.RbacV1Interface
+RbacV1alpha1() : rbacv1alpha1.RbacV1alpha1Interface
+RbacV1beta1() : rbacv1beta1.RbacV1beta1Interface
+Scheduling() : schedulingv1alpha1.SchedulingV1alpha1Interface
+SchedulingV1alpha1() : schedulingv1alpha1.SchedulingV1alpha1Interface
+Settings() : settingsv1alpha1.SettingsV1alpha1Interface
+SettingsV1alpha1() : settingsv1alpha1.SettingsV1alpha1Interface
+Storage() : storagev1.StorageV1Interface
+StorageV1() : storagev1.StorageV1Interface
+StorageV1beta1() : storagev1beta1.StorageV1beta1Interface

其中Core()和CoreV1()获取到的corev1.CoreV1Interface,用来操作kubernetes的最基础的Resrouce。

type CoreV1Interface interface {
RESTClient() rest.Interface
ComponentStatusesGetter
ConfigMapsGetter
EndpointsGetter
EventsGetter
LimitRangesGetter
NamespacesGetter
NodesGetter
PersistentVolumesGetter
PersistentVolumeClaimsGetter
PodsGetter
PodTemplatesGetter
ReplicationControllersGetter
ResourceQuotasGetter
SecretsGetter
ServicesGetter
ServiceAccountsGetter
}

例如查找指定 namespace 中的所有 Pod:

pods, err := clientset.CoreV1().Pods("lijiaocn").List(v1.ListOptions{})

创建 RESTClient

client-go/rest 实现了 RESTClient,RESTClient 的创建方法有两种。

第一种方式是直接通过 config 创建,这种方式需要在 config 中填入 GroupVersion 信息:

// create restClient from config
func CreateCoreRestClient(config *rest.Config) (*rest.RESTClient, error) { config.ContentConfig.GroupVersion = &core_v1.SchemeGroupVersion
config.ContentConfig.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}
config.APIPath = "/api" restClient, err := rest.RESTClientFor(config)
if err != nil {
return nil, err
} if restClient == nil {
return nil, errors.New("restclient1 is nil")
} return restClient, nil
}

第二种方式是从 Clientset 中获取,这种方式不需要手动填入 GroupVersion,调用 Clientset 对应的接口即可:

// get restClient from clientset
func GetCoreRestClient(config *rest.Config) (rest.Interface, error) { clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
} if clientset == nil {
return nil, errors.New("clientset is nil")
} restClient := clientset.CoreV1().RESTClient()
if restClient == nil {
return nil, errors.New("restclient is nil")
} return restClient, nil
}

使用 RESTClient

RESTClient 是链式调用,使用方法如下,Do() 方法最后调用:

// /<namespace>/<resource>/<name>
// GET https://10.10.173.203/api/v1/namespaces/default/pods?limit=500
// GET https://10.10.173.203/api/v1/namespaces/kube-system/pods?limit=500
// GET https://10.10.173.203/api/v1/namespaces/kube-system/pods/kube-dns-5b54cf547c-jl4r9
result := restClient.Get().
Namespace("kube-system").
Resource("pods").
Name("kube-dns-5b54cf547c-jl4r9").
Do()
bytes, err := result.Raw()
if err != nil {
fmt.Printf("%s: %s\n", err.Error(), bytes)
} else {
fmt.Printf("%s\n", bytes)
}

client-go/tools

client-go 提供一套 tools,提供了配置文件加载、实现本地缓存等方法。

tools/clientcmd: 加载配置

tools/clientcmd 中提供了一些辅助工具,例如加载 kubeconfig 文件生成创建 client 是必须的 Config:

home, err := os.UserHomeDir()
if err != nil {
glog.Fatal(err.Error())
} file, err := filepath.Abs(home + "/.kube/config")
if err != nil {
glog.Fatal(err.Error())
} config, err := clientcmd.BuildConfigFromFlags("", file)
if err != nil {
glog.Fatal(err.Error())
return
} clientset, err := kubernetes.NewForConfig(config)
if err != nil {
glog.Fatal(err.Error())
return
}

tools/cache: 本地缓存

tools/cache 中提供了本地缓存的功能,特别是提供了 informer。

创建 informer 需要提供的 listwatcher 和 handler 都用 cache 中方法创建或在 cache 中定义:

labels := make(map[string]string)
selector := fields.SelectorFromSet(labels)
listwatch := cache.NewListWatchFromClient(restClient, "endpoints", "", selector)

handler 定义:

handler := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
fmt.Printf("Add endpoint:\n")
if ep, ok := obj.(*core_v1.Endpoints); !ok {
fmt.Printf("not endpoints\n")
} else {
printEndpoint(ep)
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
fmt.Printf("Update endpoint:\n")
if epOld, ok := oldObj.(*core_v1.Endpoints); !ok {
fmt.Printf("not endpoints\n")
} else {
printEndpoint(epOld)
} if epNew, ok := newObj.(*core_v1.Endpoints); !ok {
fmt.Printf("not endpoints\n")
} else {
printEndpoint(epNew)
}
},
DeleteFunc: func(obj interface{}) {
fmt.Printf("Delete endpoint:\n")
if ep, ok := obj.(*core_v1.Endpoints); !ok {
fmt.Printf("not endpoint")
} else {
printEndpoint(ep)
}
},
}

cache.NewInformer() 返回一个 store,一个controller,前者用于查询,后者用于控制同步。

stop := make(chan struct{})
store, controller := cache.NewInformer(listwatch, &core_v1.Endpoints{}, 0*time.Second, handler)
controller.Run(stop)

Kubernetes 的 Client Libraries 的使用的更多相关文章

  1. 编译pure-ftpd时提示错误Your MySQL client libraries aren't properly installed

    如果出现类似configure: error: Your MySQL client libraries aren’t properly installed 的错误,请将mysql目录下的 includ ...

  2. Tutorial 7: Schemas & client libraries

    转载自:http://www.django-rest-framework.org/tutorial/7-schemas-and-client-libraries/ Tutorial 7: Schema ...

  3. Kubernetes Python Client 初体验之node操作

    今天讲一下k8s中对于各个实物节点node的操作. 首先是获取所有nodes信息: self.config.kube_config.load_kube_config(config_file=" ...

  4. Kubernetes Python Client 初体验之Deployment

    Kubernetes官方推荐我们使用各种Controller来管理Pod的生命周期,今天写一个最常用的Deployment的操作例子. 首先是创建Deployment: with open(path. ...

  5. Kubernetes Python Client 初体验之安装授权

    最近想做一个基于flask的云平台管理服务器,利用python调用kubenetes提供的API来实现云平台的操作.笔者使用的是Windows,kubernetes集群安装在Ubuntu和Respbi ...

  6. Django REST framework 第七章 Schemas & client libraries

    模式是一个机器可读文档,描述可用的API端点,URL以及它们支持的操作. 模式对于自动生成文档是一个很有用的工具,也可以用来动态调用可以于API交互的客户端库. Core API 为了提供模式支持,R ...

  7. Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.

    System.Data.OracleClient 已经过时了.微软不再支持它. 因此,我建议你为. NET 使用Oracle数据提供程序:ODP.Net. 你可以从以下位置下载: 版本:Release ...

  8. Kubernetes相关概念

    This page explains how Kubernetes objects are represented in the Kubernetes API, and how you can exp ...

  9. kubernetes extension point

    以下大部分来自于k8s document, 笔者只是总结归纳, 解释不足的地方请参阅相关文档 Intention Non-sustainable way to customize Kubernetes ...

随机推荐

  1. Python 练习题总结(待续)

    1.编写一个函数,接受一个参数n,n为正整数,左右两种打印方 式.要求数字必须对齐 正三角 倒三角 实现思路: 思路1.一行一行打印,前面追加空格,每一个空格的宽度等于数字字符串的宽度 #正三角打印d ...

  2. 《剑指offer》数学题及其它 (牛客11.05)

    比较多的思维题,涉及位运算.快速幂.二进制.约瑟夫问题.队列.贪心.dp等等. 难度 题目 知识点 ☆ 12.数值的整数次方 细节,快速幂 ☆☆ 47.求1+2+3+···+n 思维发散 ☆☆ 48. ...

  3. 三节课MINI计划第五周

    一.任务及干货 二.作品 (一)小组分工 (二)社群运营方案

  4. csu 1756: Prime

    1756: Prime Submit Page   Summary   Time Limit: 3 Sec     Memory Limit: 128 Mb     Submitted: 281    ...

  5. 使用django进行大文件的上传下载

    下载 基于Django建立的网站,如果提供文件下载功能,最简单的方式莫过于将静态文件交给Nginx等处理,但有些时候,由于网站本身逻辑,需要通过Django提供下载功能,如页面数据导出功能(下载动态生 ...

  6. 【转】Linux下查看CPU、内存、磁盘信息

    1.查看CPU信息# 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数cat /proc/c ...

  7. Luogu4022 CTSC2012熟悉的文章(广义后缀自动机+二分答案+动态规划+单调队列)

    对作文库中的串建出广义SAM,然后显然可以二分答案,二分之后考虑暴力dp,设f[i]为前i位最长匹配长度,显然有f[i]=max(f[i-1],f[j]+i-j) (i-j>=l&&am ...

  8. 0160 十分钟看懂时序数据库(I)-存储

    摘要:2017年时序数据库忽然火了起来.开年2月Facebook开源了beringei时序数据库:到了4月基于PostgreSQL打造的时序数据库TimeScaleDB也开源了,而早在2016年7月, ...

  9. 用python将项目中的所有代码(或txt)合并在一个文件中

    设计模式开卷考试给的例子代码都是一个类一个java,实在太恶心了,所以写了一个python脚本. import os fileansdir=r'C:\Users\tonyson_in_the_rain ...

  10. (八)SpringBoot之freeMarker基本使用

    一.案例 1.1 pom.xml <dependencies> <!-- 除去logback支持 --> <dependency> <groupId>o ...