2024年Java最新kubebuilder实战之二:初次体验kubebuilder,蚂蚁金服4面Java高级开发

Kafka实战笔记

关于这份笔记,为了不影响大家的阅读体验,我只能在文章中展示部分的章节内容和核心截图

image.png

  • Kafka入门
  • 为什么选择Kafka
  • Karka的安装、管理和配置

image.png

  • Kafka的集群
  • 第一个Kafka程序
  • image.png

afka的生产者

image.png

  • Kafka的消费者
  • 深入理解Kafka
  • 可靠的数据传递

image.png

image.png

  • Spring和Kalka的整合
  • Sprinboot和Kafka的整合
  • Kafka实战之削峰填谷
  • 数据管道和流式处理(了解即可)

image.png

  • Kafka实战之削峰填谷

image.png

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

import (

“context”

“github.com/go-logr/logr”

“k8s.io/apimachinery/pkg/runtime”

ctrl “sigs.k8s.io/controller-runtime”

“sigs.k8s.io/controller-runtime/pkg/client”

webappv1 “helloworld/api/v1”

)

// GuestbookReconciler reconciles a Guestbook object

type GuestbookReconciler struct {

client.Client

Log logr.Logger

Scheme *runtime.Scheme

}

// +kubebuilder:rbac:groups=webapp.com.bolingcavalry,resources=guestbooks,verbs=get;list;watch;create;update;patch;delete

// +kubebuilder:rbac:groups=webapp.com.bolingcavalry,resources=guestbooks/status,verbs=get;update;patch

func (r *GuestbookReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {

_ = context.Background()

_ = r.Log.WithValues(“guestbook”, req.NamespacedName)

// your logic here

return ctrl.Result{}, nil

}

func (r *GuestbookReconciler) SetupWithManager(mgr ctrl.Manager) error {

return ctrl.NewControllerManagedBy(mgr).

For(&webappv1.Guestbook{}).

Complete®

}

  1. 本文以体验基本流程为主,不深入研究源码,所以对上面的代码仅做少量修改,用于验证是否能生效,改动如下图红框所示:

在这里插入图片描述

  1. 执行以下命令,会编译并启动刚才修改的controller:

cd $GOPATH/src/helloworld

make run

  1. 此时控制台输出以下内容,这里要注意,controller是在kubebuilder电脑上运行的,一旦使用Ctrl+c中断控制台,就会导致controller停止:

[root@kubebuilder helloworld]# make run

/root/gopath/bin/controller-gen object:headerFile=“hack/boilerplate.go.txt” paths=“./…”

go fmt ./…

go vet ./…

/root/gopath/bin/controller-gen “crd:trivialVersions=true” rbac:roleName=manager-role webhook paths=“./…” output:crd:artifacts:config=config/crd/bases

go run ./main.go

2021-01-23T20:58:35.107+0800 INFO controller-runtime.metrics metrics server is starting to listen {“addr”: “:8080”}

2021-01-23T20:58:35.108+0800 INFO setup starting manager

2021-01-23T20:58:35.108+0800 INFO controller-runtime.manager starting metrics server {“path”: “/metrics”}

2021-01-23T20:58:35.108+0800 INFO controller-runtime.controller Starting EventSource {“controller”: “guestbook”, “source”: “kind source: /, Kind=”}

2021-01-23T20:58:35.208+0800 INFO controller-runtime.controller Starting Controller {“controller”: “guestbook”}

2021-01-23T20:58:35.209+0800 INFO controller-runtime.controller Starting workers {“controller”: “guestbook”, “worker count”: 1}

创建Guestbook资源的实例

  1. 现在kubernetes已经部署了Guestbook类型的CRD,而且对应的controller也已正在运行中,可以尝试创建Guestbook类型的实例了(相当于有了pod的定义后,才可以创建pod);

  2. kubebuilder已经自动创建了一个类型的部署文件:$GOPATH/src/helloworld/config/samples/webapp_v1_guestbook.yaml ,内容如下,很简单,接下来咱们就用这个文件来创建Guestbook实例:

apiVersion: webapp.com.bolingcavalry/v1

kind: Guestbook

metadata:

name: guestbook-sample

spec:

Add fields here

foo: bar

  1. 重新打开一个控制台,登录kubebuilder电脑,执行以下命令即可创建Guestbook类型的实例:

cd $GOPATH/src/helloworld

kubectl apply -f config/samples/

  1. 如下所示,控制台提示资源创建成功:

[root@kubebuilder helloworld]# kubectl apply -f config/samples/

guestbook.webapp.com.bolingcavalry/guestbook-sample created

  1. 用kubectl get命令可以看到实例已经创建:

[root@kubebuilder helloworld]# kubectl get Guestbook

NAME AGE

guestbook-sample 112s

  1. 用命令kubectl edit Guestbook guestbook-sample编辑该实例,修改的内容如下图红框所示:

在这里插入图片描述

  1. 此时去controller所在控制台,可以看到新增和修改的操作都有日志输出,咱们新增的日志都在里面,代码调用栈一目了然:

2021-01-24T09:51:50.418+0800 INFO controllers.Guestbook 1. default/guestbook-sample

2021-01-24T09:51:50.418+0800 INFO controllers.Guestbook 2. goroutine 188 [running]:

runtime/debug.Stack(0xc0002a1808, 0xc0002fc600, 0x1b)

/root/go/src/runtime/debug/stack.go:24 +0x9f

helloworld/controllers.(*GuestbookReconciler).Reconcile(0xc0003c9dd0, 0xc0002d02f9, 0x7, 0xc0002d02e0, 0x10, 0x12f449647b, 0xc000456f30, 0xc000456ea8, 0xc000456ea0)

/root/gopath/src/helloworld/controllers/guestbook_controller.go:49 +0x1a9

sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler(0xc00022a480, 0x1430e00, 0xc0003e7560, 0x0)

/root/gopath/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/internal/controller/controller.go:256 +0x166

sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem(0xc00022a480, 0xc000469600)

/root/gopath/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/internal/controller/controller.go:232 +0xb0

sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).worker(0xc00022a480)

/root/gopath/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/internal/controller/controller.go:211 +0x2b

k8s.io/apimachinery/pkg/util/wait.JitterUntil.func1(0xc000292980)

/root/gopath/pkg/mod/k8s.io/apimachinery@v0.17.2/pkg/util/wait/wait.go:152 +0x5f

k8s.io/apimachinery/pkg/util/wait.JitterUntil(0xc000292980, 0x3b9aca00, 0x0, 0x1609101, 0xc000102480)

/root/gopath/pkg/mod/k8s.io/apimachinery@v0.17.2/pkg/util/wait/wait.go:153 +0x105

k8s.io/apimachinery/pkg/util/wait.Until(0xc000292980, 0x3b9aca00, 0xc000102480)

/root/gopath/pkg/mod/k8s.io/apimachinery@v0.17.2/pkg/util/wait/wait.go:88 +0x4d

created by sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func1

/root/gopath/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/internal/controller/controller.go:193 +0x32d

2021-01-24T09:51:50.418+0800 DEBUG controller-runtime.controller Successfully Reconciled {“controller”: “guestbook”, “request”: “default/guestbook-sample”}

2021-01-24T09:52:33.632+0800 INFO controllers.Guestbook 1. default/guestbook-sample

2021-01-24T09:52:33.633+0800 INFO controllers.Guestbook 2. goroutine 188 [running]:

runtime/debug.Stack(0xc0002a1808, 0xc0003fa5e0, 0x1b)

/root/go/src/runtime/debug/stack.go:24 +0x9f

helloworld/controllers.(*GuestbookReconciler).Reconcile(0xc0003c9dd0, 0xc0002d02f9, 0x7, 0xc0002d02e0, 0x10, 0x1d0410fe42, 0xc000456f30, 0xc000456ea8, 0xc000456ea0)

/root/gopath/src/helloworld/controllers/guestbook_controller.go:49 +0x1a9

sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler(0xc00022a480, 0x1430e00, 0xc0003d24c0, 0x0)

/root/gopath/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/internal/controller/controller.go:256 +0x166

sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem(0xc00022a480, 0xc000469600)

/root/gopath/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/internal/controller/controller.go:232 +0xb0

sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).worker(0xc00022a480)

/root/gopath/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/internal/controller/controller.go:211 +0x2b

k8s.io/apimachinery/pkg/util/wait.JitterUntil.func1(0xc000292980)

/root/gopath/pkg/mod/k8s.io/apimachinery@v0.17.2/pkg/util/wait/wait.go:152 +0x5f

k8s.io/apimachinery/pkg/util/wait.JitterUntil(0xc000292980, 0x3b9aca00, 0x0, 0x1609101, 0xc000102480)

/root/gopath/pkg/mod/k8s.io/apimachinery@v0.17.2/pkg/util/wait/wait.go:153 +0x105

k8s.io/apimachinery/pkg/util/wait.Until(0xc000292980, 0x3b9aca00, 0xc000102480)

/root/gopath/pkg/mod/k8s.io/apimachinery@v0.17.2/pkg/util/wait/wait.go:88 +0x4d

created by sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func1

/root/gopath/pkg/mod/sigs.k8s.io/controller-runtime@v0.5.0/pkg/internal/controller/controller.go:193 +0x32d

2021-01-24T09:52:33.633+0800 DEBUG controller-runtime.controller Successfully Reconciled {“controller”: “guestbook”, “request”: “default/guestbook-sample”}

删除实例并停止controller

  1. 不再需要Guestbook实例的时候,执行以下命令即可删除:

cd $GOPATH/src/helloworld

kubectl delete -f config/samples/

  1. 不再需要controller的时候,去它的控制台使用Ctrl+c中断即可;

将controller制作成docker镜像

  1. 至此,咱们已经体验过了kubebuilder的基本功能,不过实际生产环境中controller一般都会运行在kubernetes环境内,像上面这种运行在kubernetes之外的方式就不合适了,咱们来试试将其做成docker镜像然后在kubernetes环境运行;

  2. 这里有个要求,就是您要有个kubernetes可以访问的镜像仓库,例如局域网内的Harbor,或者公共的hub.docker.com,我这为了操作方便选择了hub.docker.com,使用它的前提是拥有hub.docker.com的注册帐号;

  3. 在kubebuilder电脑上,打开一个控制台,执行docker login命令登录,根据提示输入hub.docker.com的帐号和密码,这样就可以在当前控制台上执行docker push命令将镜像推送到hub.docker.com上了(这个网站的网络很差,可能要登录好几次才能成功);

  4. 执行以下命令构建docker镜像并推送到hub.docker.com,镜像名为bolingcavalry/guestbook:002:

cd $GOPATH/src/helloworld

make docker-build docker-push IMG=bolingcavalry/guestbook:002

  1. hub.docker.com的网络状况不是一般的差,kubebuilder电脑上的docker一定要设置镜像加速,上述命令如果遭遇超时失败,请重试几次,此外,构建过程中还会下载诸多go模块的依赖,也需要您耐心等待,也很容易遇到网络问题,需要多次重试,所以,最好是使用局域网内搭建的Habor服务;

  2. 最终,命令执行成功后输出如下:

[root@kubebuilder helloworld]# make docker-build docker-push IMG=bolingcavalry/guestbook:002

/root/gopath/bin/controller-gen object:headerFile=“hack/boilerplate.go.txt” paths=“./…”

go fmt ./…

go vet ./…

/root/gopath/bin/controller-gen “crd:trivialVersions=true” rbac:roleName=manager-role webhook paths=“./…” output:crd:artifacts:config=config/crd/bases

go test ./… -coverprofile cover.out

? helloworld [no test files]

? helloworld/api/v1 [no test files]

ok helloworld/controllers 8.604s coverage: 0.0% of statements

docker build . -t bolingcavalry/guestbook:002

Sending build context to Docker daemon 40.27MB

Step 1/14 : FROM golang:1.13 as builder

—> d6f3656320fe

Step 2/14 : WORKDIR /workspace

—> Using cache

—> 83d05ead1041

Step 3/14 : COPY go.mod go.mod

—> Using cache

—> ae3e15a529f4

Step 4/14 : COPY go.sum go.sum

—> Using cache

—> 082223532ccc

Step 5/14 : RUN go mod download

—> Using cache

—> bcdcfa1d65ca

Step 6/14 : COPY main.go main.go

—> Using cache

—> 81d6a629ca98

Step 7/14 : COPY api/ api/

—> Using cache

—> 75f99b174e97

Step 8/14 : COPY controllers/ controllers/

—> b130d9f47903

Step 9/14 : RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go

—> Running in 768880aca19f

Removing intermediate container 768880aca19f

—> bb4a494d3b43

Step 10/14 : FROM gcr.io/distroless/static:nonroot

—> 947e6f3ed7c1

Step 11/14 : WORKDIR /

—> Using cache

—> 22cc43cef8fb

Step 12/14 : COPY --from=builder /workspace/manager .

—> 2137778f22c0

Step 13/14 : USER nonroot:nonroot

—> Running in 18295673073d

Removing intermediate container 18295673073d

—> f7545379ab1f

Step 14/14 : ENTRYPOINT [“/manager”]

—> Running in 550c47dd61dc

Removing intermediate container 550c47dd61dc

—> 31cb31a6b03f

Successfully built 31cb31a6b03f

Successfully tagged bolingcavalry/guestbook:002

docker push bolingcavalry/guestbook:002

The push refers to repository [docker.io/bolingcavalry/guestbook]

99035107a955: Pushed

728501c5607d: Layer already exists

002: digest: sha256:54f8ec88511cce5b04c5d65cc15e0f7a7b4a8afb6b235904a638bff79e3c5784 size: 739

  1. 去hub.docker.com网站看看,如下图,新镜像已经上传,这样只要任何机器只要能上网就能pull此镜像到本地使用了:

在这里插入图片描述

  1. 镜像准备好之后,执行以下命令即可在kubernetes环境部署controller:

cd $GOPATH/src/helloworld

make deploy IMG=bolingcavalry/guestbook:002

  1. 控制台会提示各类资源被创建(rbac居多):

[root@kubebuilder ~]# cd $GOPATH/src/helloworld

[root@kubebuilder helloworld]# make deploy IMG=bolingcavalry/guestbook:002

/root/gopath/bin/controller-gen “crd:trivialVersions=true” rbac:roleName=manager-role webhook paths=“./…” output:crd:artifacts:config=config/crd/bases

cd config/manager && kustomize edit set image controller=bolingcavalry/guestbook:002

kustomize build config/default | kubectl apply -f -

namespace/helloworld-system created

Warning: apiextensions.k8s.io/v1beta1 CustomResourceDefinition is deprecated in v1.16+, unavailable in v1.22+; use apiextensions.k8s.io/v1 CustomResourceDefinition

customresourcedefinition.apiextensions.k8s.io/guestbooks.webapp.com.bolingcavalry configured

role.rbac.authorization.k8s.io/helloworld-leader-election-role created

clusterrole.rbac.authorization.k8s.io/helloworld-manager-role created

clusterrole.rbac.authorization.k8s.io/helloworld-proxy-role created

Warning: rbac.authorization.k8s.io/v1beta1 ClusterRole is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRole

clusterrole.rbac.authorization.k8s.io/helloworld-metrics-reader created

rolebinding.rbac.authorization.k8s.io/helloworld-leader-election-rolebinding created

clusterrolebinding.rbac.authorization.k8s.io/helloworld-manager-rolebinding created

clusterrolebinding.rbac.authorization.k8s.io/helloworld-proxy-rolebinding created

service/helloworld-controller-manager-metrics-service created

deployment.apps/helloworld-controller-manager created

最后

分享一些系统的面试题,大家可以拿去刷一刷,准备面试涨薪。

这些面试题相对应的技术点:

  • JVM
  • MySQL
  • Mybatis
  • MongoDB
  • Redis
  • Spring
  • Spring boot
  • Spring cloud
  • Kafka
  • RabbitMQ
  • Nginx

大类就是:

  • Java基础
  • 数据结构与算法
  • 并发编程
  • 数据库
  • 设计模式
  • 微服务
  • 消息中间件

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

程序员,每个月给你发多少工资,你才会想老板想的事?

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

inx

大类就是:

  • Java基础
  • 数据结构与算法
  • 并发编程
  • 数据库
  • 设计模式
  • 微服务
  • 消息中间件

[外链图片转存中…(img-ot2ajcuf-1714922730847)]

[外链图片转存中…(img-AyTHf1mZ-1714922730847)]

[外链图片转存中…(img-AoLLC7df-1714922730848)]

[外链图片转存中…(img-Up8zsrPI-1714922730848)]

[外链图片转存中…(img-9KS9nfCC-1714922730849)]

[外链图片转存中…(img-N1sXuk7V-1714922730849)]

[外链图片转存中…(img-imCScUwl-1714922730849)]

[外链图片转存中…(img-f7pmDbuK-1714922730850)]

[外链图片转存中…(img-SYyFV3l4-1714922730850)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值