您的位置 首页 编程知识

在 Go 中创建自定义 Kubernetes 控制器

在 go 中实现自定义控制器之前,我们首先了解什么是 kubernetes 控制器和客户资源定义(crd) k…

在 go 中实现自定义控制器之前,我们首先了解什么是 kubernetes 控制器和客户资源定义(crd)

kubernetes 控制器

kubernetes 控制器是控制平面的组件,它持续监视 kubernetes 集群的状态并采取行动以确保集群的实际状态与所需状态匹配。它会进行更改,试图使当前状态更接近所需状态。

客户资源定义 (crd)

自定义资源定义(crd)是一种扩展 kubernetes api 以创建我们自己的自定义资源的方法。这些自定义资源可以代表我们想要在 kubernetes 集群中管理的任何类型的对象。

创建自己的自定义资源定义 (crd)

apiversion: apiextensions.k8s.io/v1 kind: customresourcedefinition metadata:   name: my-crds.com.shivam.kumar spec:   group: com.shivam.kumar   names:     kind: my-crd     plural: my-crds   scope: namespaced   versions:     - name: v1       served: true       storage: true       schema:         openapiv3schema:           type: object           properties:             apiversion:               type: string             kind:               type: string             metadata:               type: object             spec:               type: object               properties:                 description:                   type: string 
登录后复制

使用 kubectl 命令应用此文件,当我们在集群中看到可用的 crd 时,我们可以看到我们创建的 crd –

在 Go 中创建自定义 Kubernetes 控制器

创建自定义资源 (cr)

apiversion: com.shivam.kumar/v1 kind: my-crd metadata:   name: my-custom-resource spec:   description: "my crd instance" 
登录后复制

使用 kubectl 命令应用此文件

现在让我们继续创建自己的自定义控制器

创建自定义 kubernetes 控制器

package main  import (     "context"     "fmt"     "path/filepath"      metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"     "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"     "k8s.io/apimachinery/pkg/runtime"     "k8s.io/apimachinery/pkg/runtime/schema"     "k8s.io/apimachinery/pkg/watch"     "k8s.io/client-go/dynamic"     "k8s.io/client-go/rest"     "k8s.io/client-go/tools/cache"     "k8s.io/client-go/tools/clientcmd"     "k8s.io/client-go/util/homedir" )  func main() {     var kubeconfig string     if home := homedir.HomeDir(); home != "" {         kubeconfig = filepath.Join(home, ".kube", "config")     }      config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)     if err != nil {         fmt.Println("Falling back to in-cluster config")         config, err = rest.InClusterConfig()         if err != nil {             panic(err.Error())         }     }      dynClient, err := dynamic.NewForConfig(config)     if err != nil {         panic(err.Error())     }      thefoothebar := schema.GroupVersionResource{Group: "com.shivam.kumar", Version: "v1", Resource: "my-crds"}      informer := cache.NewSharedIndexInformer(         &amp;cache.ListWatch{             ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {                 return dynClient.Resource(thefoothebar).Namespace("").List(context.TODO(), options)             },             WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {                 return dynClient.Resource(thefoothebar).Namespace("").Watch(context.TODO(), options)             },         },         &amp;unstructured.Unstructured{},         0,         cache.Indexers{},     )      informer.AddEventHandler(cache.ResourceEventHandlerFuncs{         AddFunc: func(obj interface{}) {             fmt.Println("Add event detected:", obj)         },         UpdateFunc: func(oldObj, newObj interface{}) {             fmt.Println("Update event detected:", newObj)         },         DeleteFunc: func(obj interface{}) {             fmt.Println("Delete event detected:", obj)         },     })      stop := make(chan struct{})     defer close(stop)      go informer.Run(stop)      if !cache.WaitForCacheSync(stop, informer.HasSynced) {         panic("Timeout waiting for cache sync")     }      fmt.Println("Custom Resource Controller started successfully")         <p>现在当我们构建这个 go 程序并运行它时 -<br> go build -o k8s-controller .<br> ./k8s-控制器</p>  <p>现在,每当我们添加、更新或删除上面创建的自定义资源时,我们都会在终端中获取它的活动日志。所以这意味着我们的控制器正在监视我们的 crd。</p>                                    
登录后复制

以上就是在 Go 中创建自定义 Kubernetes 控制器的详细内容,更多请关注php中文网其它相关文章!

本文来自网络,不代表四平甲倪网络网站制作专家立场,转载请注明出处:http://www.elephantgpt.cn/1104.html

作者: nijia

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

18844404989

在线咨询: QQ交谈

邮箱: 641522856@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部