En esta página, se muestra cómo usar objetos Ingress y Service de Kubernetes para configurar un balanceador de cargas de aplicaciones externo a fin de usar HTTP/2 para la comunicación con los servicios de backend.
Descripción general
Un balanceador de cargas de aplicaciones actúa como un proxy entre tus clientes y tu aplicación. Los clientes pueden usar HTTP/1.1 o HTTP/2 para comunicarse con el proxy del balanceador de cargas. Sin embargo, la conexión del proxy del balanceador de cargas a tu aplicación usa HTTP/1.1 de forma predeterminada. Si tu aplicación, que se ejecuta en un Pod de Google Kubernetes Engine, puede recibir solicitudes HTTP/2, configura el balanceador de cargas externo para que use HTTP/2 cuando reenvíe solicitudes a tu aplicación.
En este ejercicio, crearás una implementación, un servicio y un Ingress. Debes agregar una anotación cloud.google.com/app-protocols en tu manifiesto de servicio a fin de especificar que el balanceador de cargas debe usar HTTP/2 para comunicarse con tu aplicación.
Luego, debes llamar a tu servicio y verificar que tu aplicación haya recibido una solicitud HTTP/2.
Antes de comenzar
Antes de comenzar, asegúrate de haber realizado las siguientes tareas:
Si deseas usar Google Cloud CLI para esta tarea, instala y, luego, inicializa gcloud CLI. Si ya instalaste gcloud CLI, ejecuta gcloud components update para obtener la versión más reciente.
Obtén información sobre los recursos de Ingress y servicio de Kubernetes.
En este manifiesto, se describe un Service con las siguientes propiedades:
type: NodePort: especifica que este es un Service de tipo NodePort.
app: echoheaders: especifica que cualquier Pod que tiene esta etiqueta es miembro del Service.
cloud.google.com/app-protocols: Especifica que my-port debe usar el protocolo HTTP/2.
port: 443, protocol: TCP y targetPort: 8433: Especifican que el tráfico dirigido al servicio en el puerto TCP 443 se debe enrutar al puerto TCP 8422 en uno de los Pods miembro.
En este manifiesto, se describe un Ingress que especifica que las solicitudes entrantes se envían a un Pod que es miembro del Service echoheaders. Las solicitudes se dirigen al Pod en el targetPort que se especifica en el manifiesto del Service echoheaders. En este ejercicio, el Pod targetPort es 8443.
Aplica el manifiesto al clúster:
kubectlapply-fmy-ingress.yaml
Este comando puede tardar varios minutos en completarse mientras el controlador de Ingress de Kubernetes configura el balanceador de cargas de la aplicación.
Haz clic en el nombre de tu balanceador de cargas para ver el servicio de backend.
Verifica que el Protocolo de extremos de tu servicio de backend sea HTTP/2.
Llama a tu Service
Espera unos minutos a que GKE configure el balanceador de cargas y el servicio de backend y, luego, ingresa la dirección IP externa de tu balanceador de cargas en la barra de direcciones de tu navegador.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],["Última actualización: 2024-11-22 (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."]]