我正在管理 Kubernetes 集群,并希望 Pod1 对 Pod2 和 Pod3 进行 API 调用(但是 Pod1 - Pod3 失败!):
express-backend-service
在端口 80 上公开。getcookie-14805-service
在端口 80 上公开。apiVersion: apps/v1
kind: Deployment
metadata:
name: express-app-deployment
spec:
#...
containers:
- name: express-app
image: privaterepo/myproject-backend:latest
ports:
- containerPort: 8000
#...
apiVersion: v1
kind: Service
metadata:
name: express-backend-service
spec:
selector:
app: express-app
ports:
- protocol: TCP
port: 80
targetPort: 8000
type: ClusterIP
apiVersion: apps/v1
kind: Deployment
metadata:
name: getcookie-14805-deployment
spec:
#...
containers:
- name: getcookie-14805
image: privaterepo/myproject-scrapy:latest
ports:
- containerPort: 14805
#...
apiVersion: v1
kind: Service
metadata:
name: getcookie-14805-service
spec:
selector:
app: getcookie-14805
ports:
- protocol: TCP
port: 80
targetPort: 14805
type: ClusterIP
我可以使用服务名称 http://express-backend-service/api 成功从 Pod1 向 Pod2 发送请求。但是,当尝试使用类似方法连接到 Pod3 时,我收到连接错误。
这是 Pod1 中用于连接到 Pod3 的 Python 代码片段:
def getCookie(userId):
endpoint = 'http://getcookie-14805-service.default/crawl.json?spider_name=getCookie&url=http://images.google.com/'
post = {
"request": {
"url": "http://images.google.com/",
"meta": {'userId': userId},
"callback": "parse",
"dont_filter": "True"
},
"spider_name": "getCookie"
}
try:
response = requests.post(endpoint, json=post).json()
return response['items'][0]['finalItems']
except Exception as e:
print('getCookie error:', e)
return None
user = '6010dga53294c92c981ef3y576'
getCookie(user)
收到错误:
ConnectionError: HTTPConnectionPool(host='getcookie-14805-service.default', port=80):
Max retries exceeded with url: /crawl.json?spider_name=getCookie&url=http://images.google.com/
(Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fde7a6cc7f0>: Failed
to establish a new connection: [Errno 111] Connection refused'))
为什么我可以成功地从Pod1拨打Pod2,但无法从Pod1拨打Pod3?
我希望 Kubernetes 服务能够促进 Pod 间的通信。我需要额外配置吗?
您看到烟弹上的下面标签了吗
app: getcookie-14805
您可以分享“kubectl get ep”的输出吗