【问题标题】:Kubernetes - Service always hitting the same Pod ContainerKubernetes - 服务总是命中同一个 Pod 容器
【发布时间】:2021-08-10 23:38:04
【问题描述】:

我有一个基于 Docker Desktop 的本地 Kubernetes 安装。 我在 3 个 Pod 上设置了集群 IP 的 Kubernetes 服务。 我注意到在查看 Container 日志时,总是会命中同一个 Pod。

这是集群 IP 的默认行为吗? 如果是这样,其他 Pod 将如何使用,或者它们使用集群 IP 的意义何在?

另一种选择是使用 LoadBalancer 类型,但我希望服务只能从集群内访问。

有没有办法让 LoadBalancer 内部化?

如果有人可以请告知,将不胜感激。

更新:

我尝试过使用 LoadBalancer 类型,并且一直在命中同一个 Pod。

这是我的配置:

apiVersion: v1
kind: Namespace
metadata:
  name: dropshippingplatform
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: organisationservice-deployment
  namespace: dropshippingplatform
spec:
  selector:
    matchLabels:
      app: organisationservice-pod
  replicas: 3
  template:
    metadata:
      labels:
        app: organisationservice-pod
    spec:
      containers:
      - name: organisationservice-container
        image: organisationservice:v1.0.1
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: organisationservice-service
  namespace: dropshippingplatform
spec:
  selector:
    app: organisationservice-pod
  ports:
    - protocol: TCP
      port: 81
      targetPort: 80
  type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: apigateway-deployment
  namespace: dropshippingplatform
spec:
  selector:
    matchLabels:
      app: apigateway-pod
  template:
    metadata:
      labels:
        app: apigateway-pod
    spec:
      containers:
      - name: apigateway-container
        image: apigateway:v1.0.1
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: apigateway-service
  namespace: dropshippingplatform
spec:
  selector:
    app: apigateway-pod
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

这是我的 Ocelot 配置:

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/organisations",
      "DownstreamScheme": "http",
      "ServiceName": "organisationservice-service",
      "ServiceNamespace": "dropshippingplatform",
      "UpstreamPathTemplate": "/APIGateway/Organisations",
      "UpstreamHttpMethod": [ "Get" ],
      "Key": "Login"
    },
    {
      "DownstreamPathTemplate": "/weatherforecast",
      "DownstreamScheme": "http",
      "ServiceName": "organisationservice-service",
      "ServiceNamespace": "dropshippingplatform",
      "UpstreamPathTemplate": "/APIGateway/WeatherForecast",
      "UpstreamHttpMethod": [ "Get" ],
      "Key": "WeatherForecast"
    }
  ],
  "Aggregates": [
    {
      "RouteKeys": [
        "Login",
        "WeatherForecast"
      ],
      "UpstreamPathTemplate": "/APIGateway/Organisations/Login"
    },
    {
      "RouteKeys": [
        "Login",
        "WeatherForecast"
      ],
      "UpstreamPathTemplate": "/APIGateway/Organisations/TestAggregator",
      "Aggregator": "TestAggregator"
    }
  ],
  "GlobalConfiguration": {
    "ServiceDiscoveryProvider": {
      "Namespace": "default",
      "Type": "KubernetesServiceDiscoveryProvider"
    }
  }
}

为了隔离问题,我在有问题的 Kubernetes 服务前创建了一个负载均衡器,并直接从客户端调用该服务。同一个 Pod 一直在被击中,这告诉我它与 Kubernetes 而不是 Ocelot API 网关有关。

这里是 kubectl describe svc 的输出:

Name:              organisationservice-service
Namespace:         dropshippingplatform
Labels:            <none>
Annotations:       <none>
Selector:          app=organisationservice-pod
Type:              ClusterIP
IP:                X.X.X.119
Port:              <unset>  81/TCP
TargetPort:        80/TCP
Endpoints:         X.X.X.163:80,X.X.X.165:80,X.X.X.166:80
Session Affinity:  None
Events:            <none>

【问题讨论】:

  • 有很多不同的客户吗?是否涉及任何保持连接?什么样的网关?
  • 你是如何连接到服务的?客户端在哪里运行?客户是什么?
  • 只有一个客户端也在本地机器上运行。但是,我遇到问题的服务不是由客户端调用的,而是在配置中详细说明的 API 网关服务。此 API 网关正在运行 Ocelot。我也会添加 Ocelot 配置。
  • 你好@SachK,你能添加更多关于你的服务的细节吗?命令的结果是什么:kubectl describe svc &lt;my-service&gt; | grep -i endkubectl get ep &lt;my-service&gt;。您应该检查端点。请将此信息添加到问题中。你也可以阅读更多here
  • @Mikolaj Glodziak - 添加了输出

标签: kubernetes kubernetes-service


【解决方案1】:

我解决了。事实证明,Ocelot API Gateway 是问题所在。我将此添加到 Ocelot 配置中:

"LoadBalancerOptions": {
        "Type": "RoundRobin"
      },

现在它平均分配流量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 2020-09-14
    • 2017-05-15
    • 1970-01-01
    相关资源
    最近更新 更多