负载分配:所有HTTP请求都被重定向到k8集群中的单个pod [英] Load distribution: All HTTP requests are getting redirected to a single pod in a k8 cluster

查看:131
本文介绍了负载分配:所有HTTP请求都被重定向到k8集群中的单个pod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个非常简单的仅具有一个REST服务的spring boot应用程序.该应用程序被转换成泊坞窗映像("springdockerimage:1"),并部署在具有3个副本的Kubernetes集群中.我的部署"的内容定义如下:

I have created a very simple spring boot application with only one REST service. This app is converted into a docker image ("springdockerimage:1") and deployed in the Kubernetes cluster with 3 replicas. Contents of my "Deployment" definition is as follows:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: springapp
  labels:
   app: distributiondemo
spec:
 selector:
   matchLabels:
     app: distributiondemo
 replicas: 3
 template:
   metadata:
     labels:
       app: distributiondemo
   spec:
     containers:
      - name: spring-container
        image: springdockerimage:1

我已经为上述部署创建了服务,如下所示:

I have created service for my above deployment as follows:

apiVersion: v1
kind: Service
metadata:
  name: springservice
  labels:
    app: distributiondemo
spec:
  selector:
    app: distributiondemo
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 8080
    name: spring-port
    nodePort: 32000
  type: NodePort

在部署完上述两个YAML(部署和服务)文件后,我注意到一切都已按预期部署,即创建了3个副本,并且我的服务也具有3个端点.下面的屏幕截图是相同的证明:

After deploying both the above YAML(deployment and service) files, I noticed that everything has been deployed as expected i.e., 3 replicas are created and my service is having 3 endpoints as well. Below screenshot is the proof of the same:

由于我正在使用minikube进行本地测试,因此我将端口转发和访问应用程序作为 kubectl port-forward deployment.apps/springapp 40002:8080 .

Since I am using minikube for my local testing, I am port forwarding and accessing the application as kubectl port-forward deployment.apps/springapp 40002:8080 .

但是我注意到的一件事是,我所有的HTTP请求都被重定向到仅一个pod.

But one thing I noticed is that all my HTTP requests are getting redirected to only one pod.

while true ; do curl http://localhost:40002/docker-java-app/test ;done

我没弄错我做错了什么.任何帮助,将不胜感激.谢谢.

I am not getting where exactly I am doing it wrong. Any help would be appreciated. Thank you.

推荐答案

负载平衡可能不适用于端口转发的端口,因为它可能会将流量直接重定向到Pod(更多信息

The loadbalancing might not work with port-forwarded ports as it might be directly redirecting traffic to pod (read more here). The K8s service is the feature will give you that loadbalancing capability.

因此您可以尝试以下任一方法代替

So you can try either of below instead

  1. 使用http://your_service_dns_name:8080/docker-java-app/test
  2. 使用http://service_cluster_ip:8080/docker-java-app/test
  3. 使用http://any_host_ip_from_k8s_cluster:32000/docker-java-app/test
  1. Use http://your_service_dns_name:8080/docker-java-app/test
  2. Use http://service_cluster_ip:8080/docker-java-app/test
  3. Use http://any_host_ip_from_k8s_cluster:32000/docker-java-app/test

选项1和2仅在您从属于K8s群集的主机访问这些URL时有效.选项3仅需要从您正在访问url的主机到目标主机和端口的连接.

Option 1 and 2 works only if you are accessing those urls from a host which is part of K8s cluster. Option 3 just needs connectivity to target host and port, from the host you are accessing url.

这篇关于负载分配:所有HTTP请求都被重定向到k8集群中的单个pod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆