kubernetes 服务中请求失败

发布于 2025-01-10 16:09:45 字数 2598 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狠疯拽 2025-01-17 16:09:45

请尝试删除您的入口清单中名为 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 a localhost domain or a wildcard for allow all domains (not is a best practice).

池木 2025-01-17 16:09:45

您可以保留 - host: kome.dev,您的 axios 调用可以包含如下标头:

await axios.get('...', {
  headers: {
    'Host': 'kome.dev'
  }
})

You can keep the - host: kome.dev, your axios call can include the header like:

await axios.get('...', {
  headers: {
    'Host': 'kome.dev'
  }
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文