kubebuilder入门

1. 安装kubebuilder

brew install kubebuilder

2. 需求描述

开发一个zk operator。

cr定义为ZooKeeperCluster

3. 开发过程

3.1 创建一个空的文件夹zk-operator

mkdir zk-operator

3.2 进入该文件夹

cd zk-operator

3.3 执行初始化

kubebuilder init --domain my.domain --repo my.domain

3.4 创建API

kubebuilder create api --group webapp --version v1 --kind ZooKeeperCluster

3.5 修改zk 类字段

type ZooKeeperClusterSpec struct {
	// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
	// Important: Run "make" to regenerate code after modifying this file

	// Foo is an example field of ZooKeeperCluster. Edit zookeepercluster_types.go to remove/update
	Foo      string `json:"foo,omitempty"`
	Replicas *int32 `json:"replicas,omitempty"`
	Image    string `json:"image,omitempty"`
	Version  string `json:"version,omitempty"`
}

3.5 生成crd等文件

cd zk-operator


make manifests

3.6 在main函数中设置kubeconfig文件路径

	kubeconfig := "/Users/liyinlong/env/88.conf"
	//if os.Getenv("KUBECONFIG") != "" {
	//	kubeconfig = os.Getenv("KUBECONFIG")
	//}
	config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
	if err != nil {
		log.Fatalf("Error building kubeconfig: %s", err.Error())
	}

	mgr, err := ctrl.NewManager(config, ctrl.Options{
		Scheme: scheme,
		Metrics: metricsserver.Options{
			BindAddress:   metricsAddr,
			SecureServing: secureMetrics,
			TLSOpts:       tlsOpts,
		},
		WebhookServer:          webhookServer,
		HealthProbeBindAddress: probeAddr,
		LeaderElection:         enableLeaderElection,
		LeaderElectionID:       "1c8adf6b.my.domain",
		// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
		// when the Manager ends. This requires the binary to immediately end when the
		// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
		// speeds up voluntary leader transitions as the new leader don't have to wait
		// LeaseDuration time first.
		//
		// In the default scaffold provided, the program ends immediately after
		// the manager stops, so would be fine to enable this option. However,
		// if you are doing or is intended to do any operation such as perform cleanups
		// after the manager stops then its usage might be unsafe.
		// LeaderElectionReleaseOnCancel: true,
	})

3.7 创建一个zk cr

apiVersion: webapp.my.domain/v1
kind: ZooKeeperCluster
metadata:
  labels:
    zk: myzk
  name: myzk1
spec:
  foo: lyl
  replicas: 2
  image: zookeeper
  version: 3.4.1

Quick Start - The Kubebuilder Book

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值