Kubernetes Operator开发
1.kubebuilder 创建项目
2.Crontroller开发与部署
开发环境准备
kubebuilder 介绍
CRD的开发与部署
Crontroller开发与部署
Operator功能设计
借助operator完成 和企业内部注册中心打通
这里以Traefik+etcd的模式为例进行演示说明
在这里etcd provider注册中心可以换成eruka
流程
1. watch Pods
2.动态注册Pods的Endponit到Traefik
Operator 开发工具选择
Operator 开发SDK有2个选择:
kubebuilder
operator sdk
本质上他们都是在k8s控制器运行时上进行的封装,主要都是脚手架的生成,使用体验相差不大
kubebuilder
维护方: kubernetes-sigs
基于 k8s控制器运行时 封装
包含CRD和Controller开发
operator sdk
维护方: coreOS
基于 k8s控制器运行时封装
早期不包含CRD开发,在往kubebuilder方向进行融合
环境
go v1.18.3
kubernetes v1.24.1
kubebuilder v3.6.0
安装kubebuilder
https://objects.githubusercontent.com/github-production-release-asset-2e65be/125275487/35d09c97-2992
https://github.com/kubernetes-sigs/kubebuilder/releases/download/v3.6.0/kubebuilder_linux_amd64
[root@k8s-worker02 ~]# kubebuilder version
Version: main.version{KubeBuilderVersion:"3.6.0", KubernetesVendor:"1.24.1", GitCommit:"f20414648f1851ae97997f4a5f8eb4329f450f6d", BuildDate:"2022-08-03T11:47:17Z", GoOs:"linux", GoArch:"amd64"}
kubebuilder介绍
[root@k8s-worker02 ~]# 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
deploy-image.go.kubebuilder.io/v1-alpha | 3
go.kubebuilder.io/v2 | 2, 3
go.kubebuilder.io/v3 | 3
go.kubebuilder.io/v4-alpha | 3
grafana.kubebuilder.io/v1-alpha | 3
kustomize.common.kubebuilder.io/v1 | 3
kustomize.common.kubebuilder.io/v2-alpha | 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.
创建一个Operator项目
kubebuilder提供了一个init命令用来初始化一个新的Operator工程目录,具体用法如下:
[root@k8s-worker02 ~]# kubebuilder init -h
Initialize a new project including the following files:
- a "go.mod" with project dependencies
- a "PROJECT" file that stores project configuration
- a "Makefile" with several useful make targets for the project
- several YAML files for project deployment under the "config" directory
- a "main.go" file that creates the manager that will run the project controllers
Usage:
kubebuilder init [flags]
Examples:
# Initialize a new project with your domain and name in copyright
kubebuilder init --plugins go/v3 --domain example.org --owner "Your name"
# Initialize a new project defining a specific project version
kubebuilder init --plugins go/v3 --project-version 3
Flags:
--component-config create a versioned ComponentConfig file, may be 'true' or 'false'
--domain string domain for groups (default "my.domain")
--fetch-deps ensure dependencies are downloaded (default true)
-h, --help help for init
--license string license to use to boilerplate, may be one of 'apache2', 'none' (default "apache2")
--owner string owner to add to the copyright
--project-name string name of this project
--project-version string project version (default "3")
--repo string name to use for go module (e.g., github.com/user/repo), defaults to the go package of the current working directory.
--skip-go-version-check if specified, skip checking the Go version
Global Flags:
--plugins strings plugin keys to be used for this subcommand execution
关键参数说明:
--plugins,指定生成代码的插件, 默认使用“go.kubebuilder.io/v3”。
--project-version 支持的项目版本有2,3 默认3
--repo module path,就是 go mod init 指定的go module的名称·
--domain,组织名称 用于API Group等
--owner,operater所有者,一般填写开发者邮箱