HTTP/2
You might have services in your Kubernetes cluster that use HTTP/2 for communication. Typically these are gRPC services, but it could apply to any service that uses HTTP/2 in its transport layer. To enable HTTP/2 communication, the spec.useHttp2 value for the Upstream must be set to true. This setting instructs Gloo Gateway to use HTTP/2 for communication with the destination.
Explore your options for how to add the spec.useHttp2 value to your Upstream resource.
Before you begin
Get the external address of the gateway and save it in an environment variable.
Enable on the Upstream
You can add the spec.useHttp2 setting to an Upstream directly.
Create an Upstream for the httpbin app and add the
spec.useHttp2setting to configure the Upstream for HTTP/2.kubectl apply -f- <<EOF apiVersion: gloo.solo.io/v1 kind: Upstream metadata: name: httpbin-httpbin-8000 namespace: gloo-system spec: kube: selector: app: httpbin serviceName: httpbin serviceNamespace: httpbin servicePort: 8000 useHttp2: true EOFIf you enabled service discovery in Gloo Gateway, an Upstream is already created for you. In this case, runkubectl edit upstream httpbin-httpbin-8000 -n gloo-systemto edit the Upstream that was created for you and manually add theuseHttp2setting.Optional: Remove the
useHttp2setting from your Upstream by editing the Upstream and removing the setting.kubectl edit upstream httpbin-httpbin-8000 -n gloo-system
Enable with service annotations
One of the ways to enable HTTP/2, is to add the gloo.solo.io/h2_service annotation to the Kubernetes service and set it to true. If the annotation is present, Gloo Gateway automatically updates the Upstream and adds the useHttp2 value.
To use service annotations to enable HTTP/2 for a destination, you must enable service discovery in Gloo Gateway.
Review the httpbin Upstream resource that was automatically created for the httpbin app that you deployed as part of the Get started guide. Note that the
spec.useHttp2option is not set.kubectl get upstream httpbin-httpbin-8000 -n gloo-system -o yamlAdd the
gloo.solo.io/h2_service=trueannotation to the httpbin service.kubectl annotate service httpbin gloo.solo.io/h2_service=true -n httpbinReview the httpbin Upstream again and verify that you now see the
useHttp2: truesetting on your Upstream.kubectl get upstream httpbin-httpbin-8000 -n gloo-system -o yamlExample output:
apiVersion: gloo.solo.io/v1 kind: Upstream metadata: annotations: gloo.solo.io/h2_service: "true" kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"httpbin","service":"httpbin"},"name":"httpbin","namespace":"httpbin"},"spec":{"ports":[{"name":"http","port":8000,"targetPort":8080},{"name":"tcp","port":9000}],"selector":{"app":"httpbin"}}} creationTimestamp: "2024-04-26T20:34:49Z" generation: 4 labels: discovered_by: kubernetesplugin name: httpbin-httpbin-8000 namespace: gloo-system resourceVersion: "4421071" uid: a73b1b82-2910-497a-9f82-ff513ebe911d spec: discoveryMetadata: labels: app: httpbin service: httpbin kube: selector: app: httpbin serviceName: httpbin serviceNamespace: httpbin servicePort: 8000 useHttp2: trueUpdate the annotation on the httpbin service and set it to
false.kubectl annotate service httpbin gloo.solo.io/h2_service=false -n httpbin --overwriteReview the Upstream resource again and verify that the
useHttp2setting was updated tofalse.kubectl get upstream httpbin-httpbin-8000 -n gloo-system -o yamlExample output:
... spec: discoveryMetadata: labels: app: httpbin service: httpbin kube: selector: app: httpbin serviceName: httpbin serviceNamespace: httpbin servicePort: 8000 useHttp2: false
Once set, you cannot remove the useHttp2 setting from an Upstream entirely by using service annotations. For example, a command, such as kubectl annotate service httpbin gloo.solo.io/h2_service- -n httpbin does not remove this setting from the Upstream. You can only update the useHttp2 field and set it to true or false. To remove this setting from an Upstream entirely, you must edit the Upstream and remove that setting.
In a race condition where both the annotation and port name are set, the annotation value takes precedence as it is evaluated first. If the annotation is set to false and the port name is set to http2, the spec.useHttp2 setting on the Upstream evaluates to false. To avoid conflicting HTTP/2 settings, it is recommended to use either service annotations or port names.
Enable with port names
You can enable HTTP/2 by setting specific port names on the service that exposes the Upstream. The name of the port must be one of the following: grpc, http2, or h2.
To use port names to enable HTTP/2 for a destination, you must enable service discovery in Gloo Gateway.
Review the httpbin Upstream resource that was automatically created for the httpbin app that you deployed as part of the Get started guide. Note that the
spec.useHttp2option is not set.kubectl get upstream httpbin-httpbin-8000 -n gloo-system -o yamlReview the ports that are defined on the httpbin service.
kubectl get service httpbin -n httpbin -o yamlExample output:
... spec: clusterIP: 172.20.148.195 clusterIPs: - 172.20.148.195 internalTrafficPolicy: Cluster ipFamilies: - IPv4 ipFamilyPolicy: SingleStack ports: - name: http port: 8000 protocol: TCP targetPort: 8080 - name: tcp port: 9000 protocol: TCP targetPort: 9000Edit the service and change the
httpport tohttp2.kubectl edit service httpbin -n httpbinExample output:
... spec: clusterIP: 172.20.148.195 clusterIPs: - 172.20.148.195 internalTrafficPolicy: Cluster ipFamilies: - IPv4 ipFamilyPolicy: SingleStack ports: - name: http2 port: 8000 protocol: TCP targetPort: 8080 - name: tcp port: 9000 protocol: TCP targetPort: 9000Review the Upstream for the httpbin app and verify that the
useHttp2setting was added.kubectl get upstream httpbin-httpbin-8000 -n gloo-system -o yamlExample output:
... spec: discoveryMetadata: labels: app: httpbin service: httpbin kube: selector: app: httpbin serviceName: httpbin serviceNamespace: httpbin servicePort: 8000 useHttp2: true
Once set, you cannot remove the useHttp2 setting from an Upstream entirely by using port names. For example, if you change back the port name from http2 to http, the useHttp2 setting is not removed from the Upstream. To remove this setting from an Upstream entirely, you must edit the Upstream and remove that setting.
In a race condition where both the annotation and port name are set, the annotation value takes precedence as it is evaluated first. If the annotation is set to false and the port name is set to http2, the spec.useHttp2 setting on the Upstream evaluates to false. To avoid conflicting HTTP/2 settings, it is recommended to use either service annotations or port names.