Google容器引擎公开服务“网站无法访问”
我正在使用Flask,Docker和Google Container Engine构build一个简单的Web应用程序。 我已经指定了以下DockerFile:
# Use an official Python runtime as a base image FROM python:2.7-slim # Set the working directory to /app WORKDIR /app # Copy the current directory contents into the container at /app ADD . /app # Install any needed packages specified in requirements.txt RUN pip install -r requirements.txt # Make port 80 available to the world outside this container EXPOSE 8080 # Define environment variable ENV NAME World # Run app.py when the container launches CMD ["python", "app.py"]
注意我公开端口8080。
这是我简单的Flask应用程序:
from flask import Flask, jsonify from flask import make_response app = Flask(__name__) tasks = [ { 'type': 'order', 'contents':[1,2,3,4,5,6,7,8,9,10] } ] @app.route('/', methods=['GET']) def get_tasks(): return jsonify({'tasks': tasks}) @app.errorhandler(404) def not_found(error): return make_response(jsonify({'error': 'Not found'}), 404) if __name__ == '__main__': app.run(host='0.0.0.0', port=8080)
注意host='0.0.0.0'
和port=8080
。
我在本地运行Docker容器,成功:
docker run --rm -p 8080:8080 gcr.io/${PROJECT_ID}/hello-node:v1
但是,当我使用Google Container Engine部署应用程序时,我无法通过kubectl get service
提供的外部端口访问应用程序。
我运行以下来部署Pod
:
kubectl run hello-world --image=gcr.io/${PROJECT_ID}/hello-node:v1 --port 8080
我运行以下命令来创build一个Service
来从互联网上访问:
kubectl expose deployment hello-world --type=LoadBalancer --port 8080
为什么我无法访问该服务? 似乎我已经在每一步中打开了端口8080 1)Flask应用程序2)Dockerfile 3) Pod
部署4) Service
创build。
我想你应该指出目标端口以及暴露你的部署时,像这样:
kubectl expose deployment hello-world --type=LoadBalancer --port=8080 --target-port=8080
希望能帮助到你