Google 容器引擎公开服务 "Site Cannot Be Reached"

Google Container Engine Expose Service "Site Cannot Be Reached"

我正在寻找使用 Flask、Docker 和 Google 容器引擎构建一个简单的 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 容器引擎部署应用程序时,我无法通过 kubectl get service 提供的外部端口访问该应用程序。

我在运行下面部署了一个Pod

kubectl run hello-world --image=gcr.io/${PROJECT_ID}/hello-node:v1 --port 8080

我运行下面的命令创建一个Service从互联网访问:

kubectl expose deployment hello-world --type=LoadBalancer --port 8080

为什么我无法访问该服务?看来我在每个步骤中都打开了端口 8080 1) Flask 应用程序 2) Docker文件 3) Pod 部署 4) Service 创建。

我认为您在公开部署时也应该指出目标端口,如下所示:

kubectl expose deployment hello-world --type=LoadBalancer --port=8080 --target-port=8080

希望对您有所帮助