我一直在研究stackoverflow,不明白为什么我的kubectl无法连接到Kubernetes文档中的简单烧瓶示例。这是我的档案:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello world from python"
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0")
我的dockerfile:
FROM python:3.9
RUN mkdir myapp
RUN cd myapp
COPY hello_world.py .
COPY requirements.txt .
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python3", "hello_world.py"]
我的yaml文件:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world-deployment
labels:
app: hello
spec:
replicas: 4
selector:
matchLabels:
app: hello
template:
metadata:
labels:
app: hello
spec:
containers:
- name: hello-world-container
image: mrajancsr/playground:1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
name: hello-world-service
spec:
selector:
app: hello
ports:
- protocol: TCP
port: 5000
targetPort: 5000
现在,当我运行以下命令时:
kubectl get pods
我明白了
hello-world-deployment-67d4d6c95c-2cpvx 1/1 Running 0 12m
hello-world-deployment-67d4d6c95c-dm78v 1/1 Running 0 12m
hello-world-deployment-67d4d6c95c-f62w7 1/1 Running 0 10m
hello-world-deployment-67d4d6c95c-xlr7w 1/1 Running 0 12m
接下来:
kubectl get svc
我得到。。。
hello-world-service ClusterIP 10.97.58.104 <none> 5000/TCP 12h
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d13h
然后我输入:
kubectl exec -it hello-world-deployment-67d4d6c95c-2cpvx bash
curl localhost:5000
我明白了:
Hello world from python
这同样有效:
docker run -p 5001:5000 hello-python
我不能使用5000:5000,因为出于某种原因它已经绑定了。
但我无法通过kubectl连接它:
curl 10.97.58.104:5000
然后我读到kubectl不起作用的原因是,也许我需要推动我的docker形象,让它拉动?因此,我创建了一个docker存储库,并推送了我的图像,因此您可以将标签视为
mrajancsr/playground:1
因为这是我从私人回购中提取的,它仍然不起作用。