kuberbuilder安装
安装go环境,在ubuntu下
- 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
- (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. - 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
- 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
. - Verify that you've installed Go by opening a command prompt and typing the following command:
$ go version
- 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 .