tjtjtjのメモ

自分のためのメモです

envoy grpc ロードバランサー

go + grpc インストール

Goをインストール

cd /usr/local/src
curl -O https://dl.google.com/go/go1.12.6.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.12.6.linux-amd64.tar.gz

PATH通す

~/.bash_profile

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:/usr/local/go/bin

grpc 持ってくる

$ go get google.golang.org/grpc

grpc helloworld

helloworld サーバ起動

$ cd $GOPATH/src/google.golang.org/grpc/examples/helloworld
$ go run greeter_server/main.go

helloworld クライアント実行

$ cd $GOPATH/src/google.golang.org/grpc/examples/helloworld
$ go run greeter_client/main.go

するとこんな感じになる。サーバ側

# go run greeter_server/main.go
2019/06/20 20:03:21 Received: world
2019/06/20 20:03:29 Received: world
2019/06/20 20:03:31 Received: world
2019/06/20 20:04:53 Received: asdf

クライアント側

# go run greeter_client/main.go
2019/06/20 20:03:21 Greeting: Hello world
# go run greeter_client/main.go
2019/06/20 20:03:29 Greeting: Hello world
# go run greeter_client/main.go
2019/06/20 20:03:31 Greeting: Hello world
# go run greeter_client/main.go asdf
2019/06/20 20:04:53 Greeting: Hello asdf

試しに curl してみた

# curl http://localhost:50051
curl: (56) Recv failure: 接続が相手からリセットされました

grpc proxy

envoy.yamlここからパクった。

static_resources:
  listeners:
  - address:
      socket_address:
        address: 0.0.0.0
        port_value: 50151
    filter_chains:
    - filters:
      - name: envoy.http_connection_manager
        config:
          codec_type: auto
          stat_prefix: ingress_http
          route_config:
            name: local_route
            virtual_hosts:
            - name: local_service
              domains:
              - "*"
              routes:
              - match:
                  prefix: "/"
                route:
                  cluster: local_service_grpc
          access_log:
          - name: envoy.file_access_log
            config:
              path: "/dev/stdout"
          http_filters:
          - name: envoy.router
            config: {}
  clusters:
  - name: local_service_grpc
    connect_timeout: 0.250s
    type: static
    lb_policy: round_robin
    http2_protocol_options: {}
    load_assignment:
      cluster_name: local_service_grpc
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: 127.0.0.1
                port_value: 50051

envoy 起動。今回ホストネットワークを使う。

docker run \
    --name envoy --rm --publish 50151:50151 \
    --net=host \
    -v /tmp/envoy:/etc/envoy \
    envoyproxy/envoy:v1.10.0

client を 50151 に向ける。

vi greeter_client/main.go

const (
    address     = "localhost:50151"
    defaultName = "world"
)

クライアント実行

# go run greeter_client/main.go
2019/06/20 20:50:48 Greeting: Hello world

# go run greeter_client/main.go qwer
2019/06/20 20:50:52 Greeting: Hello qwer

helloworld サーバ

2019/06/20 20:50:48 Received: world
2019/06/20 20:50:52 Received: qwer

envoyの標準出力

[2019-06-20 11:50:37.411][1][info][main] [source/server/server.cc:478] starting main dispatch loop
[2019-06-20T11:50:48.159Z] "POST /helloworld.Greeter/SayHello HTTP/2" 200 - 12 18 1 0 "-" "grpc-go/1.22.0-dev" "99609321-7034-4657-b3df-db9b498e2bbb" "localhost:50151" "127.0.0.1:50051"
[2019-06-20T11:50:52.968Z] "POST /helloworld.Greeter/SayHello HTTP/2" 200 - 11 17 0 0 "-" "grpc-go/1.22.0-dev" "e4646c0f-c857-47c0-9d32-10fde8cd305c" "localhost:50151" "127.0.0.1:50051"

grpc loadbalance

サーバ1コ増やす

$ cd $GOPATH/src/google.golang.org/grpc/examples/helloworld
$ cp -R greeter_server greeter_serverb
$ vi greeter_serverb/main.go

const (
        port = ":50052"
)
:
        return &pb.HelloReply{Message: "Hello b " + in.Name}, nil

$ go run greeter_serverb/main.go

増やしたサーバをクラスタに追加

  clusters:
  - name: local_service_grpc
    connect_timeout: 0.250s
    type: static
    lb_policy: round_robin
    http2_protocol_options: {}
    load_assignment:
      cluster_name: local_service_grpc
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: 127.0.0.1
                port_value: 50051
        - endpoint:
            address:
              socket_address:
                address: 127.0.0.1
                port_value: 50052

クライアント実行

# go run greeter_client/main.go
2019/06/20 21:03:13 Greeting: Hello b world
# go run greeter_client/main.go
2019/06/20 21:03:16 Greeting: Hello world
# go run greeter_client/main.go
2019/06/20 21:03:18 Greeting: Hello world
# go run greeter_client/main.go
2019/06/20 21:03:20 Greeting: Hello b world
# go run greeter_client/main.go
2019/06/20 21:03:21 Greeting: Hello world

参考

christina04.hatenablog.com

おわり

grpc - unary だとうまくいってるけど、streaming 系だとどうなるんだろう。grpc - route_guide を試す?