gRPC Examples

    Static configuration:

    1. web:
    2. address: :80
    3. providers:
    4. file:
    5. directory: /path/to/dynamic/config
    6. api: {}
    1. --entryPoints.web.address=:80
    2. --providers.file.directory=/path/to/dynamic/config
    3. --api.insecure=true

    /path/to/dynamic/config/dynamic_conf.{toml,yml}:

    1. ## dynamic configuration ##
    2. [http]
    3. [http.routers]
    4. [http.routers.routerTest]
    5. service = "srv-grpc"
    6. rule = "Host(`frontend.local`)"
    7. [http.services]
    8. [http.services.srv-grpc]
    9. [http.services.srv-grpc.loadBalancer]
    10. [[http.services.srv-grpc.loadBalancer.servers]]
    11. url = "h2c://backend.local:8080"
    1. ## dynamic configuration ##
    2. http:
    3. routers:
    4. routerTest:
    5. service: srv-grpc
    6. rule: Host(`frontend.local`)
    7. services:
    8. loadBalancer:
    9. servers:
    10. - url: h2c://backend.local:8080

    Warning

    For providers with labels, you will have to specify the traefik.http.services..loadbalancer.server.scheme=h2c

    Conclusion

    We don't need specific configuration to use gRPC in Traefik, we just need to use h2c protocol, or use HTTPS communications to have HTTP2 with the backend.

    With HTTPS

    This section explains how to use Traefik as reverse proxy for gRPC application with self-signed certificates.

    In order to secure the gRPC server, we generate a self-signed certificate for service url:

    That will prompt for information, the important answer is:

    1. Common Name (e.g. server FQDN or YOUR name) []: backend.local

    gRPC Client Certificate

    Generate your self-signed certificate for router url:

    with

    1. Common Name (e.g. server FQDN or YOUR name) []: frontend.local

    At last, we configure our Traefik instance to use both self-signed certificates.

    Static configuration:

    1. [entryPoints]
    2. [entryPoints.websecure]
    3. address = ":4443"
    4. [serversTransport]
    5. # For secure connection on backend.local
    6. rootCAs = [ "./backend.cert" ]
    7. [api]
    8. [provider.file]
    9. directory = "/path/to/dynamic/config"
    1. --entryPoints.websecure.address=:4443
    2. # For secure connection on backend.local
    3. --serversTransport.rootCAs=./backend.cert
    4. --providers.file.directory=/path/to/dynamic/config
    5. --api.insecure=true
    1. ## dynamic configuration ##
    2. [http]
    3. [http.routers]
    4. [http.routers.routerTest]
    5. service = "srv-grpc"
    6. rule = "Host(`frontend.local`)"
    7. [http.services]
    8. [http.services.srv-grpc]
    9. [http.services.srv-grpc.loadBalancer]
    10. [[http.services.srv-grpc.loadBalancer.servers]]
    11. # Access on backend with HTTPS
    12. url = "https://backend.local:8080"
    13. [tls]
    14. # For secure connection on frontend.local
    15. certFile = "./frontend.cert"
    16. keyFile = "./frontend.key"
    1. http:
    2. routers:
    3. routerTest:
    4. service: srv-grpc
    5. rule: Host(`frontend.local`)
    6. services:
    7. srv-grpc:
    8. loadBalancer:
    9. servers:
    10. # Access on backend with HTTPS
    11. - url: https://backend.local:8080
    12. tls:
    13. # For secure connection on frontend.local
    14. certificates:
    15. - certfile: ./frontend.cert
    16. keyfile: ./frontend.key

    Warning

    With some services, the server URLs use the IP, so you may need to configure insecureSkipVerify instead of the rootCAs to activate HTTPS without hostname verification.

    A gRPC example in go (modify for https)

    We use the gRPC greeter example in grpc-go

    Warning

    In order to use this gRPC example, we need to modify it to use HTTPS

    So we modify the "gRPC server example" to use our own self-signed certificate:

    1. // ...
    2. // Read cert and key file
    3. backendCert, _ := ioutil.ReadFile("./backend.cert")
    4. backendKey, _ := ioutil.ReadFile("./backend.key")
    5. // Generate Certificate struct
    6. cert, err := tls.X509KeyPair(backendCert, backendKey)
    7. if err != nil {
    8. log.Fatalf("failed to parse certificate: %v", err)
    9. }
    10. // Create credentials
    11. creds := credentials.NewServerTLSFromCert(&cert)
    12. // Use Credentials in gRPC server options
    13. serverOption := grpc.Creds(creds)
    14. var s *grpc.Server = grpc.NewServer(serverOption)
    15. defer s.Stop()
    16. pb.RegisterGreeterServer(s, &server{})
    17. err := s.Serve(lis)