我用 minikube 建立了一个本地 kubernetes 集群。在我的集群上,我只有一个部署在运行,并且附加了一项服务。我在端口 30100 上使用了 NodePort 来公开该服务,因此我可以从我的浏览器或通过 curl 访问它。
这是我用来设置集群的
python-server.yml
文件:
apiVersion: apps/v1
kind: Deployment
metadata:
name: python-server-deployment
namespace: kubernetes-hello-world
labels:
app: python-server
spec:
replicas: 1
selector:
matchLabels:
app: python-server
template:
metadata:
labels:
app: python-server
spec:
containers:
- name: python-hello-world
image: hello-world-python:latest
imagePullPolicy: Never
ports:
- containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
name: python-server-internal-service
namespace: kubernetes-hello-world
spec:
type: NodePort
selector:
app: python-server
ports:
- protocol: TCP
port: 80
targetPort: 5000
nodePort: 30100
我的
python-hello-world
图像是基于这个python文件:
from http.server import BaseHTTPRequestHandler, HTTPServer
import os
class MyServer(BaseHTTPRequestHandler):
def do_GET(self):
load = os.getloadavg()
html = """
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<meta charset="utf-8">
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
"""
self.send_response(200)
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(bytes(html, "utf-8"))
return
def run():
addr = ('', 5000)
httpd = HTTPServer(addr, MyServer)
httpd.serve_forever()
if __name__ == '__main__':
run()
当我运行集群时,我可以按预期接收带有
curl {node_ip}:30100
的 hello world html。但是当我尝试使用相同的 ip:port 通过我的浏览器访问我的服务时,我超时了。
我读到这可能是由于缺少标头引起的,但我认为我的 python 文件中包含了所有必要的标头,那么还有什么可能导致这种情况?