在 go 中实现自定义控制器之前,我们首先了解什么是 kubernetes 控制器和客户资源定义(crd)
kubernetes 控制器是控制平面的组件,它持续监视 kubernetes 集群的状态并采取行动以确保集群的实际状态与所需状态匹配。它会进行更改,试图使当前状态更接近所需状态。
自定义资源定义(crd)是一种扩展 kubernetes api 以创建我们自己的自定义资源的方法。这些自定义资源可以代表我们想要在 kubernetes 集群中管理的任何类型的对象。
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 -

apiversion: com.shivam.kumar/v1 kind: my-crd metadata: name: my-custom-resource spec: description: "my crd instance"
使用 kubectl 命令应用此文件
现在让我们继续创建自己的自定义控制器
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(
&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)
},
},
&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")
<-stop
}
现在当我们构建这个 go 程序并运行它时 -
go build -o k8s-controller .
./k8s-控制器
现在,每当我们添加、更新或删除上面创建的自定义资源时,我们都会在终端中获取它的活动日志。所以这意味着我们的控制器正在监视我们的 crd。
以上就是在 Go 中创建自定义 Kubernetes 控制器的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号