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

本文涉及的产品
Serverless 应用引擎 SAE,800核*时 1600GiB*时
性能测试 PTS,5000VUM额度
服务治理 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 .


相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
目录
相关文章
|
5天前
|
存储 Kubernetes 容器
Kubernetes(K8S) kubesphere 安装
Kubernetes(K8S) kubesphere 安装
25 4
|
6天前
|
Kubernetes Linux Docker
Kubernetes(K8S) helm 安装
Kubernetes(K8S) helm 安装
15 1
|
13天前
|
Kubernetes 应用服务中间件 nginx
debian11使用kubeadm安装k8s
debian11使用kubeadm安装k8s
|
23天前
|
Linux
Linux telnet安装及端口测试联通性
Linux telnet安装及端口测试联通性
37 10
|
1天前
|
Kubernetes 安全 搜索推荐
在K8S中,二进制与Kubeadm安装有何区别?
在K8S中,二进制与Kubeadm安装有何区别?
|
1天前
|
存储 Kubernetes 监控
在K8S中,集群安装过程有哪些?
在K8S中,集群安装过程有哪些?
|
25天前
|
Kubernetes 调度 数据库
Kubernetes架构及安装
Kubernetes架构及安装
78 3
|
6天前
|
存储 Kubernetes 关系型数据库
Kubernetes(K8S) 安装Nacos,报 No DataSource set
Kubernetes(K8S) 安装Nacos,报 No DataSource set
13 0
|
6天前
|
Kubernetes 容器
Kubernetes(K8S) 安装 Metrics-Server kubectl top (metrics-server) node NotFound
Kubernetes(K8S) 安装 Metrics-Server kubectl top (metrics-server) node NotFound
16 0
|
6天前
|
Kubernetes 负载均衡 应用服务中间件
使用 Kubeadm 部署 Kubernetes(K8S) 安装 -- Ingress-Ngnix
使用 Kubeadm 部署 Kubernetes(K8S) 安装 -- Ingress-Ngnix
17 0

推荐镜像

更多