我正在管理 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-pod
labels:
app: getcookie-pod
spec:
replicas: 1
selector:
matchLabels:
app: getcookie-pod
template:
metadata:
labels:
app: getcookie-pod
spec:
containers:
- name: getcookie-pod
image: privaterepo/myproject-scrapy:latest
imagePullPolicy: Always # Ensure the latest image is always pulled
ports:
- containerPort: 14805
envFrom:
- secretRef:
name: scrapy-env
env:
- name: SCRAPYRT_PORT
value: "14805"
imagePullSecrets:
- name: docker-credentials
apiVersion: v1
kind: Service
metadata:
name: getcookie-service
spec:
selector:
app: getcookie-pod
ports:
- protocol: TCP
port: 80
targetPort: 14805
type: ClusterIP
kubectl get svc
:
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
express-backend-service ClusterIP 10.99.145.37 <none> 80/TCP 2d
getcookie-service ClusterIP 10.106.14.183 <none> 80/TCP 29m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d
豆荚
kubectl get pods
$ kubectl get po
NAME READY STATUS RESTARTS AGE
express-app-deployment-6896ff994c-gl4pd 1/1 Running 3 (4h4m ago) 2d
getcookie-pod-59b8575ffc-8dcqb 1/1 Running 0 28m
jupyter-debug-pod 1/1 Running 3 (7h33m ago) 6d9h
pod3 日志(getcookie-pod-59b8575ffc-8dcqb):
$ kubectl logs getcookie-pod-59b8575ffc-8dcqb -f
2024-01-08 06:26:07+0000 [-] Log opened.
2024-01-08 06:26:07+0000 [-] Site starting on 14805
2024-01-08 06:26:07+0000 [-] Starting factory <twisted.web.server.Site object at 0x7fc2139a44f0>
2024-01-08 06:26:07+0000 [-] Running with reactor: AsyncioSelectorReactor.
2024-01-08 06:56:24+0000 [-] "127.0.0.1" - - [08/Jan/2024:06:56:24 +0000] "GET / HTTP/1.1" 404 167 "-" "curl/7.68.0"
上面的卷曲日志仅在我对 Pod 执行
exec
操作并运行以下命令后才显示:
curl http://localhost:14805
尝试直接curl getcookie-service 不起作用:
curl http://getcookie-service
输出:
curl: (7) Failed to connect to getcookie-service port 80: Connection refused
我可以使用服务名称 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
k get ep
NAME ENDPOINTS AGE
express-backend-service 10.244.0.230:8000 2d8h
getcookie-14805-service 10.244.0.12:14805 20h
kubernetes 100.65.21.11:443 2d23h
端点对象看起来不错。
很可能 getcookie-14805 pod 无法正常工作。您可以使用 kubectl exec 进入 pod 并在容器内运行以下测试并验证响应吗
卷曲http://localhost:14805
注意:如果服务位于同一命名空间中,则服务 dns 中不需要服务名称。 http://getcookie-14805-service 应该可以工作。