Home > Enterprise >  Request failed within services in kubernetes
Request failed within services in kubernetes

Time:03-01

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.

CodePudding user response:

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).

CodePudding user response:

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

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