日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁 > 科技  > 軟件

在 Kubernetes 環(huán)境中實(shí)現(xiàn) gRPC 負(fù)載均衡

來源: 責(zé)編: 時間:2023-10-17 09:38:22 296觀看
導(dǎo)讀前言前段時間寫過一篇 gRPC 的入門文章,在最后還留了一個坑沒有填:圖片也就是 gRPC 的負(fù)載均衡問題,因?yàn)楫?dāng)時的業(yè)務(wù)請求量不算大,再加上公司沒有對 Istio 這類服務(wù)網(wǎng)格比較熟悉的大牛,所以我們也就一直拖著沒有解決,依然只

前言

前段時間寫過一篇 gRPC 的入門文章,在最后還留了一個坑沒有填:5Fj28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片5Fj28資訊網(wǎng)——每日最新資訊28at.com

也就是 gRPC 的負(fù)載均衡問題,因?yàn)楫?dāng)時的業(yè)務(wù)請求量不算大,再加上公司沒有對 Istio 這類服務(wù)網(wǎng)格比較熟悉的大牛,所以我們也就一直拖著沒有解決,依然只是使用了 kubernetes 的 service 進(jìn)行負(fù)載,好在也沒有出什么問題。5Fj28資訊網(wǎng)——每日最新資訊28at.com

由于現(xiàn)在換了公司后也需要維護(hù)公司的服務(wù)網(wǎng)格服務(wù),結(jié)合公司內(nèi)部對 Istio 的使用現(xiàn)在終于不再停留在理論階段了。5Fj28資訊網(wǎng)——每日最新資訊28at.com

所以也終有機(jī)會將這個坑填了。5Fj28資訊網(wǎng)——每日最新資訊28at.com

gRPC 負(fù)載均衡

負(fù)載不均衡

原理

先來回顧下背景,為什么會有 gRPC 負(fù)債不均衡的問題。由于 gRPC 是基于 HTTP/2 協(xié)議的,所以客戶端和服務(wù)端會保持長鏈接,一旦鏈接建立成功后就會一直使用這個連接處理后續(xù)的請求。5Fj28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片5Fj28資訊網(wǎng)——每日最新資訊28at.com

除非我們每次請求之后都新建一個連接,這顯然是不合理的。5Fj28資訊網(wǎng)——每日最新資訊28at.com

所以要解決 gRPC 的負(fù)載均衡通常有兩種方案:5Fj28資訊網(wǎng)——每日最新資訊28at.com

  • 服務(wù)端負(fù)載均衡
  • 客戶端負(fù)載均衡 在 gRPC 這個場景服務(wù)端負(fù)載均衡不是很合適,所有的請求都需要經(jīng)過一個負(fù)載均衡器,這樣它就成為整個系統(tǒng)的瓶頸,所以更推薦使用客戶端負(fù)載均衡。

客戶端負(fù)載均衡目前也有兩種方案,最常見也是傳統(tǒng)方案。5Fj28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片5Fj28資訊網(wǎng)——每日最新資訊28at.com

這里以 Dubbo 的調(diào)用過程為例,調(diào)用的時候需要從服務(wù)注冊中心獲取到提供者的節(jié)點(diǎn)信息,然后在客戶端本地根據(jù)一定的負(fù)載均衡算法得出一個節(jié)點(diǎn)然后發(fā)起請求。5Fj28資訊網(wǎng)——每日最新資訊28at.com

換成 gRPC 也是類似的,這里以 go-zero 負(fù)載均衡的原理為例:5Fj28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片5Fj28資訊網(wǎng)——每日最新資訊28at.com

gRPC 官方庫也提供了對應(yīng)的負(fù)載均衡接口,但我們依然需要自己維護(hù)服務(wù)列表然后在客戶端編寫負(fù)載均衡算法,這里有個官方 demo:5Fj28資訊網(wǎng)——每日最新資訊28at.com

https://github.com/grpc/grpc-go/blob/87eb5b7502493f758e76c4d09430c0049a81a557/examples/features/load_balancing/client/main.go5Fj28資訊網(wǎng)——每日最新資訊28at.com

但切換到 kubernetes 環(huán)境中時再使用以上的方式就不夠優(yōu)雅了,因?yàn)槲覀兪褂?kubernetes 的目的就是不想再額外的維護(hù)這個客戶端包,這部分能力最好是由 kubernetes 自己就能提供。5Fj28資訊網(wǎng)——每日最新資訊28at.com

但遺憾的是 kubernetes 提供的 service 只是基于 L4 的負(fù)載,所以我們每次請求的時候都只能將請求發(fā)往同一個 Provider 節(jié)點(diǎn)。5Fj28資訊網(wǎng)——每日最新資訊28at.com

測試

這里我寫了一個小程序來驗(yàn)證負(fù)債不均衡的示例:5Fj28資訊網(wǎng)——每日最新資訊28at.com

// Create gRPC servergo func() {     var port = ":50051"     lis, err := net.Listen("tcp", port)     if err != nil {        log.Fatalf("failed to listen: %v", err)     }     s := grpc.NewServer()     pb.RegisterGreeterServer(s, &server{})     if err := s.Serve(lis); err != nil {        log.Fatalf("failed to serve: %v", err)     } else {        log.Printf("served on %s /n", port)     }  }()// server is used to implement helloworld.GreeterServer.  type server struct {     pb.UnimplementedGreeterServer  }    // SayHello implements helloworld.GreeterServer  func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {     log.Printf("Received: %v", in.GetName())     name, _ := os.Hostname()     // Return hostname of Server   return &pb.HelloReply{Message: fmt.Sprintf("hostname:%s, in:%s", name, in.Name)}, nil  }

使用同一個 gRPC 連接發(fā)起一次 gRPC 請求,服務(wù)端會返回它的 hostname5Fj28資訊網(wǎng)——每日最新資訊28at.com

var (     once sync.Once     c    pb.GreeterClient  )  http.HandleFunc("/grpc_client", func(w http.ResponseWriter, r *http.Request) {     once.Do(func() {        service := r.URL.Query().Get("name")        conn, err := grpc.Dial(fmt.Sprintf("%s:50051", service), grpc.WithInsecure(), grpc.WithBlock())        if err != nil {           log.Fatalf("did not connect: %v", err)        }        c = pb.NewGreeterClient(conn)     })       // Contact the server and print out its response.     name := "world"     ctx, cancel := context.WithTimeout(context.Background(), time.Second)     defer cancel()     g, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})     if err != nil {        log.Fatalf("could not greet: %v", err)     }     fmt.Fprint(w, fmt.Sprintf("Greeting: %s", g.GetMessage()))  })

創(chuàng)建一個 service 用于給 gRPC 提供域名:5Fj28資訊網(wǎng)——每日最新資訊28at.com

apiVersion: v1  kind: Service  metadata:    name: native-tools-2spec:    selector:      app: native-tools-2  ports:      - name: http        port: 8081        targetPort: 8081      - name: grpc        port: 50051        targetPort: 50051

同時將我們的 gRPC server 部署三個節(jié)點(diǎn),再部署了一個客戶端節(jié)點(diǎn):5Fj28資訊網(wǎng)——每日最新資訊28at.com

? k get podNAME                                READY   STATUS    RESTARTSnative-tools-2-d6c454689-52wgd      1/1     Running   0              native-tools-2-d6c454689-67rx4      1/1     Running   0              native-tools-2-d6c454689-zpwxt      1/1     Running   0              native-tools-65c5bd87fc-2fsmc       2/2     Running   0

我們進(jìn)入客戶端節(jié)點(diǎn)執(zhí)行多次 grpc 請求:5Fj28資訊網(wǎng)——每日最新資訊28at.com

k exec -it native-tools-65c5bd87fc-2fsmc bashGreeting: hostname:native-tools-2-d6c454689-zpwxt, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2Greeting: hostname:native-tools-2-d6c454689-zpwxt, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2Greeting: hostname:native-tools-2-d6c454689-zpwxt, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2Greeting: hostname:native-tools-2-d6c454689-zpwxt, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2Greeting: hostname:native-tools-2-d6c454689-zpwxt, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2Greeting: hostname:native-tools-2-d6c454689-zpwxt, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2Greeting: hostname:native-tools-2-d6c454689-zpwxt, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2Greeting: hostname:native-tools-2-d6c454689-zpwxt, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2

會發(fā)現(xiàn)每次請求的都是同一個節(jié)點(diǎn) native-tools-2-d6c454689-zpwxt,這也就證明了在 kubernetes 中直接使用 gRPC 負(fù)載是不均衡的,一旦連接建立后就只能將請求發(fā)往那個節(jié)點(diǎn)。5Fj28資訊網(wǎng)——每日最新資訊28at.com

使用 Istio

Istio 可以拿來解決這個問題,我們換到一個注入了 Istio 的 namespace 下還是同樣的 代碼,同樣的 service 資源進(jìn)行測試。5Fj28資訊網(wǎng)——每日最新資訊28at.com

關(guān)于開啟 namespace 的 Istio 注入會在后續(xù)更新,現(xiàn)在感興趣的可以查看下官方文檔:https://istio.io/latest/docs/setup/additional-setup/sidecar-injection/5Fj28資訊網(wǎng)——每日最新資訊28at.com

Greeting: hostname:native-tools-2-5fbf46cf54-5m7dl, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2Greeting: hostname:native-tools-2-5fbf46cf54-xprjz, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2Greeting: hostname:native-tools-2-5fbf46cf54-5m7dl, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2Greeting: hostname:native-tools-2-5fbf46cf54-5m7dl, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2Greeting: hostname:native-tools-2-5fbf46cf54-xprjz, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2Greeting: hostname:native-tools-2-5fbf46cf54-xprjz, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2Greeting: hostname:native-tools-2-5fbf46cf54-5m7dl, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2Greeting: hostname:native-tools-2-5fbf46cf54-5m7dl, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2Greeting: hostname:native-tools-2-5fbf46cf54-nz8h5, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2Greeting: hostname:native-tools-2-5fbf46cf54-nz8h5, in:worldistio-proxy@n:/$ curl http://127.0.0.1:8081/grpc_client?name=native-tools-2

可以發(fā)現(xiàn)同樣的請求已經(jīng)被負(fù)載到了多個 server 后端,這樣我們就可以不再單獨(dú)維護(hù)一個客戶端 SDK 的情況下實(shí)現(xiàn)了負(fù)載均衡。5Fj28資訊網(wǎng)——每日最新資訊28at.com

原理

其實(shí)本質(zhì)上 Istio 也是客戶端負(fù)載均衡的一種實(shí)現(xiàn)。5Fj28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片5Fj28資訊網(wǎng)——每日最新資訊28at.com

以 Istio 的架構(gòu)圖為例:5Fj28資訊網(wǎng)——每日最新資訊28at.com

  • 每一個 Pod 下會新增一個 Proxy 的 container,所有的流量入口和出口都會經(jīng)過它。
  • 它會從控制平面 Istiod 中拿到服務(wù)的注冊信息,也就是 kubernetes 中的 service。
  • 發(fā)生請求時由 proxy 容器中的 Envoy 進(jìn)行最終的負(fù)載請求。

可以在使用了 Istio 的 Pod 中查看到具體的容器:5Fj28資訊網(wǎng)——每日最新資訊28at.com

? k get pod native-tools-2-5fbf46cf54-5m7dl -n istio-test-2 -o json | jq '.spec.containers[].name'"istio-proxy""native-tools-2"

可以發(fā)現(xiàn)這里存在一個 istio-proxy 的容器,也就是我們常說的 sidecar,這樣我們就可以把原本的 SDK 里的功能全部交給 Istio 去處理。5Fj28資訊網(wǎng)——每日最新資訊28at.com

總結(jié)

當(dāng)然 Istio 的功能遠(yuǎn)不止于此,比如:5Fj28資訊網(wǎng)——每日最新資訊28at.com

  • 統(tǒng)一網(wǎng)關(guān),處理東西、南北向流量。
  • 灰度發(fā)布
  • 流量控制
  • 接口粒度的超時配置
  • 自動重試等

這次只是一個開胃菜,更多關(guān)于 Istio 的內(nèi)容會在后續(xù)更新,比如會從如何在 kubernetes 集群中安裝 Istio 講起,帶大家一步步使用好 Istio。5Fj28資訊網(wǎng)——每日最新資訊28at.com

本文相關(guān)源碼:https://github.com/crossoverJie/k8s-combat5Fj28資訊網(wǎng)——每日最新資訊28at.com

參考鏈接:5Fj28資訊網(wǎng)——每日最新資訊28at.com

  • https://istio.io/latest/docs/setup/getting-started/
  • https://segmentfault.com/a/1190000042295402
  • https://go-zero.dev/docs/tutorials/service/governance/lb

本文鏈接:http://www.www897cc.com/showinfo-26-13632-0.html在 Kubernetes 環(huán)境中實(shí)現(xiàn) gRPC 負(fù)載均衡

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: C++中的外部鏈接性和內(nèi)部鏈接性:探究其區(qū)別與應(yīng)用

下一篇: 一文徹底掌握MQ消息積壓全部解決方案

標(biāo)簽:
  • 熱門焦點(diǎn)
Top 主站蜘蛛池模板: 府谷县| 乾安县| 烟台市| 来宾市| 兰溪市| 淮南市| 锡林郭勒盟| 祁门县| 嘉善县| 揭西县| 鄂伦春自治旗| 竹溪县| 图木舒克市| 响水县| 天等县| 北川| 阳山县| 甘肃省| 喀喇| 仲巴县| 县级市| 台湾省| 巴里| 无为县| 靖边县| 苍南县| 政和县| 大埔区| 全椒县| 剑川县| 志丹县| 蕉岭县| 金塔县| 萝北县| 海门市| 秦安县| 江津市| 田东县| 太仆寺旗| 十堰市| 湟源县|