Raven
Raven

Reputation: 147

Kubernetes: spring cloud gateway not working

I have a spring cloud gateway that works fine in the docker configuration, like this: (all routes/services except ratings are removed for readability's sake)

@Value("${hosts.ratings}")
private String ratingsPath;

@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
    return builder.routes()
        .route(r -> r.host("*").and().path("/api/ratings/**")
                .uri(ratingsPath + ":2226/api/ratings/"))
                ...other routes...
        .build();
}

This gets it values from the application.properties locally, and from an environment variable in docker, like so in the docker-compose:

  apigw:
    build: ./Api-Gateway
    container_name: apigw
    links:
      - ratings
      ...
    depends_on:
      - ratings
      ...
    ports:
      - "80:8080"
    environment:
      - hosts_ratings=http://ratings
      ...

This configuration works just fine. However, when porting this to our kubernetes cluster, all routes get a 404. The deployment of our api gateway is as follows:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: apigw
  name: apigw-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: apigw
  template:
    metadata:
      labels:
        app: apigw
    spec:
      containers:
      - name: apigw
        image: redacted
        ports:
        - containerPort: 8080
        env:
        - name: hosts_ratings
          value: "ratings-service.default.svc.cluster.local"
        ...

With ratings-service being our ratings service (that definitely works, because when exposing it directly from its service, it does work), defined like this:

apiVersion: v1
kind: Service
metadata: 
  name: ratings-service
  labels:
    app: ratings
spec:
  selector:
    app: ratings
  ports:
    - port: 2226
      targetPort: 2226

The service of our api gateway is as follows, using bare metal with an external IP that does work:

apiVersion: v1
kind: Service
metadata: 
  name: apigw-service
  labels:
    app: apigw
spec:
  selector:
    app: apigw
  ports:
    - port: 80
      targetPort: 8080
  externalIPs:
    - A.B.C.D

How I believe it should work is that ratings-service.default.svc.cluster.local would get translated to the correct ip, filled in to the ratingsPath variable, and the query would succeed, but this is not the case.
Our other services are able to communicate in the same way, but the api gateway does not seem to be able to do that. What could be the problem?

Upvotes: 2

Views: 1402

Answers (1)

Mikolaj S.
Mikolaj S.

Reputation: 3224

Posting community wiki based on comment for better visibility. Feel free to expand it.


The issue was a faulty version of the image:

It seems like the service i was using just straight up didn't work. Must have been a faulty version of the image i was using.

Check also:

Upvotes: 1

Related Questions