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

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

问题描述

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

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

我有一个具有3个副本的部署的LoadBalancer服务. 我想通过(例如通过URL路径:< serviceIP>:8090/pod1)来访问SPECIFIC pod.

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


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

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

共有3个组件:REST服务器,流服务器(3个实例) 本地在不同端口上的不同JVM上)和RabbitMQ.

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.

我面临的问题是:

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.

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

推荐答案

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

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

对于副本3,您将有3个名为的吊舱

For replica 3, you will have 3 pods named

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

您可以以$(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天全站免登陆