Search code examples
javaspring-bootkubernetesnetflix-eureka

Reaching Eureka Server pod Kubernetes


I have 2 Springboot microservices, one is the Eureka server and the other one is the Gateway.

I can't find the right configuration to make the Gateway register to the Eureka server.

This is the eureka.yml with the K8s configuration:

Eureka.yml

apiVersion: v1
kind: ConfigMap
metadata:
  name: eureka-cm
data:
  eureka_service_address: http://eureka-0.eureka:8761/eureka

---

apiVersion: v1
kind: Service
metadata:
  name: eureka
  labels:
    app: eureka
spec:
  clusterIP: None
  ports:
    - port: 8761
      name: eureka
  selector:
    app: eureka

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: eureka
spec:
  serviceName: "eureka"
  replicas: 1
  selector:
    matchLabels:
      app: eureka
  template:
    metadata:
      labels:
        app: eureka
    spec:
      containers:
        - name: eureka
          image: myrepo/eureka1.0:eureka
          imagePullPolicy: Always
          ports:
            - containerPort: 8761
          env:
            - name: EUREKA_SERVER_ADDRESS
              valueFrom:
                configMapKeyRef:
                  name: eureka-cm
                  key: eureka_service_address


---                  

apiVersion: v1
kind: Service
metadata:
  name: eureka-lb
  labels:
    app: eureka
spec:
  selector:
    app: eureka
  type: NodePort
  ports:
    - port: 80
      targetPort: 8761

Eureka.application.yml

spring:
  application:
    name: eureka
server:
  port: 8761
eureka:
  instance:
    hostname: "${HOSTNAME}.eureka"
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: ${EUREKA_SERVER_ADDRESS}

Gateway.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: cloud-gateway-app
  labels:
    app: cloud-gateway-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: cloud-gateway-app
  template:
    metadata:
      labels:
        app: cloud-gateway-app
    spec:
      containers:
        - name: cloud-gateway-app
          image: myrepo/gateway1.0:gateway
          imagePullPolicy: Always
          ports:
            - containerPort: 9191

---
apiVersion: v1
kind: Service
metadata:
  name: cloud-gateway-svc
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 9191
      protocol: TCP
  selector:
    app: cloud-gateway-app

Gateway.application.yml

eureka:
  instance:
    preferIpAddress: true
    hostname: eureka-0
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://eureka-0.eureka.default.svc.cluster.local:8761/eureka

This is the error I got when I check the logs of the Gateway's pod:

error on POST request for "http://eureka-0.eureka.default.svc.cluster.local:8761/eureka/apps/API-GATEWAY": eureka-0.eureka.default.svc.cluster.local; nested exception is java.net.UnknownHostException: eureka-0.eureka.default.svc.cluster.local

Following the documentation I've tried to set defaultZone property of the Gateway.application.properties file following this pattern:

172-17-0-3.default.pod.cluster.local:8761/eureka

But in this way too, I can't subscribe to the Eureka Server.


Solution

  • I resolved by modifying the Gateway.application.yml in this way:

    eureka:
      instance:
        preferIpAddress: true
        hostname: eureka-0
      client:
        registerWithEureka: true
        fetchRegistry: true
        serviceUrl:
          defaultZone: http://eureka-0.eureka:8761/eureka/
    

    EDIT:

    I'm encountering some problems in registering other microservices to the Eureka Server. I've tried by increasing the replicas of the Eureka Server and made each microservice register to a dedicated replica, but as now this is not working.