Cette page explique comment utiliser les objets Kubernetes Ingress et Service pour configurer un équilibreur de charge d'application externe de manière à utiliser HTTP/2 pour la communication avec les services de backend.
Présentation
Un équilibreur de charge d'application agit comme un proxy entre vos clients et votre application. Les clients peuvent utiliser HTTP/1.1 ou HTTP/2 pour communiquer avec le proxy de l'équilibreur de charge. Cependant, la connexion entre le proxy de l'équilibreur de charge et votre application utilise le protocole HTTP/1.1 par défaut. Si votre application exécutée dans un pod Google Kubernetes Engine (GKE) peut recevoir des requêtes HTTP/2, configurez l'équilibreur de charge externe pour qu'il utilise HTTP/2 lorsqu'il transmet des requêtes à votre application.
Dans cet exercice, vous allez créer un déploiement, un service et une entrée. Vous insérerez une annotationcloud.google.com/app-protocols dans le fichier manifeste du service pour indiquer que l'équilibreur de charge doit utiliser HTTP/2 pour communiquer avec votre application.
Ensuite, vous appellerez votre service et vérifierez que votre application a bien reçu une requête HTTP/2.
Avant de commencer
Avant de commencer, effectuez les tâches suivantes :
Si vous souhaitez utiliser Google Cloud CLI pour cette tâche, installez puis initialisez gcloud CLI. Si vous avez déjà installé gcloud CLI, assurez-vous de disposer de la dernière version en exécutant la commande gcloud components update.
Documentez-vous sur les ressources Entrée et Service de Kubernetes.
Ce fichier manifeste décrit un service avec les propriétés suivantes :
type: NodePort : indique qu'il s'agit d'un service de type NodePort.
app: echoheaders : indique que tout pod portant cette étiquette est membre du service.
cloud.google.com/app-protocols : indique que my-port doit utiliser le protocole HTTP/2.
port: 443, protocol: TCP et targetPort: 8433 : indique que le trafic envoyé au service sur le port TCP 443 doit être acheminé vers le port TCP 8443 sur l'un des pods membres.
Ce fichier manifeste décrit un Ingress qui spécifie que les requêtes entrantes sont envoyées à un pod membre du service echoheaders. Les requêtes sont acheminées vers le pod sur le port targetPort spécifié dans le fichier manifeste du service echoheaders. Dans cet exercice, le pod targetPort est 8443.
Appliquez le fichier manifeste à votre cluster :
kubectlapply-fmy-ingress.yaml
L'exécution de cette commande peut prendre plusieurs minutes lorsque le contrôleur d'entrée Kubernetes configure l'équilibreur de charge d'application.
Sous Name (Nom), localisez votre équilibreur de charge.
Cliquez sur le nom de votre équilibreur de charge pour afficher votre service de backend.
Vérifiez que le protocole du point de terminaison de votre service de backend est HTTP/2.
Appeler votre service
Attendez quelques minutes que GKE configure l'équilibreur de charge et le service de backend, puis saisissez l'adresse IP externe de votre équilibreur de charge dans la barre d'adresse de votre navigateur.
Configurez des certificats SSL pour votre équilibreur de charge avec l'objet "Entrée".
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/01 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/01 (UTC)."],[],[],null,["# Using HTTP/2 for load balancing with Ingress\n\n[Autopilot](/kubernetes-engine/docs/concepts/autopilot-overview) [Standard](/kubernetes-engine/docs/concepts/choose-cluster-mode)\n\n*** ** * ** ***\n\nThis page shows how to use Kubernetes\n[Ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/)\nand\n[Service](https://kubernetes.io/docs/concepts/services-networking/service/)\nobjects to configure an external Application Load Balancer to use\n[HTTP/2](https://http2.github.io/)\nfor communication with backend services.\n\nOverview\n--------\n\nAn Application Load Balancer acts as a proxy between your clients and your\napplication. Clients can use HTTP/1.1 or HTTP/2 to communicate with the load\nbalancer proxy. However, the connection from the load balancer proxy to your\napplication uses HTTP/1.1 by default. If your application, running in a\nGoogle Kubernetes Engine (GKE) Pod, is capable of receiving HTTP/2 requests, you\nconfigure the external load balancer to use HTTP/2 when it forwards requests to\nyour application.\n\nIn this exercise, you create a Deployment, a Service, and an Ingress. You put a\n`cloud.google.com/app-protocols` annotation in your Service manifest to specify\nthat the load balancer should use HTTP/2 to communicate with your application.\nThen you call your service and verify that your application received an HTTP/2\nrequest.\n\nBefore you begin\n----------------\n\nBefore you start, make sure that you have performed the following tasks:\n\n- Enable the Google Kubernetes Engine API.\n[Enable Google Kubernetes Engine API](https://console.cloud.google.com/flows/enableapi?apiid=container.googleapis.com)\n- If you want to use the Google Cloud CLI for this task, [install](/sdk/docs/install) and then [initialize](/sdk/docs/initializing) the gcloud CLI. If you previously installed the gcloud CLI, get the latest version by running `gcloud components update`. **Note:** For existing gcloud CLI installations, make sure to set the `compute/region` [property](/sdk/docs/properties#setting_properties). If you use primarily zonal clusters, set the `compute/zone` instead. By setting a default location, you can avoid errors in the gcloud CLI like the following: `One of [--zone, --region] must be supplied: Please specify location`. You might need to specify the location in certain commands if the location of your cluster differs from the default that you set.\n\n\u003c!-- --\u003e\n\n- Read about the Kubernetes [Ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/) and [Service](https://kubernetes.io/docs/concepts/services-networking/service/) resources.\n- Read about the [HTTP/2 limitations for an external Application Load Balancer](/load-balancing/docs/https#HTTP2-limitations).\n\nCreate the Deployment\n---------------------\n\n1. Copy the following manifest to a file named `my-deployment.yaml`:\n\n apiVersion: apps/v1\n kind: Deployment\n metadata:\n name: echoheaders\n spec:\n replicas: 2\n selector:\n matchLabels:\n app: echoheaders\n template:\n metadata:\n labels:\n app: echoheaders\n spec:\n containers:\n - name: echoheaders\n image: registry.k8s.io/echoserver:1.10\n ports:\n - containerPort: 8443\n\n This manifest describes a Deployment with two replicas of the\n `echoheaders` web application.\n2. Apply the manifest to your cluster:\n\n kubectl apply -f my-deployment.yaml\n\n| **Note:** To ensure the load balancer can make a correct HTTP2 request to your backend, your backend must be configured with SSL. For more information on what types of certificates are accepted, see [Encryption from the load balancer to the backends](/load-balancing/docs/ssl-certificates#backend-encryption).\n\nCreate the Service\n------------------\n\n1. Copy the following manifest to a file named `my-service.yaml`:\n\n apiVersion: v1\n kind: Service\n metadata:\n annotations:\n cloud.google.com/app-protocols: '{\"my-port\":\"HTTP2\"}'\n name: echoheaders\n labels:\n app: echoheaders\n spec:\n type: NodePort\n ports:\n - port: 443\n targetPort: 8443\n protocol: TCP\n name: my-port\n selector:\n app: echoheaders\n\n This manifest describes a Service with the following properties:\n - `type: NodePort`: Specifies that this is a Service of type [NodePort](https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types).\n - `app: echoheaders`: Specifies that any Pod that has this label is a member of the Service.\n - `cloud.google.com/app-protocols`: Specifies that `my-port` should use the HTTP/2 protocol.\n - `port: 443`, `protocol: TCP`, and `targetPort: 8433`: Specify that traffic directed to the Service on TCP port 443 should be routed to TCP port 8443 on one of the member Pods.\n2. Apply the manifest to your cluster:\n\n kubectl apply -f my-service.yaml\n\n3. View the Service:\n\n kubectl get service echoheaders --output yaml\n\n The output is similar to the following: \n\n apiVersion: v1\n kind: Service\n metadata:\n annotations:\n cloud.google.com/app-protocols: '{\"my-port\":\"HTTP2\"}'\n ...\n labels:\n app: echoheaders\n name: echoheaders\n ...\n spec:\n clusterIP: 10.39.251.148\n ...\n ports:\n - name: my-port\n nodePort: 30647\n port: 443\n protocol: TCP\n targetPort: 8443\n selector:\n app: echoheaders\n ...\n type: NodePort\n ...\n\nCreate the Ingress\n------------------\n\n1. Copy the following manifest to a file named `my-ingress.yaml`:\n\n apiVersion: networking.k8s.io/v1\n kind: Ingress\n metadata:\n name: echomap\n spec:\n defaultBackend:\n service:\n name: echoheaders\n port:\n number: 443\n\n This manifest describes an Ingress that specifies that incoming requests are\n sent to a Pod that is a member of the `echoheaders` Service. Requests are\n routed to the Pod on the `targetPort` that is specified in the `echoheaders`\n Service manifest. In this exercise, the Pod `targetPort` is `8443`.\n2. Apply the manifest to your cluster:\n\n kubectl apply -f my-ingress.yaml\n\n This command can take several minutes to complete while the Kubernetes\n Ingress controller configures the Application Load Balancer.\n3. View the Ingress:\n\n kubectl get ingress echomap --output yaml\n\n The output is similar to the following: \n\n kind: Ingress\n metadata:\n ...\n name: echomap\n ...\n spec:\n backend:\n serviceName: echoheaders\n servicePort: 443\n status:\n loadBalancer:\n ingress:\n - ip: 203.0.113.2\n\n In this output, the IP address of the Ingress is `203.0.113.2`.\n\nTest the load balancer\n----------------------\n\n### gcloud\n\n1. List your backend services:\n\n gcloud compute backend-services list\n\n2. Describe your backend service:\n\n gcloud beta compute backend-services describe \u003cvar translate=\"no\"\u003eBACKEND_SERVICE_NAME\u003c/var\u003e --global\n\n Replace \u003cvar translate=\"no\"\u003eBACKEND_SERVICE_NAME\u003c/var\u003e with the name of\n your backend service.\n\n The output specifies the `protocol` is `HTTP2`: \n\n backends:\n ...\n description: '{...,\"kubernetes.io/service-port\":\"443\",\"x-features\":[\"HTTP2\"]}'\n ...\n kind: compute#backendService\n loadBalancingScheme: EXTERNAL\n protocol: HTTP2\n ...\n\n### Console\n\n1. Go to the **Load balancing** page in the Google Cloud console.\n\n [Go to Load balancing](https://console.cloud.google.com/networking/loadbalancing/loadBalancers/list)\n2. Under **Name**, locate your load balancer.\n\n3. Click the name of your load balancer to view your backend service.\n\n4. Verify that the **Endpoint protocol** for your backend service is\n **HTTP/2**.\n\nCall your Service\n-----------------\n\nWait a few minutes for GKE to configure the load balancer and\nbackend service, then enter the external IP address of your load balancer in\nyour browser's address bar.\n\nThe output is similar to the following: \n\n Hostname: echoheaders-7886d5bc68-xnrwj\n ...\n Request Information:\n ...\n method=GET\n real path=/\n query=\n request_version=2\n request_scheme=https\n ...\n\n Request Headers:\n ...\n x-forwarded-for=[YOUR_IP_ADDRESS], 203.0.113.2\n x-forwarded-proto=http\n ...\n\nThis output information about the request from the load balancer to the\nPod:\n\n- `request_version=2`: Indicates that the request between the load balancer and the Pod used HTTP/2.\n- `x-forwarded-proto=http`: Indicates that the request between the browser and the load balancer used HTTP 1.1, not HTTP/2.\n\nWhat's next\n-----------\n\n- Set up an\n [external Application Load Balancer with Ingress](/kubernetes-engine/docs/tutorials/http-balancer).\n\n- Configure a\n [static IP address and domain name](/kubernetes-engine/docs/tutorials/configuring-domain-name-static-ip)\n for your application using Ingress.\n\n- Configure [SSL certificates](/kubernetes-engine/docs/how-to/ingress-multi-ssl)\n for your Ingress load balancer."]]