更新时间:2022-09-16 GMT+08:00
分享

安装Kubebuilder

环境准备

  • 已安装go v1.16+。
  • 已安装make。
  • 已安装docker 17.03+。
  • 已安装Kubernetes v1.15.0+ 。
  • 已安装kustomize,安装方法请参见安装 kustomize
  • 已安装controller-gen,安装方法请参见安装controller-gen

安装Kubebuilder

Kubebuilder是基于custom resource definitions(CRDs)构建Kubernetes APIs的框架,有如下两种安装方法 。

  • 安装方法一:根据kubebuilder官网提供的go语言版本和节点系统架构下载。
    # go env GOOS -- 获取操作系统类型,例如:linux等
    # go env GOARCH -- 获取系统架构,例如:arm或amd64等
    $ curl -L -o kubebuilder https://go.kubebuilder.io/dl/latest/$(go env GOOS)/$(go env GOARCH)
    $ chmod +x kubebuilder && mv kubebuilder /usr/local/bin/
  • 安装方法二:避免因证书导致无法通过curl进行下载。
    # 通过git clone及本地编译的方法安装kubebuilder可以避免因操作系统类型、系统架构输入错误导致
    # kubebuilder二进制文件因操作系统和系统架构导致错误导致的无法使用。同时,通过git操作避免
    # 在使用curl过程中需要对开发环境进行证书配置等操作
    $ git clone https://github.com/kubernetes-sigs/kubebuilder.git
    Cloning into 'kubebuilder'...
    remote: Enumerating objects: 49241, done.
    remote: Counting objects: 100% (2995/2995), done.
    remote: Compressing objects: 100% (1087/1087), done.
    remote: Total 49241 (delta 1897), reused 2679 (delta 1741), pack-reused 46246
    Receiving objects: 100% (49241/49241), 63.87 MiB | 1.84 MiB/s, done.
    Resolving deltas: 100% (25723/25723), done.
    Updating files: 100% (1230/1230), done.
    
    $ cd kubebuilder
    $ git tag # 列出git目录下所有tag
    release-0.1.6
    tools-1.10.1
    tools-1.11.0
    v0.1.10
    v0.1.11
    v0.1.12
    ....
    $ git checkout v3.2.0 # 目前最新tag为v3.2.0,可根据开发需求选择合适的版本
    Note: switching to 'v3.2.0'.
    
    You are in 'detached HEAD' state. You can look around, make experimental
    changes and commit them, and you can discard any commits you make in this
    state without impacting any branches by switching back to a branch.
    
    If you want to create a new branch to retain commits you create, you may
    do so (now or later) by using -c with the switch command. Example:
    
      git switch -c <new-branch-name>
    
    Or undo this operation with:
    
      git switch -
    
    Turn off this advice by setting config variable advice.detachedHead to false
    
    HEAD is now at b7a730c8 Merge pull request #2409 from camilamacedo86/release-3
    
    # 检查是否切换到想要的版本
    $ git branch
    * (HEAD detached at v3.2.0)
      master
    
    # 编译kubebuilder二进制文件
    $ make build
    go build -ldflags " -X main.kubeBuilderVersion=v3.2.0 -X main.goos=linux -X main.goarch=amd64 -X main.gitCommit=b7a730c84495122a14a0faff95e9e9615fffbfc5 -X main.buildDate=2021-12-14T06:30:08Z " -o bin/kubebuilder ./cmd
    go: downloading github.com/gobuffalo/flect v0.2.3
    go: downloading sigs.k8s.io/controller-tools v0.7.0
    go: downloading sigs.k8s.io/kustomize/kyaml v0.10.21
    go: downloading k8s.io/api v0.22.2
    go: downloading k8s.io/apiextensions-apiserver v0.22.2
    
    # 复制二进制文件到指定目录,方便后期使用
    $ cp bin/kubebuilder /usr/local/bin/

相关文档