前言

前段时间写过一篇 gRPC 的入门文章,在最后还留了一个坑没有填:



也就是 gRPC 的负载均衡问题,因为当时的业务请求量不算大,再加上公司没有对 Istio 这类服务网格比较熟悉的大牛,所以我们也就一直拖着没有解决,依然只是使用了 kubernetes 的 service 进行负载,好在也没有出什么问题。

由于现在换了公司后也需要维护公司的服务网格服务,结合公司内部对 Istio 的使用现在终于不再停留在理论阶段了。

所以也终有机会将这个坑填了。

gRPC 负载均衡

负载不均衡

原理

先来回顾下背景,为什么会有 gRPC 负债不均衡的问题。

由于 gRPC 是基于 HTTP/2 协议的,所以客户端和服务端会保持长链接,一旦链接建立成功后就会一直使用这个连接处理后续的请求。

除非我们每次请求之后都新建一个连接,这显然是不合理的。

所以要解决 gRPC 的负载均衡通常有两种方案:

  • 服务端负载均衡
  • 客户端负载均衡

    gRPC 这个场景服务端负载均衡不是很合适,所有的请求都需要经过一个负载均衡器,这样它就成为整个系统的瓶颈,所以更推荐使用客户端负载均衡。

客户端负载均衡目前也有两种方案,最常见也是传统方案。



这里以 Dubbo 的调用过程为例,调用的时候需要从服务注册中心获取到提供者的节点信息,然后在客户端本地根据一定的负载均衡算法得出一个节点然后发起请求。

换成 gRPC 也是类似的,这里以 go-zero 负载均衡的原理为例:

gRPC 官方库也提供了对应的负载均衡接口,但我们依然需要自己维护服务列表然后在客户端编写负载均衡算法,这里有个官方 demo:

https://github.com/grpc/grpc-go/blob/87eb5b7502493f758e76c4d09430c0049a81a557/examples/features/load_balancing/client/main.go

但切换到 kubernetes 环境中时再使用以上的方式就不够优雅了,因为我们使用 kubernetes 的目的就是不想再额外的维护这个客户端包,这部分能力最好是由 kubernetes 自己就能提供。

但遗憾的是 kubernetes 提供的 service 只是基于 L4 的负载,所以我们每次请求的时候都只能将请求发往同一个 Provider 节点。

测试

这里我写了一个小程序来验证负债不均衡的示例:

// Create gRPC server
go 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 连接发起一次 gRPC 请求,服务端会返回它的 hostname

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()))
})

创建一个 service 用于给 gRPC 提供域名:

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

同时将我们的 gRPC server 部署三个节点,再部署了一个客户端节点:

❯ k get pod
NAME READY STATUS RESTARTS
native-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

我们进入客户端节点执行多次 grpc 请求:

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

会发现每次请求的都是同一个节点 native-tools-2-d6c454689-zpwxt,这也就证明了在 kubernetes 中直接使用 gRPC 负载是不均衡的,一旦连接建立后就只能将请求发往那个节点。

使用 Istio

Istio 可以拿来解决这个问题,我们换到一个注入了 Istio 的 namespace 下还是同样的 代码,同样的 service 资源进行测试。

关于开启 namespace 的 Istio 注入会在后续更新,现在感兴趣的可以查看下官方文档:

https://istio.io/latest/docs/setup/additional-setup/sidecar-injection/

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

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

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

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

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

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

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

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

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

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

可以发现同样的请求已经被负载到了多个 server 后端,这样我们就可以不再单独维护一个客户端 SDK 的情况下实现了负载均衡。

原理

其实本质上 Istio 也是客户端负载均衡的一种实现。



以 Istio 的架构图为例:

  • 每一个 Pod 下会新增一个 Proxycontainer,所有的流量入口和出口都会经过它。
  • 它会从控制平面 Istiod 中拿到服务的注册信息,也就是 kubernetes 中的 service。
  • 发生请求时由 proxy 容器中的 Envoy 进行最终的负载请求。

可以在使用了 Istio 的 Pod 中查看到具体的容器:

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

可以发现这里存在一个 istio-proxy 的容器,也就是我们常说的 sidecar,这样我们就可以把原本的 SDK 里的功能全部交给 Istio 去处理。

总结

当然 Istio 的功能远不止于此,比如:

  • 统一网关,处理东西、南北向流量。
  • 灰度发布
  • 流量控制
  • 接口粒度的超时配置
  • 自动重试等

这次只是一个开胃菜,更多关于 Istio 的内容会在后续更新,比如会从如何在 kubernetes 集群中安装 Istio 讲起,带大家一步步使用好 Istio

本文相关源码:

https://github.com/crossoverJie/k8s-combat

参考链接:

在 kubernetes 环境中实现 gRPC 负载均衡的更多相关文章

  1. Kubernetes 中的 gRPC 负载均衡

    安装环境依赖 docker-desktop >= 4.1.1 kubernetes >= 1.21.5 go >= 1.17 protobuf >= 3.17.3 istioc ...

  2. 【译】gRPC负载均衡

    原文地址:https://github.com/grpc/grpc/blob/master/doc/load-balancing.md gRPC负载均衡 范围 本文档解释了gPRC的负载均衡的设计. ...

  3. gRPC负载均衡(客户端负载均衡)

    前言 上篇介绍了如何使用etcd实现服务发现,本篇将基于etcd的服务发现前提下,介绍如何实现gRPC客户端负载均衡. gRPC负载均衡 gRPC官方文档提供了关于gRPC负载均衡方案Load Bal ...

  4. gRPC负载均衡(自定义负载均衡策略)

    前言 上篇文章介绍了如何实现gRPC负载均衡,但目前官方只提供了pick_first和round_robin两种负载均衡策略,轮询法round_robin不能满足因服务器配置不同而承担不同负载量,这篇 ...

  5. 一步一步在Windows中使用MyCat负载均衡 下篇

    之前在 一步一步在Windows中使用MyCat负载均衡 上篇 中已经讲了如何配置出MyCat.下面讲其相关的使用. 五.配置MyCat-eye 对于MyCat监控官网还提供一个MyCat-eye w ...

  6. Linux中keepalived+LVS负载均衡的搭建测试

    1.1 LVS简介       LVS(Linux Virtual Server),也就是Linux虚拟服务器, 是一个自由软件项目.使用LVS技术要达到的目标是:通过LVS提供的负载均衡技术和Lin ...

  7. 一步一步在Windows中使用MyCat负载均衡

    一步一步在Windows中使用MyCat负载均衡 http://www.cnblogs.com/zhangs1986/p/6408981.html   mycat+sqlServer简单demo配置 ...

  8. LVS集群中的IP负载均衡技术

    LVS集群中的IP负载均衡技术 章文嵩 (wensong@linux-vs.org) 转自LVS官方参考资料 2002 年 4 月 本文在分析服务器集群实现虚拟网络服务的相关技术上,详细描述了LVS集 ...

  9. 一步一步在Windows中使用MyCat负载均衡 上篇

    传统关系型数据库的分布式开发通常需要自己做,不仅耗时耗力而且效果不是很理想,当想快速搭建时,最初想到的是看有没有第三方,网上牛人还是很多的,做得比较好的其中之一Mycat,它是开源的分布式数据库系统, ...

  10. Linux环境下Nginx及负载均衡

    Nginx 简介 Nginx 是一个高性能的 HTTP 和反向代理 Web 服务器,同时也提供了 IMAP/POP3/SMTP 服务.前向代理作为客户端的代理,服务端只知道代理的 IP 地址而不知道客 ...

随机推荐

  1. Set 接口及其常用方法

    Set 接口基本介绍 Set接口是Collection接口的一个子接口,其主要特点如下: 不允许重复元素:Set接口的实现类不会包含重复的元素.更正式地说,不包含任何一对使得e1.equals(e2) ...

  2. python笔记:第六章函数&方法

    1.系统函数 由系统提供,直接拿来用或是导入模块后使用 a = 1.12386 result = round(a,2) print(result) > 1.12 2.自定义函数 函数是结构化编程 ...

  3. 2023-07-11:给定正整数 n, 返回在 [1, n] 范围内具有 至少 1 位 重复数字的正整数的个数。 输入:n = 100。 输出:10。

    2023-07-11:给定正整数 n, 返回在 [1, n] 范围内具有 至少 1 位 重复数字的正整数的个数. 输入:n = 100. 输出:10. 答案2023-07-11: 函数的主要思路如下: ...

  4. 查询mysql数据库目前有哪些链接 具体ip及数量

    SELECT substring_index(host, ':',1) AS host_name,state,count(*) FROM information_schema.processlist ...

  5. P5752 [NOI1999] 棋盘分割题解

    本文来自我的洛谷博客. 这个题解思路虽然与其他人的思路相同, 但力求使用清晰易懂的图片和文字,讲解最简洁的道理. 请大家耐心地看完,注意要结合图片一起哦~~ 2022-8-24 更改了格式与错别字. ...

  6. 【Java】工具类 -- 持续更新

    Java原生工具类 Objects requireNotNull():为空抛异常,不为空返回本身 deepEquals():对象深度相等(数组层面)判断 调用Arrays.deepEquals0() ...

  7. 2023ccpc大学生程序设计竞赛-zx

    这次ccpc整体来说做题做的比较卡,第一个签到都wa了,后面几道中档题全都是至少wa一次才能过,这导致我们不仅罚时增加也导致需要大量时间修改代码,还有一个G题很可惜,当时只注意到B过题多所以有点被带歪 ...

  8. python笔记:第十一章正则表达式

    1.模块re 以一定规则,快速检索文本,或是实现一些替换操作 默认下,区分大小写 2.常见的匹配字符表 字符 描述 \d 代表任意数字,就是阿拉伯数字 0-9 这些 \D 代表非数字的字符.与\d完全 ...

  9. KVM 虚拟机 热插拔硬盘

    新建硬盘 lvm 命令 lvcreate -L 200G -n lv02 ssd01 qemu-img 命令 qemu-img create -f raw test1G.raw 1G dd 命令 dd ...

  10. SqlSugar本地缓存查询实现方式

    有C#的国产ORM SqlSugar 好久了,实在话还不错,不过毕竟是早期产物不能过分要求规范化,有些项目查询语句需要用到缓存,官方是redis,我写了个本地缓存借助ConcurrentBag,因为有 ...