一个Kubebuilder的简单demo

安装kubebuilder

安装文档: https://book.kubebuilder.io/quick-start.html

快捷命令:

1# download kubebuilder and install locally.
2curl -L -o kubebuilder https://go.kubebuilder.io/dl/latest/$(go env GOOS)/$(go env GOARCH)
3chmod +x kubebuilder && mv kubebuilder /usr/local/bin/
4# 查看是否安装成功
5kubebuilder version

自动补全

~/.bash_profile~/.bashrc下增加以下内容

 1# kubebuilder autocompletion
 2echo 'source <(kubebuilder completion bash)' >>~/.bash_profile
 3source ~/.bash_profile
 4kubebuilder completion bash >/opt/homebrew/etc/bash_completion.d/kubebuilder
 5
 6# 增加kb简写自动补全
 7vim ~/.bash_profile
 8alias kb=kubebuilder
 9complete -o default -F __start_kubebuilder kb
10source ~/.bash_profile

重启命令行即可生效

快速体验

 1# 初始化项目
 2kubebuilder init --domain github.com --repo github.com/kubebuilder-demo/simple
 3
 4# 创建api
 5kubebuilder create api --group kubebuilder-demo --version v1 --kind Simple
 6# 生成crd
 7make manifests
 8# 安装crd到k8s集群,这里用于kustomize,如果没有会自动下载到./bin/,这里下载kustomize可能被qiang自己想办法
 9make install
10# 查看crd是否安装成功:simples.kubebuilder-demo.github.com
11kubectl get crd
12
13# 启动controller
14make run
15
16# 部署cr
17kubectl apply -f config/samples/kubebuilder-demo_v1_simple.yaml
18# 查看simple的cr是否部署成功
19kubectl get simple
20
21# 打包docker镜像
22# 这里发现 Dockerfile 里的 gcr.io/distroless/static:nonroot 镜像被qiang了,改为:devk8s/gcr-io_distroless_static:nonroot
23# Dockerfile golang builder 时要加 ENV GOPROXY="https://goproxy.cn,direct" 
24# 用docker-build时,k8s机器上报standard_init_linux.go:228: exec user process caused: exec format error,平台不兼容导致所以用docker-buildx
25make docker-buildx IMG=kubebuilderdemo/simple:0.0.1
26
27# 部署到k8s集群
28# config/default/manager_auth_proxy_patch.yaml下的 
29# gcr.io/kubebuilder/kube-rbac-proxy:v0.13.1 改为 devk8s/gcr-io_kubebuilder_kube-rbac-proxy:v0.13.1
30make deploy IMG=kubebuilderdemo/simple:0.0.1
31# 查看资源是否就绪
32kubectl -nsimple-system get svc
33kubectl -nsimple-system get deploy
34kubectl -nsimple-system get pod
35
36# pod启动不了的几个调试命令
37 kubectl -nsimple-system describe pod pod名称
38 kubectl -nsimple-system logs  pod名称 --all-containers
39 kubectl -nsimple-system logs pod名称 -c 容器名称
40 kubectl -nsimple-system get event
41 kubectl -nsimple-system get events --field-selector involvedObject.name=pod名称
42
43# 卸载crd
44make uninstall
45# 反部署
46make undeploy

注意点

  • go和docker镜像有些可能被qiang这块要处理
  • Dockerfile golang builder 时要加 ENV GOPROXY=“https://goproxy.cn,direct”
  • Dockerfile 里的 gcr.io/distroless/static:nonroot 镜像被qiang了,改为:devk8s/gcr-io_distroless_static:nonroot
  • config/default/manager_auth_proxy_patch.yaml下的 gcr.io/kubebuilder/kube-rbac-proxy:v0.13.1 改为 devk8s/gcr-io_kubebuilder_kube-rbac-proxy:v0.13.1
  • 如果在苹果芯片上build用make docker-buildx(多平台build)参考:Docker使用Buildx构建多平台镜像

项目地址

https://github.com/kubebuilder-demo/simple

发布日期:2023-06-06 20:03 字数:234 用时 2分钟
tags:K8s Kubebuilder