<p>I'm developing a project with microservices architecture. I have Spring cloud gateway, eureka service discovery and book microservice. When i run these application locally everything works. When i run these with docker compose still no problems. But when i deploy these to kubernetes i get error on api gateway. I get this error when i send get request to /book on api gateway</p>
<p>Error on api gateway:</p>
<pre><code>java.net.UnknownHostException: failed to resolve 'book-service-55665db7ff-bd75t' after 2 queries 
        at io.netty.resolver.dns.DnsResolveContext.finishResolve(DnsResolveContext.java:1046) ~[netty-resolver-dns-4.1.68.Final.jar!/:4.1.68.Final]
        Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
        |_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
        |_ checkpoint ⇢ HTTP GET &quot;/book&quot; [ExceptionHandlingWebHandler]
</code></pre>
<p>api gateway config:</p>
<pre><code>server:
  port: 8084
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
      - id: book
        uri: lb://BOOK-SERVICE/book
        predicates:
        - Path=/book/**
      - id: author
        uri: lb://BOOK-SERVICE/author
        predicates:
        - Path=/author/**
      - id: genre
        uri: lb://BOOK-SERVICE/genre
        predicates:
        - Path=/genre/**
</code></pre>
<p>book microservice config:</p>
<pre><code>spring:
  application:
    name: BOOK-SERVICE
  datasource:
    url: jdbc:postgresql://localhost:5432/book_service
    username: postgres
    password: password
  jpa:
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQL10Dialect
server:
  port: 8081
</code></pre>
<p>kubernetes manifest:</p>
<pre><code>#secret
apiVersion: v1
kind: Secret
metadata:
  name: database
type: Opaque
data:
  password: cGFzc3dvcmQ=
---
#api gateway
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-gateway
  labels:
    app: api-gateway
spec:
  replicas: 1
  selector:
    matchLabels:
      app: api-gateway
  template:
    metadata:
      labels:
        app: api-gateway
    spec:
      containers:
      - name: api-gateway
        image: rikaciv802/api-gateway
        ports:
        - containerPort: 8084
        env:
        - name: eureka.client.serviceUrl.defaultZone
          value: http://service-discovery:8761/eureka
---
apiVersion: v1
kind: Service
metadata:
  name: api-gateway
spec:
  selector:
    app: api-gateway
  type: LoadBalancer
  ports:
    - protocol: TCP
      port: 8084
      targetPort: 8084
      nodePort: 30000
---
#service discovery
apiVersion: apps/v1
kind: Deployment
metadata:
  name: service-discovery
  labels:
    app: service-discovery
spec:
  replicas: 1
  selector:
    matchLabels:
      app: service-discovery
  template:
    metadata:
      labels:
        app: service-discovery
    spec:
      containers:
      - name: service-discovery
        image: rikaciv802/service-discovery
        ports:
        - containerPort: 8761
---
apiVersion: v1
kind: Service
metadata:
  name: service-discovery
spec:
  selector:
    app: service-discovery
  ports:
    - protocol: TCP
      port: 8761
      targetPort: 8761
---
#book service
apiVersion: apps/v1
kind: Deployment
metadata:
  name: book-service-database
  labels:
    app: book-service-database
spec:
  replicas: 1
  selector:
    matchLabels:
      app: book-service-database
  template:
    metadata:
      labels:
        app: book-service-database
    spec:
      containers:
      - name: book-service-database
        image: postgres:14.0
        ports:
        - containerPort: 5432
        env:
        - name: POSTGRES_DB
          value: book_service
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: database
              key: password
---
apiVersion: v1
kind: Service
metadata:
  name: book-service-database
spec:
  selector:
    app: book-service-database
  ports:
    - protocol: TCP
      port: 5432
      targetPort: 5432
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: book-service
  labels:
    app: book-service
spec:
  replicas: 1
  selector:
    matchLabels:
      app: book-service
  template:
    metadata:
      labels:
        app: book-service
    spec:
      containers:
      - name: book-service
        image: rikaciv802/book-service
        ports:
        - containerPort: 8081
        env:
        - name: eureka.client.serviceUrl.defaultZone
          value: http://service-discovery:8761/eureka
        - name: SPRING_DATASOURCE_URL
          value: jdbc:postgresql://book-service-database:5432/book_service
        - name: SPRING_DATASOURCE_PASSWORD
          valueFrom:
            secretKeyRef:
              name: database
              key: password
---
apiVersion: v1
kind: Service
metadata:
  name: book-service
spec:
  selector:
    app: book-service
  ports:
    - protocol: TCP
      port: 8081
      targetPort: 8081
</code></pre>
<p>service discovery config:</p>
<pre><code>eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
server:
  port: 8761
</code></pre>

mrcoderbritish
<p>It seems you have DNS issues with pod hostnames. In kubernetes DNS name of pods are generally formatted like this: <code>pod-ip-address.deployment-name.my-namespace.svc.cluster-domain.example</code> (see <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pods" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pods</a>)</p>
<p>You can workaround this by using IP address: set <code>eureka.instance.preferIpAddress</code> to <code>true</code>.</p>
<p>See <a href="https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-eureka-server.html#spring-cloud-eureka-server-prefer-ip-address" rel="nofollow noreferrer">https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-eureka-server.html#spring-cloud-eureka-server-prefer-ip-address</a></p>
