NGINX 入口未到达服务(clusterIP)
NGINX Ingress is not reaching Service(clusterIP)
我正在尝试在 Kubernetes 集群中部署节点 js 应用程序。我为此创建了部署和服务(clusterIp),并在 nginx 入口配置文件中添加了一个角色,但似乎 nginx 无法访问该服务。
NodeJs 应用程序:
'use strict';
const express = require('express');
// Constants
const PORT = 5678;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
console.log("request received");
res.send('Hello World');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
Docker 文件:
FROM node:14
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
EXPOSE 5678
CMD [ "node", "server.js" ]
部署:
apiVersion: apps/v1
kind: Deployment
metadata:
name: apple-app-deployment
labels:
app: apple
spec:
replicas: 2
selector:
matchLabels:
app: apple
template:
metadata:
labels:
app: apple
spec:
containers:
- name: apple-app
image: anudcker/node-web-appp:1
ports:
- containerPort: 5678
服务:
kind: Service
apiVersion: v1
metadata:
name: apple-service
spec:
selector:
app: apple
ports:
- port: 5678 # Default port for image
targetPort: 5678
入口:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
# kubernetes.io/ingress.class: azure/application-gateway
ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /apple
pathType: Prefix
backend:
service:
name: apple-service
port:
number: 5678
现在,每当我尝试通过点击 /apple 访问服务时,我都会收到回复
Cannot GET /apple
来自 Nginx 入口。我在 nginx 入口控制器 pod 中找到了以下日志。
10.244.1.1 - - [07/May/2021:06:22:29 +0000] "GET /apple HTTP/1.1" 404 144 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36" 465 0.004 [demo-apple-service-5678] [] 10.244.0.24:5678 144 0.004 404 5a5bdf8f61d59b665218be0a18a6ab4c
有什么想法吗?
问题是您正在使用 networking.k8s.io/v1beta1
的 rewrite-target
,以及 apiVersion: networking.k8s.io/v1
。
您需要将注释更新为新注释:
nginx.ingress.kubernetes.io/rewrite-target: /
我正在尝试在 Kubernetes 集群中部署节点 js 应用程序。我为此创建了部署和服务(clusterIp),并在 nginx 入口配置文件中添加了一个角色,但似乎 nginx 无法访问该服务。
NodeJs 应用程序:
'use strict';
const express = require('express');
// Constants
const PORT = 5678;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
console.log("request received");
res.send('Hello World');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
Docker 文件:
FROM node:14
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
EXPOSE 5678
CMD [ "node", "server.js" ]
部署:
apiVersion: apps/v1
kind: Deployment
metadata:
name: apple-app-deployment
labels:
app: apple
spec:
replicas: 2
selector:
matchLabels:
app: apple
template:
metadata:
labels:
app: apple
spec:
containers:
- name: apple-app
image: anudcker/node-web-appp:1
ports:
- containerPort: 5678
服务:
kind: Service
apiVersion: v1
metadata:
name: apple-service
spec:
selector:
app: apple
ports:
- port: 5678 # Default port for image
targetPort: 5678
入口:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
# kubernetes.io/ingress.class: azure/application-gateway
ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /apple
pathType: Prefix
backend:
service:
name: apple-service
port:
number: 5678
现在,每当我尝试通过点击 /apple 访问服务时,我都会收到回复
Cannot GET /apple
来自 Nginx 入口。我在 nginx 入口控制器 pod 中找到了以下日志。
10.244.1.1 - - [07/May/2021:06:22:29 +0000] "GET /apple HTTP/1.1" 404 144 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36" 465 0.004 [demo-apple-service-5678] [] 10.244.0.24:5678 144 0.004 404 5a5bdf8f61d59b665218be0a18a6ab4c
有什么想法吗?
问题是您正在使用 networking.k8s.io/v1beta1
的 rewrite-target
,以及 apiVersion: networking.k8s.io/v1
。
您需要将注释更新为新注释:
nginx.ingress.kubernetes.io/rewrite-target: /