kubernetes 服务中请求失败
我正在使用 Kubernetes 创建微服务基础设施。我有两个服务,一个身份验证模块和客户端服务。客户端服务向身份验证模块发出一些请求,以获取有关用户、身份验证 cookie 等的信息。
这是基础设施:
apiVersion: apps/v1
kind: Deployment
metadata:
name: auth-depl
spec:
replicas: 1
selector:
matchLabels:
app: auth
template:
metadata:
labels:
app: auth
spec:
containers:
- name: auth
image: theimage
env:
- name: JWT_KEY
valueFrom:
secretKeyRef:
name: jwt-secret
key: JWT_KEY
resources:
limits:
cpu: "1"
memory: "512Mi"
requests:
cpu: "1"
memory: "512Mi"
---
apiVersion: v1
kind: Service
metadata:
name: auth-srv
spec:
selector:
app: auth
ports:
- name: auth
protocol: TCP
port: 3000
targetPort: 3000
显然工作正常。然后,客户端模块:
apiVersion: apps/v1
kind: Deployment
metadata:
name: client-depl
spec:
replicas: 1
selector:
matchLabels:
app: client
template:
metadata:
labels:
app: client
spec:
containers:
- name: client
image: theimage
---
apiVersion: v1
kind: Service
metadata:
name: client-srv
spec:
selector:
app: client
ports:
- name: client
protocol: TCP
port: 3000
targetPort: 3000
为了保持API一致性,我使用nginx-ingress。它似乎工作完美,配置如下:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-service
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- host: kome.dev
http:
paths:
- path: /api/users/?(.*)
pathType: Prefix
backend:
service:
name: auth-srv
port:
number: 3000
- path: /?(.*)
pathType: Prefix
backend:
service:
name: client-srv
port:
number: 3000
问题是我的客户端(用 nextjs 编写)必须向身份验证模块的特定路由发出请求:
Index.getInitialProps = async () => {
if (typeof window === 'undefined') {
// server side
const response = await axios.get('http://ingress-nginx-controller.ingress-nginx.svc.cluster.local/api/users/currentuser')
return response
} else {
// client side
const response = await axios.get('/api/users/currentuser')
return response
}
}
收到 404 错误,但端点存在并且在外部工作正常ingress-nginx 的。
I am creating a micro services infrastructure with Kubernetes. I have two services, an authentication module and client service. The client service make some requests to authentication module in order to get information about the user, the authentication cookie and etc. etc. etc.
This is the infraestructure:
apiVersion: apps/v1
kind: Deployment
metadata:
name: auth-depl
spec:
replicas: 1
selector:
matchLabels:
app: auth
template:
metadata:
labels:
app: auth
spec:
containers:
- name: auth
image: theimage
env:
- name: JWT_KEY
valueFrom:
secretKeyRef:
name: jwt-secret
key: JWT_KEY
resources:
limits:
cpu: "1"
memory: "512Mi"
requests:
cpu: "1"
memory: "512Mi"
---
apiVersion: v1
kind: Service
metadata:
name: auth-srv
spec:
selector:
app: auth
ports:
- name: auth
protocol: TCP
port: 3000
targetPort: 3000
Apparently working properly. Then, the client module:
apiVersion: apps/v1
kind: Deployment
metadata:
name: client-depl
spec:
replicas: 1
selector:
matchLabels:
app: client
template:
metadata:
labels:
app: client
spec:
containers:
- name: client
image: theimage
---
apiVersion: v1
kind: Service
metadata:
name: client-srv
spec:
selector:
app: client
ports:
- name: client
protocol: TCP
port: 3000
targetPort: 3000
In order to maintain API consistency, I am using nginx-ingress. It seems to work perfectly and here is the configuration:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-service
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- host: kome.dev
http:
paths:
- path: /api/users/?(.*)
pathType: Prefix
backend:
service:
name: auth-srv
port:
number: 3000
- path: /?(.*)
pathType: Prefix
backend:
service:
name: client-srv
port:
number: 3000
The problem is that my client, written in nextjs, has to make a request to a specific route of the authentication module:
Index.getInitialProps = async () => {
if (typeof window === 'undefined') {
// server side
const response = await axios.get('http://ingress-nginx-controller.ingress-nginx.svc.cluster.local/api/users/currentuser')
return response
} else {
// client side
const response = await axios.get('/api/users/currentuser')
return response
}
}
is getting a 404 error, but the endpoint exists and works fine outside of ingress-nginx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试删除您的入口清单中名为 host 的节点,然后重试,您的应用尝试向您的入口发出本地请求,但入口配置了域
kome.dev 所以这是入口返回 404 错误的原因(因为你的应用程序在向你的入口发出请求时不是来自 kome.dev 域),为了修复,你可以添加一个
localhost
域或允许的通配符所有域(不是最佳实践)。Please try to delete the node named host in your ingress manifiest and try again, your app try to make a local request to your ingress but the ingress is configure with the domain
kome.dev
so this is the cause for ingress return to a 404 error (because your app when make a request to your ingress dont come from to kome.dev domain), for fix that you can add alocalhost
domain or a wildcard for allow all domains (not is a best practice).您可以保留
- host: kome.dev
,您的 axios 调用可以包含如下标头:You can keep the
- host: kome.dev
, your axios call can include the header like: