K8s Operator总结----(二) kubebuilder 安装测试

简介: kubebuilder 安装测试

kuberbuilder安装

安装go环境,在ubuntu下

  1. Remove any previous Go installation

    by deleting the /usr/local/go folder (if it exists), then extract the archive you just downloaded into /usr/local, creating a fresh Go tree in /usr/local/go:

$ rm -rf /usr/local/go && tar -C /usr/local -xzf go1.21.3.linux-amd64.tar.gz

  1. (You may need to run the command as root or through sudo).
    Do not untar the archive into an existing /usr/local/go tree. This is known to produce broken Go installations.
  2. Add /usr/local/go/bin to the  PATH  environment variable.
    You can do this by adding the following line to your $HOME/.profile or /etc/profile (for a system-wide installation):

export PATH=$PATH:/usr/local/go/bin

  1. Note: Changes made to a profile file may not apply until the next time you log into your computer. To apply the changes immediately, just run the shell commands directly or execute them from the profile using a command such as source $HOME/.profile.
  2. Verify that you've installed Go by opening a command prompt and typing the following command:

$ go version

  1. Confirm that the command prints the installed version of Go.

安装kubebuilder

# download kubebuilder and install locally.

curl-L-o kubebuilder "https://go.kubebuilder.io/dl/latest/$(go env GOOS)/$(go env GOARCH)"

# 注意如果是直接下载,可能需要修改下文件名字

chmod+x kubebuilder && mv kubebuilder /usr/local/bin/

创建demo

如果出现不能下载的情况,执行下面的命令,对go mod配置proxy,走代理下载: export GOPROXY=https://goproxy.io

export GOPROXY=https://goproxy.io

如果已经创建了,想覆盖,增加--force选型。

user@k8s-master:~/demo1$ kubebuilder create api --group webapp --kind Welcome --version v1 --force

INFO Create Resource [y/n]                        

y

INFO Create Controller [y/n]                      

y

INFO Writing kustomize manifests for you to edit...

INFO Writing scaffold for you to edit...          

INFO api/v1/welcome_types.go                      

INFO api/v1/groupversion_info.go                  

INFO internal/controller/suite_test.go            

INFO internal/controller/welcome_controller.go    

INFO Update dependencies:

$ go mod tidy          

INFO Running make:

$ make generate                

mkdir-p /home/user/demo1/bin

test -s /home/user/demo1/bin/controller-gen && /home/user/demo1/bin/controller-gen --version | grep-q v0.13.0 || \

GOBIN=/home/user/demo1/bin go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.13.0

go: downloading sigs.k8s.io/controller-tools v0.13.0

go: downloading github.com/spf13/cobra v1.7.0

go: downloading github.com/gobuffalo/flect v1.0.2

go: downloading k8s.io/apiextensions-apiserver v0.28.0

go: downloading k8s.io/apimachinery v0.28.0

go: downloading golang.org/x/tools v0.12.0

go: downloading github.com/fatih/color v1.15.0

go: downloading k8s.io/api v0.28.0

go: downloading github.com/mattn/go-colorable v0.1.13

go: downloading github.com/mattn/go-isatty v0.0.17

go: downloading golang.org/x/sys v0.11.0

go: downloading golang.org/x/net v0.14.0

go: downloading golang.org/x/mod v0.12.0

go: downloading golang.org/x/text v0.12.0

/home/user/demo1/bin/controller-gen object:headerFile="hack/boilerplate.go.txt"paths="./..."

Next: implement your new API and generate the manifests (e.g. CRDs,CRs) with:

$ make manifests

deployment, err :=r.createWelcomeDeployment(welcome)

iferr!=nil {

returnctrl.Result{}, err

}

log.Info("create deployment success!")

svc, err :=r.createService(welcome)

iferr!=nil {

returnctrl.Result{}, err

}

log.Info("create service success!")

applyOpts := []client.PatchOption{client.ForceOwnership, client.

FieldOwner("welcome_controller")}

err=r.Patch(ctx, &deployment, client.Apply, applyOpts...)

例子welcome.go

 1packagemain

 2  

 3import (

 4         "fmt"

 5         "net/http"

 6         "os"

 7 )

 8funcmain() {

 9       name :=os.Getenv("NAME")

10       hello :=fmt.Sprintf("Hello %s ", name)

11       http.Handle("/hello/", http.StripPrefix("/hello/", http.FileServer(http.Dir("static"))))

12       f, err :=os.OpenFile("./static/index.html", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)

13       iferr!=nil {

14            panic(err)

15       }

16       deferf.Close()

17       if_, err=f.WriteString(hello); err!=nil {

18            panic(err)

19       }

20       port :=os.Getenv("PORT")

21       ifport=="" {

22       port="8080"

23       }

24       // Start the web service on the specified port

25       err=http.ListenAndServe(":"+port, nil)

26       iferr!=nil {

27       panic(err)

28       }

29 }

对应的dockerfile

 1 FROM golang:1.12 as builder

 2 # Copy local code to the container image.

 3 WORKDIR /

 4 COPY . .

 5 COPY static /static

 6 # Build the command inside the container.

 7 RUN CGO_ENABLED=0 GOOS=linux go build -v -o main

 8 # Use a Docker multi-stage build to create a lean production image.

 9 FROM alpine

10 RUN apk add --no-cache ca-certificates

11 # Copy the binary to the production image from the builder stage.

12 COPY --from=builder /main /usr/local/main

13 COPY --from=builder /static /static

14 # Run the web service on container startup.

15 CMD ["/usr/local/main"]

运行

user@gitlabrunner:~/tmp$ docker build -t welcome_operator:v1 .


相关实践学习
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。     相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
目录
相关文章
|
运维 关系型数据库 MySQL
os-copilot安装_配置_功能测试全集
我是一位中级运维工程师,我平时工作会涉及到 各类服务器的 数据库 与 java环境配置 操作。 我顺利使用了OS Copilot的 -t -f | 功能,我的疑惑是不能在自动操作过程中直接给与脚本运行权限,必须需要自己运行一下 chmod 这个既然有了最高的权限,为什么就不能直接给与运行权限呢。 我认为 -t 功能有用,能解决后台运行基础命令操作。 我认为 -f 功能有用,可以通过task文件中撰写连续任务操作。 我认为 | 对文件理解上有很直接的解读,可以在理解新程序上有很大帮助。
381 86
|
12月前
|
存储 人工智能 编译器
【03】鸿蒙实战应用开发-华为鸿蒙纯血操作系统Harmony OS NEXT-测试hello word效果-虚拟华为手机真机环境调试-为DevEco Studio编译器安装中文插件-测试写一个滑动块效果-介绍诸如ohos.ui等依赖库-全过程实战项目分享-从零开发到上线-优雅草卓伊凡
【03】鸿蒙实战应用开发-华为鸿蒙纯血操作系统Harmony OS NEXT-测试hello word效果-虚拟华为手机真机环境调试-为DevEco Studio编译器安装中文插件-测试写一个滑动块效果-介绍诸如ohos.ui等依赖库-全过程实战项目分享-从零开发到上线-优雅草卓伊凡
799 11
【03】鸿蒙实战应用开发-华为鸿蒙纯血操作系统Harmony OS NEXT-测试hello word效果-虚拟华为手机真机环境调试-为DevEco Studio编译器安装中文插件-测试写一个滑动块效果-介绍诸如ohos.ui等依赖库-全过程实战项目分享-从零开发到上线-优雅草卓伊凡
|
存储 运维 Kubernetes
正式开源,Doris Operator 支持高效 Kubernetes 容器化部署方案
飞轮科技推出了 Doris 的 Kubernetes Operator 开源项目(简称:Doris Operator),并捐赠给 Apache 基金会。该工具集成了原生 Kubernetes 资源的复杂管理能力,并融合了 Doris 组件间的分布式协同、用户集群形态的按需定制等经验,为用户提供了一个更简洁、高效、易用的容器化部署方案。
632 16
正式开源,Doris Operator 支持高效 Kubernetes 容器化部署方案
|
10月前
|
SQL 缓存 关系型数据库
MySQL8.4 Enterprise安装Firewall及测试
MySQL8.4 Enterprise安装Firewall及测试
348 0
|
11月前
|
人工智能 运维 Kubernetes
2025 超详细!Lens Kubernetes IDE 多平台下载安装与集群管理教程
Lens 是一款企业级 Kubernetes 可视化操作平台,2025版实现了三大技术革新:AI智能运维(异常检测准确率98.7%)、多云联邦管理(支持50+集群)和实时3D拓扑展示。本文介绍其安装环境、配置流程、核心功能及高阶技巧,帮助用户快速上手并解决常见问题。适用于 Windows、macOS 和 Ubuntu 系统,需满足最低配置要求并前置依赖组件如 kubectl 和 Helm。通过 Global Cluster Hub 实现多集群管理,AI辅助故障诊断提升运维效率,自定义监控看板和插件生态扩展提供更多功能。
|
11月前
|
人工智能 测试技术 API
Windows用户必备:Postman v11详细安装指南与API测试入门教程(附官网下载
Postman是全球领先的API开发与测试工具,支持REST、SOAP、GraphQL等协议调试。2025年最新版v11新增AI智能生成测试用例、多环境变量同步等功能,适用于前后端分离开发、自动化测试、接口文档自动生成及团队协作共享API资源。本文详细介绍Postman的软件定位、核心功能、安装步骤、首次配置、基础使用及常见问题解答,帮助用户快速上手并高效利用该工具进行API开发与测试。
|
弹性计算 Ubuntu Java
OS-Copilot-ubuntu镜像版本的具体测试使用(安装方式有单独注明)
作为一名个人开发者,我主要负责云资源的运维和管理。在使用OS Copilot的过程中,我遇到了一些配置问题,特别是在ECS实例中设置AccessKey时,但最终成功解决了。通过使用OS Copilot的-t/-f/管道功能,我大大提升了效率,减少了命令编写的工作量,特别是在搭建Java运行环境时效果显著。此外,| 功能帮助我快速理解文档,整体体验非常流畅,推荐给其他开发者使用。
314 6
|
弹性计算 运维 Ubuntu
os-copilot在Alibaba Cloud Linux镜像下的安装与功能测试
我顺利使用了OS Copilot的 -t -f 功能,我的疑惑是在换行的时候就直接进行提问了,每次只能写一个问题,没法连续换行更有逻辑的输入问题。 我认为 -t 管道 功能有用 ,能解决环境问题的连续性操作。 我认为 -f 管道 功能有用 ,可以单独创建可连续性提问的task问题。 我认为 | 对文件直接理解在新的服务器理解有很大的帮助。 此外,我还有建议 可以在非 co 的环境下也能进行连续性的提问。
280 7
|
Kubernetes 应用服务中间件 nginx
二进制安装Kubernetes(k8s)v1.32.0
本指南提供了一个详细的步骤,用于在Linux系统上通过二进制文件安装Kubernetes(k8s)v1.32.0,支持IPv4+IPv6双栈。具体步骤包括环境准备、系统配置、组件安装和配置等。
4572 11
|
自然语言处理 机器人 Python
ChatGPT使用学习:ChatPaper安装到测试详细教程(一文包会)
ChatPaper是一个基于文本生成技术的智能研究论文工具,能够根据用户输入进行智能回复和互动。它支持快速下载、阅读论文,并通过分析论文的关键信息帮助用户判断是否需要深入了解。用户可以通过命令行或网页界面操作,进行论文搜索、下载、总结等。
417 1
ChatGPT使用学习:ChatPaper安装到测试详细教程(一文包会)

推荐镜像

更多