sigs.k8s.io/gateway-api@v1.0.0/site-src/api-types/grpcroute.md (about) 1 # GRPCRoute 2 3 ??? example "Experimental Channel in v0.6.0+" 4 5 The `GRPCRoute` resource is Alpha and part of the Experimental Channel in `v0.6.0+`. 6 7 !!! info "Experimental Channel" 8 9 The `GRPCRoute` resource described below is currently only included in the 10 "Experimental" channel of Gateway API. For more information on release 11 channels, refer to the [related documentation](https://gateway-api.sigs.k8s.io/concepts/versioning). 12 13 14 [GRPCRoute][grpcroute] is a Gateway API type for specifying routing behavior 15 of gRPC requests from a Gateway listener to an API object, i.e. Service. 16 17 ## Background 18 19 While it is possible to route gRPC with `HTTPRoutes` or via custom, out-of-tree 20 CRDs, in the long run, this leads to a fragmented ecosystem. 21 22 gRPC is a [popular RPC framework adopted widely across the industry](https://grpc.io/about/#whos-using-grpc-and-why). 23 The protocol is used pervasively within the Kubernetes project itself as the basis for 24 many interfaces, including: 25 26 - [the CSI](https://github.com/container-storage-interface/spec/blob/5b0d4540158a260cb3347ef1c87ede8600afb9bf/spec.md), 27 - [the CRI](https://github.com/kubernetes/cri-api/blob/49fe8b135f4556ea603b1b49470f8365b62f808e/README.md), 28 - [the device plugin framework](https://kubernetes.io/docs/concepts/extend-kubernetes/compute-storage-net/device-plugins/) 29 30 Given gRPC's importance in the application-layer networking space and to 31 the Kubernetes project in particular, the determination was made not to allow 32 the ecosystem to fragment unnecessarily. 33 34 ### Encapsulated Network Protocols 35 36 In general, when it is possible to route an encapsulated protocol at a lower 37 level, it is acceptable to introduce a route resource at the higher layer when 38 the following criteria are met: 39 40 - Users of the encapsulated protocol would miss out on significant conventional features from their ecosystem if forced to route at a lower layer. 41 - Users of the enapsulated protocol would experience a degraded user experience if forced to route at a lower layer. 42 - The encapsulated protocol has a significant user base, particularly in the Kubernetes community. 43 44 gRPC meets all of these criteria, so the decision was made to include `GRPCRoute`in the Gateway API. 45 46 ### Cross Serving 47 48 Implementations that support GRPCRoute must enforce uniqueness of 49 hostnames between `GRPCRoute`s and `HTTPRoute`s. If a route (A) of type `HTTPRoute` or 50 `GRPCRoute` is attached to a Listener and that listener already has another Route (B) of 51 the other type attached and the intersection of the hostnames of A and B is 52 non-empty, then the implementation must reject Route A. That is, the 53 implementation must raise an 'Accepted' condition with a status of 'False' in 54 the corresponding RouteParentStatus. 55 56 In general, it is recommended that separate hostnames be used for gRPC and 57 non-gRPC HTTP traffic. This aligns with standard practice in the gRPC community. 58 If however, it is a necessity to serve HTTP and gRPC on the same hostname with 59 the only differentiator being URI, the user should use `HTTPRoute` resources for 60 both gRPC and HTTP. This will come at the cost of the improved UX of the 61 `GRPCRoute` resource. 62 63 ## Spec 64 65 The specification of a GRPCRoute consists of: 66 67 - [ParentRefs][parentRef]- Define which Gateways this Route wants to be attached 68 to. 69 - [Hostnames][hostname] (optional)- Define a list of hostnames to use for 70 matching the Host header of gRPC requests. 71 - [Rules][grpcrouterule]- Define a list of rules to perform actions against 72 matching gRPC requests. Each rule consists of [matches][matches], 73 [filters][filters] (optional), and [backendRefs][backendRef] (optional) 74 fields. 75 76 <!--- Editable SVG available at site-src/images/grpcroute-basic-example.svg --> 77 The following illustrates a GRPCRoute that sends all traffic to one Service: 78  79 80 ### Attaching to Gateways 81 82 Each Route includes a way to reference the parent resources it wants to attach 83 to. In most cases, that's going to be Gateways, but there is some flexibility 84 here for implementations to support other types of parent resources. 85 86 The following example shows how a Route would attach to the `acme-lb` Gateway: 87 88 ```yaml 89 apiVersion: gateway.networking.k8s.io/v1alpha2 90 kind: GRPCRoute 91 metadata: 92 name: grpcroute-example 93 spec: 94 parentRefs: 95 - name: acme-lb 96 ``` 97 98 Note that the target Gateway needs to allow GRPCRoutes from the route's 99 namespace to be attached for the attachment to be successful. 100 101 ### Hostnames 102 103 Hostnames define a list of hostnames to match against the Host header of the 104 gRPC request. When a match occurs, the GRPCRoute is selected to perform request 105 routing based on rules and filters (optional). A hostname is the fully qualified 106 domain name of a network host, as defined by [RFC 3986][rfc-3986]. Note the 107 following deviations from the “host” part of the URI as defined in the RFC: 108 109 - IPs are not allowed. 110 - The : delimiter is not respected because ports are not allowed. 111 112 Incoming requests are matched against hostnames before the GRPCRoute rules are 113 evaluated. If no hostname is specified, traffic is routed based on GRPCRoute 114 rules and filters (optional). 115 116 The following example defines hostname "my.example.com": 117 ```yaml 118 apiVersion: gateway.networking.k8s.io/v1alpha2 119 kind: GRPCRoute 120 metadata: 121 name: grpcroute-example 122 spec: 123 hostnames: 124 - my.example.com 125 ``` 126 127 ### Rules 128 129 Rules define semantics for matching an gRPC requests based on conditions, 130 optionally executing additional processing steps, and optionally forwarding 131 the request to an API object. 132 133 #### Matches 134 135 Matches define conditions used for matching an gRPC requests. Each match is 136 independent, i.e. this rule will be matched if any single match is satisfied. 137 138 Take the following matches configuration as an example: 139 ```yaml 140 apiVersion: gateway.networking.k8s.io/v1alpha2 141 kind: GRPCRoute 142 ... 143 matches: 144 - method: 145 service: com.example.User 146 method: Login 147 headers: 148 values: 149 version: "2" 150 - method: 151 service: com.example.v2.User 152 method: Login 153 ``` 154 155 For a request to match against this rule, it must satisfy EITHER of the 156 following conditions: 157 158 - The `com.example.User.Login` method **AND** contains the header "version: 2" 159 - The `com.example.v2.User.Login` method. 160 161 If no matches are specified, the default is to match every gRPC request. 162 163 #### Filters (optional) 164 165 Filters define processing steps that must be completed during the request or 166 response lifecycle. Filters act as an extension point to express additional 167 processing that may be performed in Gateway implementations. Some examples 168 include request or response modification, implementing authentication 169 strategies, rate-limiting, and traffic shaping. 170 171 The following example adds header "my-header: foo" to gRPC requests with Host 172 header "my.filter.com". Note that GRPCRoute uses HTTPRoute filters for features 173 with functionality identical to HTTPRoute, such as this. 174 175 ```yaml 176 {% include 'experimental/grpc-filter.yaml' %} 177 ``` 178 179 API conformance is defined based on the filter type. The effects of ordering 180 multiple behaviors are currently unspecified. This may change in the future 181 based on feedback during the alpha stage. 182 183 Conformance levels are defined by the filter type: 184 185 - All "core" filters MUST be supported by implementations supporting GRPCRoute. 186 - Implementers are encouraged to support "extended" filters. 187 - "Implementation-specific" filters have no API guarantees across implementations. 188 189 Specifying a core filter multiple times has unspecified or custom conformance. 190 191 If an implementation can not support a combinations of filters, they must clearly 192 document that limitation. In cases where incompatible or unsupported 193 filters are specified and cause the `Accepted` condition to be set to status 194 `False`, implementations may use the `IncompatibleFilters` reason to specify 195 this configuration error. 196 197 #### BackendRefs (optional) 198 199 BackendRefs defines the API objects to which matching requests should be sent. If 200 unspecified, the rule performs no forwarding. If unspecified and no filters 201 are specified that would result in a response being sent, an `UNIMPLEMENTED` error code 202 is returned. 203 204 205 206 The following example forwards gRPC requests for the method `User.Login` to service 207 "my-service1" on port `50051` and gRPC requests for the method `Things.DoThing` with 208 header `magic: foo` to service "my-service2" on port `50051`: 209 ```yaml 210 {% include 'experimental/basic-grpc.yaml' %} 211 ``` 212 213 The following example uses the `weight` field to forward 90% of gRPC requests to 214 `foo.example.com` to the "foo-v1" Service and the other 10% to the "foo-v2" 215 Service: 216 ```yaml 217 {% include 'experimental/traffic-splitting/grpc-traffic-split-2.yaml' %} 218 ``` 219 220 Reference the [backendRef][backendRef] API documentation for additional details 221 on `weight` and other fields. 222 223 ## Status 224 225 Status defines the observed state of the GRPCRoute. 226 227 ### RouteStatus 228 229 RouteStatus defines the observed state that is required across all route types. 230 231 #### Parents 232 233 Parents define a list of the Gateways (or other parent resources) that are 234 associated with the GRPCRoute, and the status of the GRPCRoute with respect to 235 each of these Gateways. When a GRPCRoute adds a reference to a Gateway in 236 parentRefs, the controller that manages the Gateway should add an entry to this 237 list when the controller first sees the route and should update the entry as 238 appropriate when the route is modified. 239 240 ## Examples 241 242 The following example indicates GRPCRoute "grpc-example" has been accepted by 243 Gateway "gw-example" in namespace "gw-example-ns": 244 ```yaml 245 apiVersion: gateway.networking.k8s.io/v1alpha2 246 kind: GRPCRoute 247 metadata: 248 name: grpc-example 249 ... 250 status: 251 parents: 252 - parentRefs: 253 name: gw-example 254 namespace: gw-example-ns 255 conditions: 256 - type: Accepted 257 status: "True" 258 ``` 259 260 ## Merging 261 Multiple GRPCRoutes can be attached to a single Gateway resource. Importantly, 262 only one Route rule may match each request. For more information on how conflict 263 resolution applies to merging, refer to the [API specification][grpcrouterule]. 264 265 266 [grpcroute]: /reference/spec/#gateway.networking.k8s.io/v1alpha2.GRPCPRoute 267 [grpcrouterule]: /reference/spec/#gateway.networking.k8s.io/v1alpha2.GRPCRouteRule 268 [hostname]: /reference/spec/#gateway.networking.k8s.io/v1beta1.Hostname 269 [rfc-3986]: https://tools.ietf.org/html/rfc3986 270 [matches]: /reference/spec/#gateway.networking.k8s.io/v1alpha2.GRPCRouteMatch 271 [filters]: /reference/spec/#gateway.networking.k8s.io/v1alpha2.GRPCRouteFilter 272 [backendRef]: /reference/spec/#gateway.networking.k8s.io/v1alpha2.GRPCBackendRef 273 [parentRef]: /reference/spec/#gateway.networking.k8s.io/v1beta1.ParentRef 274