LoginSignup
1
0

More than 3 years have passed since last update.

gRPCの疎通確認をCircleCIで行う

Posted at

ビルドしたserverに対して、ヘルスチェックが通るか確認したい
grpc-health-probeを活用して行うと簡単にできる

ヘルスチェックを実装する

gRPC Health Checking Protocol v1を満たすようにヘルスチェックを実装する
自前で実装するより、google.golang.org/grpc/healthあたりを使うと楽だし便利

package main

import (
    "google.golang.org/grpc"
    "google.golang.org/grpc/health"
    "google.golang.org/grpc/health/grpc_health_v1"
    "log"
    "net"
)

func example() {
    lis, err := net.Listen("tcp", ":8080")
    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }
    grpcServer := grpc.NewServer()
    grpc_health_v1.RegisterHealthServer(grpcServer, health.NewServer())
    grpcServer.Serve(lis)
}

buildして、ヘルスチェックが通るか確認する

Dockerfile
FROM golang:1.13 as builder

ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GOARCH=amd64
WORKDIR /app
COPY . .
RUN go build -a -installsuffix cgo -ldflags '-w -s' -o server

FROM alpine
RUN apk add --no-cache ca-certificates
COPY --from=builder /app/server /server
EXPOSE 8080
CMD ["/server"]

ciの設定

.circleci/config.yml
version: 2.1

jobs:
  build:
    docker:
      - image: circleci/golang:1.13
    steps:
      - checkout
      - setup_remote_docker
      - run:
          name: Build image
          command: docker build -t server .
      - run:
          name: Test image
          command: |
            docker run -d -p 8080:8080 --name built-image server
            docker run --network container:built-image amothic/grpc-health-probe -addr=localhost:8080

amothic/grpc-health-probeはscratchをベースにgrpc-health-proveを置いただけのイメージです
https://github.com/amothic/docker-grpc-health-probe

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0