Hey, I'm trying to create a simple ping application over grpc with traefik as an proof of concept for a project. Unfortunately after starting the containers, the app doesn't work!.
Here is the source code of the server, client and proto files:
const port = ":5050"
type server struct {
pb.UnimplementedPingServer
}
func (s *server) Ping(ctx context.Context, in *pb.PingRequest) (*pb.PongResponse, error) {
log.Printf("Ping Request recieved")
return &pb.PongResponse{
Message: "Pong",
}, nil
}
func main() {
log.Printf("Starting GRPC server over port: %v \n", port)
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
log.Println("Server is listening ...")
pb.RegisterPingServer(s, &server{})
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
const address = "localhost:5050"
func main() {
log.Println("Client is starting ...")
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewPingClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
log.Println("Sending ping requests to server")
for {
r, err := c.Ping(ctx, &pb.PingRequest{})
if err != nil {
log.Fatalf("could not ping: %v", err)
}
log.Println(r.GetMessage())
time.Sleep(1000)
}
}
syntax = "proto3";
package proto;
service Ping{
rpc Ping(PingRequest) returns (PongResponse){}
}
message PingRequest{
}
message PongResponse{
string message = 1;
}
I deployed the client and server in two different docker containers, and they work fine over localhost without traefik. Now with traefik this is the configurations:
traefik.yml:
entryPoints:
#http
web:
address: :80
#https
web-secure:
address: :443
# Providers are where Traefikās dynamic configuration comes from
providers:
docker:
# Listen for docker socket
endpoint: unix:///var/run/docker.sock
# Watch for changes to running container
watch: true
# Don't expose everything!
exposedByDefault: false
api:
dashboard: true
insecure: true
and the docker-compose files:
services:
traefik:
image: traefik:v2.3.1
# traefik checks the containers on the host network( bridges or other networks are valid too)
network_mode: host
volumes:
# read only access to containers
- /var/run/docker.sock:/var/run/docker.sock:ro
# configuration file
- ./traefik:/etc/traefik
restart: unless-stopped
pingerserver:
image: traefik/pingerserver:latest
restart: unless-stopped
# This is where the magic happens!
labels:
- traefik.enable=true
- traefik.http.services.pingerserver.loadbalancer.server.scheme=h2c
- traefik.http.routers.pingerserver.rule=Host(`pingerserver.localhost`)
pingerclient:
image: traefik/pingerclient:latest
restart: unless-stopped
# This is where the magic happens!
labels:
- traefik.enable=true
- traefik.http.services.pingerserver.loadbalancer.server.scheme=h2c
- traefik.http.routers.pingerclient.rule=Host(`pingerclient.localhost`)
Although traefik finds the containers and adds them as services/routers in the dashboard. There is no connection. Unfortunately I'm new to traefik and don't know the right configurations and couldn't find it out from the documentation and I really need help on this. Thank you for your attention.