我一直在查看 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")
我的碼頭檔案:
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因為這就是我從私人存盤庫中提取的標簽,但它仍然無法正常作業。
uj5u.com熱心網友回復:
kubectl告訴你你的服務hello-world-service有一個ClusterIPof 10.97.58.104。
您的服務的 IP 位于在不同網路上運行的集群內部,您無法直接從主機網路對其進行尋址。
要通過服務直接訪問您的 pod,您可以kubectl port-forward svc/hello-world-service <host-port>:<service-port>在您的情況下使用服務埠所在的位置5000,并且host-port可以是您希望的任何可用埠。
如果您無法將埠 5000 分配為主機埠,則您可能有一個正在運行的應用程式實體。
所以總結一下:
kubectl port-forward svc/hello-world-service 5001:5000
curl http://localhost:5001
Hello world from python
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/457704.html
標籤:Python 烧瓶 Kubernetes kubectl 迷你库贝
