无法通过 Windows 上的 minikube (Kubernetes) 为 Angular 应用程序提供服务

Unable to serve Angular application through minikube (Kubenetes) on Windows

我正在 运行宁 Docker Windows 和 Minikube (Kubernetes) 来服务 Angular 应用程序。 angular 应用程序使用 ng serve 提供服务,并且在 windows 上正常工作。我创建了以下 Docker 文件:

# Use an official Nodejs runtime as a parent image
FROM node:6.10.3

#Create an unprivileged user 'app'
#Install dependencies
RUN useradd --user-group --create-home --shell /bin/false app &&\
   npm install -g grunt-cli &&\
   npm install -g @angular/cli

# Define environment variable
ENV ENVIRONMENT="dev" HOME=/home/app

#Copy the code to the image
COPY . $HOME/frontend/

#Chmod the files to allow unprivileged user 'app' to read/write
RUN chown -R app:app $HOME/*

#Use unprivileged user 'app' to run the app inside the container
USER app

#Set Current Working Directory of the runtime 
WORKDIR $HOME/frontend

#Install NPM dependencies
RUN npm install

# Make port 8080 and 4200 available to the world outside this container
EXPOSE 4200

# Start the webpack server when the container launches
CMD ng serve

我在 windows 中为 minikube 设置了我的环境变量以及我的 Docker 变量(minikube docker-env)

DOCKER_API_VERSION
DOCKER_CERT_PATH
DOCKER_HOST
DOCKER_MACHINE_NAME
DOCKER_TLS_VERIFY

我构建了 Docker 图像并将其标记为 v1 :

docker build -t frontend:v1 .

我从镜像中部署了 POD 并公开了部署:

kubectl run frontend --image=frontend:v1 --port=4200
kubectl expose deployment frontend --type=LoadBalancer

kubectl get pods 显示我的 pod 为 运行ning,kubectl get deployments 显示我的部署可用

我使用 kubectl exec curl localhost:4200 执行了 curl,它在容器上执行,它正确地返回了我的页面,但是当我尝试使用 minikube service frontend 运行 我的服务时,它打开了浏览器但无法访问我的应用程序。

值得注意的是,我使用 node server.js 对普通 Nodejs 应用程序 运行ning 执行了完全相同的步骤,并且使用完全相同的步骤正确地提供了服务。

知道为什么我的 angular 应用程序在开始使用 ng serve 时没有通过 Kubernetes 提供服务,或者如何着手找出问题所在?我可以从 Kubernetes 仪表板看到映射是正确的。

这可能与angular-cli更相关。默认情况下,ng serve 仅绑定到 localhost。因此,您的 docker 容器没有监听它的 public IP 地址。

尝试使用:

ng serve --host 0.0.0.0

Minikube 并不真正支持 LoadBalancer 的服务类型。它不会呕吐,但你应该仔细阅读它的真正含义。做

minikube service list

查看您可以命中的实际端点。塞巴斯蒂安的观点可能仍然适用

尝试公开端口 80 而不是 4200

kubectl run frontend --image=frontend:v1 --port=80