如何通过 Kubernetes 服务(如网关 API)路由到特定的 pod [英] How to Route to specific pod through Kubernetes Service (like a Gateway API)

查看:36
本文介绍了如何通过 Kubernetes 服务(如网关 API)路由到特定的 pod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 的Docker 桌面"上运行 Kubernetes.

我有一个用于部署的 LoadBalancer 服务,其中有 3 个副本.我想通过某种方式访问​​特定的 pod (例如通过 URL 路径:< serviceIP >:8090/pod1).

有没有办法实现这个用例?

<小时>

deployment.yaml :

<块引用>

apiVersion: v1种类:服务元数据:名称:我的服务1标签:应用程序:流规格:端口:- 端口:8090目标端口:8090名称:端口8090选择器:应用程序:流# clusterIP:无类型:负载均衡器---apiVersion: 应用程序/v1beta2种类:部署元数据:名称:流部署标签:应用程序:流规格:复制品:3选择器:匹配标签:应用程序:流战略:类型:重新创建模板:元数据:标签:应用程序:流规格:容器:- 图像:流服务器模拟:最新名称:流服务器模拟imagePullPolicy:从不环境:- 名称:STREAMER_IP值来自:字段引用:字段路径:status.podIP- 名称:STREAMER_ADDRESS值:流服务器模拟:8090端口:- 容器端口:8090

<小时>

我的最终目标是实现 Pod 的水平自动缩放.

到目前为止应用程序的设计/和工作方式(没有 kubernetes):

<块引用>

有3个组件:REST-Server、Stream-Server(3个实例本地在不同端口的不同 JVM 上)和 RabbitMQ.

1 - 客户端向REST-Server"发送一个流 url 请求.2 - REST-Server 放入 RabbitMQ 队列.3 - Stream-Server 之一接收它并填充其 IP 并通过 RabbitMQ 发送回 REST-Server.4 - 客户端接收 IP 并使用 IP 建立直接 WS 连接.

我面临的问题是 :

1 - 当客户端请求一个流 IP 时,其中一个 pod(假设 POD1)接收它并发送它的 URL(这是服务 URL,来自 LoadBalancer 服务).2 - 下次当客户端尝试使用服务 IP 进行连接(WebSocket 连接)时,它不会是接受请求的同一个 Pod.

它应该是接受请求的同一个pod,并且必须可以被客户端访问.

解决方案

您可以使用 StatefulSets,如果您不需要使用部署.

对于副本 3,您将有 3 个 pod 命名

  1. stream-deployment-0
  2. stream-deployment-1
  3. stream-deployment-2

您可以通过 $(podname).$(service name).$(namespace).svc.cluster.local

访问每个 Pod

详情请查看这个

您可能还想设置一个入口以从集群外部指向每个 Pod.

I am running Kubernetes on "Docker Desktop" in Windows.

I have a LoadBalancer Service for a deployment which has 3 replicas. I would like to access SPECIFIC pod through some means (such as via URL path : < serviceIP >:8090/pod1).

Is there any way to achieve this usecase?


deployment.yaml :

apiVersion: v1
kind: Service
metadata:
  name: my-service1
  labels:
    app: stream
spec:
  ports:
  - port: 8090
    targetPort: 8090
    name: port8090
  selector:
    app: stream
  # clusterIP: None
  type: LoadBalancer
---
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: stream-deployment
  labels:
    app: stream
spec:
  replicas: 3
  selector:
    matchLabels:
      app: stream
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: stream
    spec:
      containers:
      - image: stream-server-mock:latest
        name: stream-server-mock
        imagePullPolicy: Never
        env:
        - name: STREAMER_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: STREAMER_ADDRESS
          value: stream-server-mock:8090
        ports:
        - containerPort: 8090


My end goal is to achieve horizontal auto-scaling of pods.

How Application designed/and works as of now (without kubernetes) :

There are 3 components : REST-Server, Stream-Server (3 instances locally on different JVM on different ports), and RabbitMQ.

1 - The client sends a request to "REST-Server" for a stream url.
2 - The REST-Server puts in the RabbitMQ queue.
3 - One of the Stream-Server picks it up and populates its IP and sends back to REST-Server through RabbitMQ.
4 - The client receives the IP and establishes a direct WS connection using the IP.

The Problem what I face is :

1 - When the client requests for a stream IP, one of the pods (lets say POD1) picks it up and sends its URL (which is service URL, comes through LoadBalancer Service).
2 - Next time when the client tries to connect (WebSocket Connection) using the Service IP, it wont be the same pod which accepted the request.

It should be the same pod which accepted the request, and must be accessible by the client.

解决方案

You can use StatefulSets if you are not required to use deployment.

For replica 3, you will have 3 pods named

  1. stream-deployment-0
  2. stream-deployment-1
  3. stream-deployment-2

You can access each pod as $(podname).$(service name).$(namespace).svc.cluster.local

For details, check this

You may also want to set up an ingress to point each pod from outside of the cluster.

这篇关于如何通过 Kubernetes 服务(如网关 API)路由到特定的 pod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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