Kubernetes - 服务总是命中同一个 Pod 容器
Kubernetes - Service always hitting the same Pod Container
我有一个基于 Docker 桌面的本地 Kubernetes 安装。
我有一个 Kubernetes 服务设置,集群 IP 在 3 Pods 之上。
我在查看容器日志时注意到,始终会命中同一个 Pod。
这是集群 IP 的默认行为吗?
如果是这样,将如何使用另一个 Pods 或者他们使用集群 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>
我解决了。原来 Ocelot API 网关是问题所在。我将其添加到 Ocelot 配置中:
"LoadBalancerOptions": {
"Type": "RoundRobin"
},
现在它平均分配流量。
我有一个基于 Docker 桌面的本地 Kubernetes 安装。 我有一个 Kubernetes 服务设置,集群 IP 在 3 Pods 之上。 我在查看容器日志时注意到,始终会命中同一个 Pod。
这是集群 IP 的默认行为吗? 如果是这样,将如何使用另一个 Pods 或者他们使用集群 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>
我解决了。原来 Ocelot API 网关是问题所在。我将其添加到 Ocelot 配置中:
"LoadBalancerOptions": {
"Type": "RoundRobin"
},
现在它平均分配流量。