【发布时间】:2020-05-04 19:04:00
【问题描述】:
我读到我需要在命名空间 B 中运行另一个服务,该服务映射到命名空间 A 负载平衡器服务。 (你可以在我的 yaml b 上看到这个服务)。
我不想使用负载均衡器提供的外部 IP(如果我的场景是使用 GCP/Azure/AWS/IBM 云/OpenShift 在云上复制的)。
我使用 minikube v1.6.2 和 Kubernetes v1.17 部署了下一个场景:
命名空间a:
deployment/python-hello-world READY: 1/1
service/python-hello-world
TYPE: Loadbalancer CLUSTER-IP: 10.96.167.227 EXT-IP: <pending> PORTS: 80:31603/TCP
命名空间 b:
deployment/python-hello-world READY: 1/1
service/python-hello-world
TYPE: Loadbalancer CLUSTER-IP: 10.96.67.10 EXT-IP: <pending> PORTS: 80:31595/TCP
service/connection
TYPE: ExternalName CLUSTER-IP: <none> EXTERNAL-IP: python-hello-world-external.a.svc.cluster.local PORT: 31603/TCP
Kubernetes yaml a:
apiVersion: apps/v1
kind: Deployment
metadata:
name: python-hello-world
namespace: a
spec:
replicas: 1
selector:
matchLabels:
app: python-hello-world
template:
metadata:
labels:
app: python-hello-world
spec:
containers:
- name: server
image: python-hello-world
ports:
- containerPort: 9090
env:
- name: PORT
value: "9090"
---
apiVersion: v1
kind: Service
metadata:
name: python-hello-world-external
namespace: a
spec:
type: LoadBalancer
selector:
app: python-hello-world
ports:
- name: http
port: 80
targetPort: 9090
Kubernetes yaml b:
apiVersion: apps/v1
kind: Deployment
metadata:
name: python-hello-world
namespace: b
spec:
replicas: 1
selector:
matchLabels:
app: python-hello-world
template:
metadata:
labels:
app: python-hello-world
spec:
containers:
- name: server
image: python-hello-world
ports:
- containerPort: 9091
env:
- name: PORT
value: "9091"
---
apiVersion: v1
kind: Service
metadata:
name: python-hello-world-external
namespace: b
spec:
type: LoadBalancer
selector:
app: python-hello-world
ports:
- name: http
port: 80
targetPort: 9091
---
apiVersion: v1
kind: Service
metadata:
name: connection
namespace: b
spec:
type: ExternalName
externalName: python-hello-world-external.a.svc.cluster.local
ports:
- port: 31603
为了在我的浏览器中访问 API A,我在终端中运行:
$ minikube service python-hello-world-external -n a
|-----------|-----------------------------|-------------|-----------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|-----------------------------|-------------|-----------------------------|
| a | python-hello-world-external | http | http://192.168.39.196:31603 |
|-----------|-----------------------------|-------------|-----------------------------|
用于访问 API B:
$ minikube service python-hello-world-external -n b
|-----------|-----------------------------|-------------|-----------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|-----------------------------|-------------|-----------------------------|
| b | python-hello-world-external | http | http://192.168.39.196:31595 |
|-----------|-----------------------------|-------------|-----------------------------|
Flask API A:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "Hello World A"
if __name__ == '__main__':
app.run(debug=False, port=9090, host='0.0.0.0')
Flask API B:
import requests
from flask import Flask
app = Flask(__name__)
@app.route('/a')
def call_a():
response = requests.get("I DONT KNOW WHAT TO PUT HERE")
return response
if __name__ == '__main__':
app.run(debug=False, port=9091, host='0.0.0.0')
- 是否可以使用 minikube 在内部发送请求?
- 如果是这样,我的配置中是否缺少某些内容?
- 如果是这样,我应该在 requests.get("") 中写什么 URL?
【问题讨论】:
-
如果您不希望从 API A 到 API B 的请求不通过负载均衡器,那么您需要一个不是
type: LoadBalancer的服务(例如 clusterIP)。您可以使用<service_name>.<namespace>格式访问您的服务 -
您是否阅读过 Kubernetes 文档中的Connecting Applications with Services 和相关页面?您可以使用 Kubernetes 提供的 DNS 名称从一个 Pod 连接到一个服务(并从那里连接到另一个 Pod),它不会通过外部负载均衡器。
标签: python docker flask kubernetes minikube