$ kubectl get ep hello-go-6wdlw
NAME ENDPOINTS AGE
hello-go-6wdlw 10.244.0.71:8012 2d21h
$ kubectl -n knative-serving get pod activator-68cbc9b5c7-6fsc2 -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
activator-68cbc9b5c7-6fsc2 1/1 Running 0 5d17h 10.244.0.71 n227-020-128 <none> <none>
$ kubectl get ep hello-go-6wdlw
NAME ENDPOINTS AGE
hello-go-6wdlw 10.244.0.102:8012,10.244.0.103:8012,10.244.0.104:8012 + 1 more... 2d22h
小结
因为 serving 涉及的 CRD 比较多,这里再把它们统一放这里梳理一下,一图胜千言:
基本功能-eventing
本小节会按照“从零开始写一个 go 应用”的流程,来梳理功能
编写 go 代码
$ cat hello.go
package main
import (
"context"
"log"
cloudevents "github.com/cloudevents/sdk-go/v2"
)
type HelloWorld struct {
Msg string `json:"msg,omitempty"`
}
func receive(ctx context.Context, event cloudevents.Event) (*cloudevents.Event, cloudevents.Result) {
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)
return nil, nil
}
func main() {
c, err := cloudevents.NewDefaultClient()
if err != nil {
log.Fatalf("failed to create client, %v", err)
}
log.Fatal(c.StartReceiver(context.Background(), receive))
}
$ cat go.mod
module hello-eventing
go 1.13
require github.com/cloudevents/sdk-go/v2 v2.0.0-RC2
在 eventing 体系下编写 go 应用,需要借助 cloudevents sdk
构造镜像
$ cat Dockerfile
FROM golang:1.13 as builder
WORKDIR /app
ENV GOPROXY https://goproxy.cn,direct
COPY go.* ./
RUN go mod download
COPY . ./
RUN CGO_ENABLED=0 GOOS=linux go build -v -o server
FROM alpine:3
RUN apk add --no-cache ca-certificates
COPY --from=builder /app/server /server
CMD ["/server"]
$ docker build . -t dev.local/hello-go:eventing
$ kubectl -n knative-samples get broker
NAME URL AGE READY REASON
default http://broker-ingress.knative-eventing.svc.cluster.local/knative-samples/default 4m31s True
mt-broker-controller 将 channel 地址填充到 broker status
$ kubectl -n knative-samples get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default-kne-trigger-kn-channel ExternalName <none> imc-dispatcher.knative-eventing.svc.cluster.local <none> 16h
eventing-controller 依据 broker 创建了对应的 imc
$ kubectl -n knative-samples get imc
NAME URL AGE READY REASON
default-kne-trigger http://default-kne-trigger-kn-channel.knative-samples.svc.cluster.local 4m35s True