【发布时间】:2021-03-16 09:45:09
【问题描述】:
我在 Kubernetes 中部署了一个应用程序(正面:Angular,背面:Spring)。我有 3 个 pod:正面、背面、数据库(Mongo)。我的 pod 运行正常,并且后面的 pod 已成功连接到 Mongo pod。我使用 NGINX 对 Angular 应用程序进行了 docker 化。
现在,当我访问三个 pod 的日志时,我没有任何问题,即使是 spring 应用程序也已成功启动。 O 甚至使用 curl x Post, get... 从前端 pod 测试 API 服务,一切正常。
但是在我的浏览器中,没有任何效果。它显示 'connection to http//:localhost:8080/api refused' !!
我在我的 Angular 项目中删除了任何 'localhost' 单词,并将它们全部替换为后面的服务名称,但仍然告诉我 'cannot connect to localhost'!!!
这里是services.yaml:
apiVersion: v1
kind: Service
metadata:
name: front-service
labels:
app: front
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: http
selector:
app: front
type: NodePort
---
apiVersion: v1
kind: Service
metadata:
name: back-service
labels:
app: back
spec:
ports:
- port: 8080
targetPort: 8080
protocol: TCP
name: http
selector:
app: back
type: ClusterIP
Dockerfile 的 Angular:
# Stage 0, "build-stage", based on Node.js, to build and compile the frontend
FROM node:10.8.0 as build-stage
WORKDIR /app
RUN rm -rf node_modules
RUN npm cache clean --force
RUN npm install
COPY package*.json /app/
COPY ./ /app/
ARG configuration=production
RUN npm run build -- --output-path=./dist/parkingangular
# Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx
FROM nginx:1.15
#Copy ci-dashboard-dist
COPY --from=build-stage /app/dist/parkingangular/ /usr/share/nginx/html
#Copy default nginx configuration
RUN rm -rf /etc/nginx/conf.d/default.conf
COPY ./nginx.conf /etc/nginx/conf.d
EXPOSE 80
nginx.conf:
upstream back-service {
server back-service:8080;
}
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /api {
proxy_pass http://back-service;
}
}
get pods:
NAME READY STATUS RESTARTS AGE
backend-755c4bd78d-r4gk4 1/1 Running 0 125m
backend-755c4bd78d-z2pjt 1/1 Running 0 125m
frontend-546f8db776-62srl 1/1 Running 0 116m
frontend-546f8db776-jngt8 1/1 Running 0 116m
mongodb-66689f754d-hn5tm 1/1 Running 1 2d7h
kubectl get svc:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
back-service ClusterIP 10.108.235.196 <none> 8080/TCP 5d3h
front-service NodePort 10.103.234.24 <none> 80:32594/TCP 5d4h
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d7h
mongoservice ClusterIP 10.109.235.5 <none> 27017/TCP 2d8h
我什至让 npm clear cache 和 npm clean 但徒劳无功!!
【问题讨论】:
-
使用 nginx ingree 的入口解决了问题 :) 谢谢大家
标签: angular docker nginx kubernetes