快速安装
前提
需要本地安装 k8s,可以尝试使用 orbstack,目前我使用的就是这个工具
为了更好的体验 knative-eventing 的使用,还需要你本地有 golang 的环境以及 docker
安装部署
通过以下命令安装 knative eventing 相关组件
kubectl apply -f https://github.com/knative/eventing/releases/download/knative-v1.12.0/eventing-crds.yaml
kubectl apply -f https://github.com/knative/eventing/releases/download/knative-v1.12.0/eventing-core.yaml
kubectl apply -f https://github.com/knative/eventing/releases/download/knative-v1.12.0/in-memory-channel.yaml
kubectl apply -f https://github.com/knative/eventing/releases/download/knative-v1.12.0/mt-channel-broker.yaml
执行 kubectl get po -A 你应该可以看到如下信息
{19:55}~ ➭ kubectl get po -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system local-path-provisioner-957fdf8bc-ws7bh 1/1 Running 34 (2d1h ago) 64d
kube-system coredns-687f7d69ff-2pmhp 1/1 Running 21 (2d1h ago) 64d
knative-eventing eventing-webhook-68575ffb75-n8kcg 1/1 Running 0 16m
knative-eventing eventing-controller-5d4cd67984-djtgq 1/1 Running 0 16m
knative-eventing imc-dispatcher-7d9f7b5bc7-8p2g5 1/1 Running 0 14m
knative-eventing imc-controller-74bc76b745-v8lpw 1/1 Running 0 14m
knative-eventing mt-broker-filter-755d6d6454-c86kr 1/1 Running 0 10m
knative-eventing mt-broker-ingress-7bbdf9b6f8-9gp4r 1/1 Running 0 10m
knative-eventing mt-broker-controller-74895f6c47-gv4wh 1/1 Running 0 10m
快速体验
流程介绍
理解 knative eventing 的时候,总感觉很虚幻,因为其中传递的 cloudevent 是你很难看到摸到的,不过好在官方提供了一个 helloworld-go 的项目,可以帮助我们更好的理解 kantive eventing
这个项目做的事情就是部署了一个 deployment,这个 deployment 可以接收 cloudevent 并且将接收到的 cloudevent 展示在日志中
为了让大家更好的理解事件流动的过程,这个 deployment 还可以在接收到请求后,再构造一个请求发送出去,发送出去的请求,也让你可以在日志中看到
镜像准备
新建文件 helloworld.go,键入以下内容,这个是主要工作的部分
做的事情就是接收事件,在日志中展示,然后再构造一个事件发送出去
在这里面使用 sdk-go 来处理 cloudevent 的信息
package main
import (
"context"
"log"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/google/uuid"
)
func receive(ctx context.Context, event cloudevents.Event) (*cloudevents.Event, cloudevents.Result) {
// Here is where your code to process the event will go.
// In this example we will log the event msg
log.Printf("Event received. \n%s\n", event)
data := &HelloWorld{}
if err := event.DataAs(data); err != nil {
log.Printf("Error while extracting cloudevent Data: %s\n", err.Error())
return nil, cloudevents.NewHTTPResult(400, "failed to convert data: %s", err)
}
log.Printf("Hello World Message from received event %q", data.Msg)
// Respond with another event (optional)
// This is optional and is intended to show how to respond back with another event after processing.
// The response will go back into the knative eventing system just like any other event
newEvent := cloudevents.NewEvent()
// Setting the ID here is not necessary. When using NewDefaultClient the ID is set
// automatically. We set the ID anyway so it appears in the log.
newEvent.SetID(uuid.New().String())
newEvent.SetSource("knative/eventing/samples/hello-world")
newEvent.SetType("dev.knative.samples.hifromknative")
if err := newEvent.SetData(cloudevents.ApplicationJSON, HiFromKnative{Msg: "Hi from helloworld-go app!"}); err != nil {
return nil, cloudevents.NewHTTPResult(500, "failed to set response data: %s", err)
}
log.Printf("Responding with event\n%s\n", newEvent)
return &newEvent, nil
}
func main() {
log.Print("Hello world sample started.")
c, err := cloudevents.NewDefaultClient()
if err != nil {
log.Fatalf("failed to create client, %v", err)
}
log.Fatal(c.StartReceiver(context.Background(), receive))
}
新建文件 eventschemas.go,键入以下内容
这里的内容很简单,只是发送和接收消息的结构体
package main
// HelloWorld defines the Data of CloudEvent with type=dev.knative.samples.helloworld
type HelloWorld struct {
// Msg holds the message from the event
Msg string `json:"msg,omitempty"`
}
// HiFromKnative defines the Data of CloudEvent with type=dev.knative.samples.hifromknative
type HiFromKnative struct {
// Msg holds the message from the event
Msg string `json:"msg,omitempty"`
}
执行如下命令,创建 go.mod 文件
go mod init github.com/knative/docs/code-samples/serving/hello-world/helloworld-go
新建文件 Dockerfile,键入以下内容
# Use the official Golang image to create a build artifact.
# This is based on Debian and sets the GOPATH to /go.
# https://hub.docker.com/_/golang
FROM golang:1.19 as builder
ARG TARGETOS
ARG TARGETARCH
# Copy local code to the container image.
WORKDIR /app
# Retrieve application dependencies using go modules.
# Allows container builds to reuse downloaded dependencies.
COPY go.* ./
RUN go mod download
# Copy local code to the container image.
COPY . ./
# Build the binary.
# -mod=readonly ensures immutable go.mod and go.sum in container builds.
RUN CGO_ENABLED=0 GOOS=linux GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -mod=readonly -v -o helloworld
# Use a Docker multi-stage build to create a lean production image.
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM alpine:3
RUN apk add --no-cache ca-certificates
# Copy the binary to the production image from the builder stage.
COPY --from=builder /app/helloworld /helloworld
# Run the web service on container startup.
CMD ["/helloworld"]
好了,此时我们可以先把镜像构建出来,镜像构建地址随便,只要是你能访问到的地址即可
执行以下命令,这样构建出来的镜像就会上传到了我的镜像仓库下
docker buildx build -t "yugougou/helloworld-go" --push .
k8s 资源准备
新建 sample-app.yaml,键入以下内容
如果你的镜像地址不同,需要替换下 deployment 的镜像地址
这里的资源包含如下几个内容
1、构建出的镜像对应的 deployment 相关的 service
2、用于事件转发过滤的 broker 和 trigger
# Namespace for sample application
apiVersion: v1
kind: Namespace
metadata:
name: knative-samples
---
# A default broker
apiVersion: eventing.knative.dev/v1
kind: Broker
metadata:
name: default
namespace: knative-samples
annotations:
# Note: you can set the eventing.knative.dev/broker.class annotation to change the class of the broker.
# The default broker class is MTChannelBasedBroker, but Knative also supports use of the other class.
eventing.knative.dev/broker.class: MTChannelBasedBroker
spec: {}
---
# Knative Eventing Trigger to trigger the helloworld-go service
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
name: helloworld-go
namespace: knative-samples
spec:
broker: default
filter:
attributes:
type: dev.knative.samples.helloworld
source: dev.knative.samples/helloworldsource
subscriber:
ref:
apiVersion: v1
kind: Service
name: helloworld-go
---
# Helloworld-go app deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: helloworld-go
namespace: knative-samples
spec:
replicas: 1
selector:
matchLabels: &labels
app: helloworld-go
template:
metadata:
labels: *labels
spec:
containers:
- name: helloworld-go
image: docker.io/yugougou/helloworld-go
---
# Service that exposes helloworld-go app.
# This will be the subscriber for the Trigger
kind: Service
apiVersion: v1
metadata:
name: helloworld-go
namespace: knative-samples
spec:
selector:
app: helloworld-go
ports:
- protocol: TCP
port: 80
targetPort: 8080
执行如下命令,导入到 k8s 集群中
kubectl create -f sample-app.yaml
到目前为止,准备工作已经完成,我们可以开始体验事件传递的过程
体验事件接收
通过执行 kubectl -n knative-samples get broker default
获取到 broker 的地址
如下所示该例中为 http://broker-ingress.knative-eventing.svc.cluster.local/knative-samples/default
NAME URL AGE READY REASON
default http://broker-ingress.knative-eventing.svc.cluster.local/knative-samples/default 3d1h True
执行 kubectl -n knative-samples run curl --image=radial/busyboxplus:curl -it
进入 pod 中执行如下命令(地址记得换成上面获取到的 broker 中存储的地址) 发送事件到我们部署的 helloworld-go
中
curl -v "http://broker-ingress.knative-eventing.svc.cluster.local/knative-samples/default" \
-X POST \
-H "Ce-Id: 536808d3-88be-4077-9d7a-a3f162705f79" \
-H "Ce-Specversion: 1.0" \
-H "Ce-Type: dev.knative.samples.helloworld" \
-H "Ce-Source: dev.knative.samples/helloworldsource" \
-H "Content-Type: application/json" \
-d '{"msg":"Hello World from the curl pod."}'
执行如下命令,去查看我们部署的 helloworld-go
中的日志
kubectl --namespace knative-samples logs -l app=helloworld-go --tail=50
如下图所示,我们可以看到 hellowrold-go 已经接收到了 cloudevent 并且也尝试的往外发送了一个 cloudevent
体验事件发送
helloworld-go 发送的 cloudevent,我们又应该怎么来验证呢,怎么知道它发出的 cloudevent 是生效了还是没生效呢
当然有办法,把以下内容 apply 到集群后,使用 curl 的 pod 来发送 cloudevent,通过查看 event-display 的日志来验证是否接收到了发送的 cloudevent
# event-display app deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: event-display
namespace: knative-samples
spec:
replicas: 1
selector:
matchLabels: &labels
app: event-display
template:
metadata:
labels: *labels
spec:
containers:
- name: helloworld-go
# Source code: https://github.com/knative/eventing/tree/main/cmd/event_display
image: gcr.io/knative-releases/knative.dev/eventing/cmd/event_display
---
# Service that exposes event-display app.
# This will be the subscriber for the Trigger
kind: Service
apiVersion: v1
metadata:
name: event-display
namespace: knative-samples
spec:
selector:
app: event-display
ports:
- protocol: TCP
port: 80
targetPort: 8080
---
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
name: event-display
namespace: knative-samples
spec:
broker: default
filter:
attributes:
type: dev.knative.samples.hifromknative
source: knative/eventing/samples/hello-world
subscriber:
ref:
apiVersion: v1
kind: Service
name: event-display
执行 kubectl logs -f -n knative-samples deploy/event-display
查看日志
最终整个体验应该如下
Respect
knative eventing 安装
通过 helloworld-go 理解 eventing
其他
在看 helloworld-go README.md 的时候,发现 README.md 里面的代码示例是完全错误的,于是提了个相关的 PR,这里大家需要注意一下分辨,等 PR 合并后应该就没有这个问题了