
使用KubeBuilder入门Kubernetes二次开发
Kubernetes 的使用者将通过学习其 APIs 是如何设计和实现的,从而更深入地了解 Kubernetes 。这本书将教读者如何开发自己的 Kubernetes APIs 以及如何设计核心 Kubernetes API 的原理。官方文档链接:引言 - The Kubebuilder BookKubeBuilder 是Go语言实现的一个工具,用于k8s开发的脚手架,通过基础配置可以生成一个代码
前言
前言
Kubernetes 的使用者将通过学习其 APIs 是如何设计和实现的,从而更深入地了解 Kubernetes 。这本书将教读者如何开发自己的 Kubernetes APIs 以及如何设计核心 Kubernetes API 的原理。
官方文档链接:引言 - The Kubebuilder Book
以下带大家通过KubeBuilder入门Kubernetes二次开发,本文以创建一个compute资源为例
一、KubeBuilder是什么?
KubeBuilder 是Go语言实现的一个工具,用于k8s开发的脚手架,通过基础配置可以生成一个代码模板,这个代码模板可以实现创建CRD资源对象。
二、实现步骤
前提条件:
需要安装好go的环境,可使用 go version验证
需要访问到K8S的集群,本地或者线上的(线上的可以通过root/.kube/config文件进行连接)
1、下载kubebuilder
git clone https://github.com/kubernetes-sigs/kubebuilder
cd kubebuilder
make build
cp bin/kubebuilder /usr/local/bin
kubebuilder -h
CLI tool for building Kubernetes extensions and tools.
Usage:
kubebuilder [flags]
kubebuilder [command]
Examples:
The first step is to initialize your project:
kubebuilder init [--plugins=<PLUGIN KEYS> [--project-version=<PROJECT VERSION>]]
<PLUGIN KEYS> is a comma-separated list of plugin keys from the following table
and <PROJECT VERSION> a supported project version for these plugins.
Plugin keys | Supported project versions
------------------------------------+----------------------------
base.go.kubebuilder.io/v3 | 3
declarative.go.kubebuilder.io/v1 | 2, 3
go.kubebuilder.io/v2 | 2, 3
go.kubebuilder.io/v3 | 3
kustomize.common.kubebuilder.io/v1 | 3
For more specific help for the init command of a certain plugins and project version
configuration please run:
kubebuilder init --help --plugins=<PLUGIN KEYS> [--project-version=<PROJECT VERSION>]
Default plugin keys: "go.kubebuilder.io/v3"
Default project version: "3"
Available Commands:
alpha Alpha-stage subcommands
completion Load completions for the specified shell
create Scaffold a Kubernetes API or webhook
edit Update the project configuration
help Help about any command
init Initialize a new project
version Print the kubebuilder version
Flags:
-h, --help help for kubebuilder
--plugins strings plugin keys to be used for this subcommand execution
--project-version string project version (default "3")
Use "kubebuilder [command] --help" for more information about a command.
3.创建文件夹
mkdir compute
cd compute
3.初始化go模块,会生成一个go.mod的文件
go mod init example.com/m
go: creating new go.mod: module example.com/m
ll
-rw-r--r-- 1 yongxingma staff 30B 6 9 00:29 go.mod
cat go.mod
module example.com/m
go 1.17
4.初始化代码
kubebuilder init --domain example.com --license apache2 --owner "anyidechengxuyuan"
- --domain string domain for groups (default "my.domain")
- --owner string owner to add to the copyright
- --license string license to use to boilerplate, may be one of 'apache2', 'none' (default "apache2")
kubebuilder create api --group test --version v1 --kind Compute
- --version string resource Version
- --kind string resource Kind
- --group string resource Group
以上设置的就是K8S资源 GVK对象
5、生成manifests文件,默认会生成到项目中config/crd/bases路径下
make manifests
6、安装CRD到集群中,这里你本机已经连接到了本地或远程K8S集群中,安装的CRD就到了你的连接的集群中
# 安装CRD
make install
# 启动controller
make run

查看CRD资源,目前还没有创建任何资源
kubectl get crd | grep compute
computes.test.example.com 2022-06-09T13:13:27Z
kubectl get compute
No resources found in default namespace.
7、修改CRD对象
在结构体中,添加Name 和 Brand字段

执行 make manifest 后 yaml会根据结构体的字段自动更新

然后再执行 make install 更新到集群中。
8、创建CR对象
修改test_v1_compute.yaml
apiVersion: test.example.com/v1
kind: Compute
metadata:
name: compute-sample
spec:
brand: lenovo
name: xiaoxin

执行apply命令,安装到集群中,
kubectl apply -f config/samples/test_v1_compute.yaml
compute.test.example.com/compute-sample created
##可以查看到已经新增的对象
kubectl get compute
NAME AGE
compute-sample 40s
kubectl get compute -oyaml
apiVersion: v1
items:
- apiVersion: test.example.com/v1
kind: Compute
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"test.example.com/v1","kind":"Compute","metadata":{"annotations":{},"name":"compute-sample","namespace":"default"},"spec":{"brand":"lenovo","name":"xiaoxin"}}
creationTimestamp: "2022-06-09T13:27:54Z"
generation: 1
name: compute-sample
namespace: default
resourceVersion: "7725385"
uid: a094e5f3-3f83-49b7-9336-87234b29d7db
spec:
brand: lenovo
name: xiaoxin
kind: List
metadata:
resourceVersion: ""
selfLink: ""
总结
本文仅仅简单介绍了CRD编写、创建的简单使用,如果真要进行二次开发,还有很多地方需要学习,后边我也会更新更详细的教程,如果对你有帮助可以点个赞,感谢支持。
更多推荐
所有评论(0)