|
| 1 | +# GRPCRoute Support |
| 2 | + |
| 3 | +## What is `GRPCRoute`? |
| 4 | + |
| 5 | +The `GRPCRoute` is a custom resource defined in the Gateway API that specifies how gRPC traffic should be routed. |
| 6 | +It allows you to set up routing rules based on various match criteria, such as service names and methods. |
| 7 | +With `GRPCRoute`, you can ensure that your gRPC traffic is directed to the appropriate backend services in a |
| 8 | +Kubernetes environment. |
| 9 | + |
| 10 | +For a detailed reference on `GRPCRoute` from the Gateway API, please check the official |
| 11 | +[Gateway API documentation](https://gateway-api.sigs.k8s.io/references/spec/#networking.x-k8s.io/v1alpha2.GRPCRoute). |
| 12 | + |
| 13 | +## Setting up a HelloWorld gRPC Server |
| 14 | + |
| 15 | +In this section, we'll walk you through deploying a simple "HelloWorld" gRPC server and setting up the required |
| 16 | +routing rules using the Gateway API. |
| 17 | + |
| 18 | +### Deploying the Necessary Resources |
| 19 | + |
| 20 | +1. **Apply the Gateway Configuration**: |
| 21 | + |
| 22 | + This YAML file contains the definition for a gateway with an HTTPS listener. |
| 23 | + ``` |
| 24 | + kubectl apply -f examples/my-hotel-gateway-multi-listeners.yaml |
| 25 | + ``` |
| 26 | + |
| 27 | +2. **Deploy the gRPC Server**: |
| 28 | + |
| 29 | + Deploy the example gRPC server which will respond to the SayHello gRPC request. |
| 30 | + ``` |
| 31 | + kubectl apply -f examples/greeter-grpc-server.yaml |
| 32 | + ``` |
| 33 | + |
| 34 | +3. **Set Up the gRPC Route**: |
| 35 | + |
| 36 | + This YAML file contains the `GRPCRoute` resource which directs the gRPC traffic to our example server. |
| 37 | + ``` |
| 38 | + kubectl apply -f examples/greeter-grpc-route.yaml |
| 39 | + ``` |
| 40 | + |
| 41 | +4. **Verify the Deployment**: |
| 42 | + |
| 43 | + Check to make sure that our gRPC server pod is running and get its name. |
| 44 | + ``` |
| 45 | + kubectl get pods -A |
| 46 | + ``` |
| 47 | + |
| 48 | +### Testing the gRPC Server |
| 49 | + |
| 50 | +1. **Access the gRPC Server Pod**: |
| 51 | + |
| 52 | + Copy the name of the pod running the `greeter-grpc-server` and use it to access the pod's shell. |
| 53 | + ``` |
| 54 | + kubectl exec -it <name-of-grpc-server-pod> -- bash |
| 55 | + ``` |
| 56 | + |
| 57 | +2. **Prepare the Test Client**: |
| 58 | + |
| 59 | + Inside the pod shell, create a test client by pasting the provided Go code. |
| 60 | + ```bash |
| 61 | + cat << EOF > test.go |
| 62 | + package main |
| 63 | + |
| 64 | + import ( |
| 65 | + "crypto/tls" |
| 66 | + "log" |
| 67 | + "os" |
| 68 | + |
| 69 | + "golang.org/x/net/context" |
| 70 | + "google.golang.org/grpc" |
| 71 | + "google.golang.org/grpc/credentials" |
| 72 | + pb "google.golang.org/grpc/examples/helloworld/helloworld" |
| 73 | + ) |
| 74 | + |
| 75 | + func main() { |
| 76 | + if len(os.Args) < 3 { |
| 77 | + log.Fatalf("Usage: %s <address> <port>", os.Args[0]) |
| 78 | + } |
| 79 | + |
| 80 | + address := os.Args[1] + ":" + os.Args[2] |
| 81 | +
|
| 82 | + // Create a connection with insecure TLS (no server verification). |
| 83 | + creds := credentials.NewTLS(&tls.Config{ |
| 84 | + InsecureSkipVerify: true, |
| 85 | + }) |
| 86 | + conn, err := grpc.Dial(address, grpc.WithTransportCredentials(creds)) |
| 87 | + if err != nil { |
| 88 | + log.Fatalf("did not connect: %v", err) |
| 89 | + } |
| 90 | + defer conn.Close() |
| 91 | + c := pb.NewGreeterClient(conn) |
| 92 | + |
| 93 | + // Contact the server and print out its response. |
| 94 | + name := "world" |
| 95 | + if len(os.Args) > 3 { |
| 96 | + name = os.Args[3] |
| 97 | + } |
| 98 | + r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name}) |
| 99 | + if err != nil { |
| 100 | + log.Fatalf("could not greet: %v", err) |
| 101 | + } |
| 102 | + log.Printf("Greeting: %s", r.Message) |
| 103 | + } |
| 104 | + EOF |
| 105 | + ``` |
| 106 | +
|
| 107 | +3. **Run the Test Client**: |
| 108 | +
|
| 109 | + Execute the test client, making sure to replace `<SERVICE DNS>` with the VPC Lattice service DNS and `<PORT>` |
| 110 | + with the port your Lattice listener uses (in this example, we use 443). |
| 111 | + ```bash |
| 112 | + go run test.go <SERVICE DNS> <PORT> |
| 113 | + ``` |
| 114 | +
|
| 115 | +### Expected Output |
| 116 | +
|
| 117 | +If everything is set up correctly, you should see the following output: |
| 118 | +
|
| 119 | +``` |
| 120 | +Greeting: Hello world |
| 121 | +``` |
| 122 | +
|
| 123 | +This confirms that our gRPC request was successfully routed through VPC Lattice and processed by our `greeter-grpc-server`. |
| 124 | +
|
| 125 | +--- |
| 126 | +
|
| 127 | +We hope this guide helps you get started with using `GRPCRoute` in the EKS Controller project! |
0 commit comments