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

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 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 .


相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
目录
相关文章
|
2月前
|
Kubernetes 数据安全/隐私保护 Docker
|
3月前
|
C++
jrtplib开源库系列之一:jrtplib介绍、安装和测试(window 10环境介绍)
关于jrtplib库网上已经有很多介绍,而且目前jrtplib作者已经停止更新(Apr 18, 2020),最新版本为v3.11.2。本系列内容也以该版本进行介绍。 相信你已经对RTP/RTCP协议有一定的了解,并想更深入的了解RTP协议的具体实现,jrtplib就是使用使用C++实现的RTP/RTCP协议。具体标准为RFC3550,如果想仔细阅读原文,但是对英文又有点吃力,可以参考我的博客RTP/RTCP中英文对照,在博客的后面有百度链接,是对RFC3550的中文翻译,可能很多地方不太准确,有些内容是自己添加进去的,希望不会影响你的阅读。
34 0
|
2月前
|
Prometheus 监控 Kubernetes
如何用 Prometheus Operator 监控 K8s 集群外服务?
如何用 Prometheus Operator 监控 K8s 集群外服务?
|
5天前
|
分布式计算 Hadoop Scala
Spark【环境搭建 01】spark-3.0.0-without 单机版(安装+配置+测试案例)
【4月更文挑战第13天】Spark【环境搭建 01】spark-3.0.0-without 单机版(安装+配置+测试案例)
8 0
|
13天前
|
Kubernetes Linux 网络安全
kubeadm安装k8s
该文档提供了一套在CentOS 7.6上安装Docker和Kubernetes(kubeadm)的详细步骤,包括安装系统必备软件、关闭防火墙和SELinux、禁用swap、开启IP转发、设置内核参数、配置Docker源和加速器、安装指定版本Docker、启动Docker、设置kubelet开机启动、安装kubelet、kubeadm、kubectl、下载和配置Kubernetes镜像、初始化kubeadm、创建kubeconfig文件、获取节点加入集群命令、下载Calico YAML文件以及安装Calico。这些步骤不仅适用于v1.19.14,也适用于更高版本。
69 1
|
1月前
|
Kubernetes 测试技术 API
ChaosBlade常见问题之安装K8S探针心跳检测失败如何解决
ChaosBlade 是一个开源的混沌工程实验工具,旨在通过模拟各种常见的硬件、软件、网络、应用等故障,帮助开发者在测试环境中验证系统的容错和自动恢复能力。以下是关于ChaosBlade的一些常见问题合集:
19 0
|
1月前
|
Kubernetes 应用服务中间件 nginx
Kubernetes服务网络Ingress网络模型分析、安装和高级用法
Kubernetes服务网络Ingress网络模型分析、安装和高级用法
36 5
|
1月前
|
存储 Kubernetes 监控
KubeSphere平台安装系列之一【Kubernetes上安装KubeSphere(亲测--实操完整版)】(1/3)
KubeSphere平台安装系列之一【Kubernetes上安装KubeSphere(亲测--实操完整版)】(1/3)
40 0
|
1月前
|
存储 Kubernetes 监控
K8S集群上安装KubeSphere的详细过程
K8S集群上安装KubeSphere的详细过程
29 0
|
1月前
|
Kubernetes Linux Docker
深度解析:Kubernetes 1.28.2集群安装过程中的关键步骤
本文旨在为读者提供一份详尽的Kubernetes 1.28.2集群安装指南,帮助您从零开始构建稳定、高效的Kubernetes集群。我们将从环境准备、软件安装、集群初始化到节点添加等各个环节进行逐步讲解,确保您能够顺利完成集群的搭建。