Eureka and Kubernetes
How can I setup something like eureka.client.serviceUri?
You have to have a Kubernetes service on top of the eureka pods/deployments which then will provide you a referable IP address and port number. And then use that referable address to look up the Eureka service, instead of "8761".
To address further question about HA configuration of Eureka
You shouldn't have more than one pod/replica of Eureka per k8s service (remember, pods are ephemeral, you need a referable IP address/domain name for eureka service registry). To achieve high availability (HA), spin up more k8s services with one pod in each.
- Eureka service 1 --> a single pod
- Eureka Service 2 --> another single pod
- ..
- ..
- Eureka Service n --> another single pod
So, now you have referable IP/Domain name (IP of the k8s service) for each of your Eureka.. now it can register each other.
Feeling like it's an overkill?If all your services are in same kubernetes namespace you can achieve everything (well, almost everything, except client side load balancing) that eureka offers though k8s service + KubeDNS add-On. Read this article by Christian Posta
Edit
Instead of Services with one pod each, you can make use of StatefulSets as Stefan Ocke pointed out.
Like a Deployment, a StatefulSet manages Pods that are based on an identical container spec. Unlike a Deployment, a StatefulSet maintains a sticky identity for each of their Pods. These pods are created from the same spec, but are not interchangeable: each has a persistent identifier that it maintains across any rescheduling.
Regarding HA configuration of Eureka in Kubernetes: You can (meanwhile) use a StatefulSet for this instead of creating a service for each instance. The StatefulSet guarantees stable network identity for each instance you create.For example, the deployment could look like the following yaml (StatefulSet + headless Service).There are two Eureka instances here, according to the DNS naming rules for StatefulSets (assuming namespace is "default"):
eureka-0.eureka.default.svc.cluster.local and
eureka-1.eureka.default.svc.cluster.local
As long as your pods are in the same namespace, they can reach Eureka also as:
- eureka-0.eureka
- eureka-1.eureka
Note: The docker image used in the example is from https://github.com/stefanocke/eureka. You might want to chose or build your own one.
---apiVersion: v1kind: Servicemetadata: name: eureka labels: app: eurekaspec: ports: - port: 8761 name: eureka clusterIP: None selector: app: eureka--- apiVersion: apps/v1beta2kind: StatefulSetmetadata: name: eurekaspec: serviceName: "eureka" replicas: 2 selector: matchLabels: app: eureka template: metadata: labels: app: eureka spec: containers: - name: eureka image: stoc/eureka ports: - containerPort: 8761 env: - name: MY_POD_NAME valueFrom: fieldRef: fieldPath: metadata.name # Due to camelcase issues with "defaultZone" and "preferIpAddress", _JAVA_OPTIONS is used here - name: _JAVA_OPTIONS value: -Deureka.instance.preferIpAddress=false -Deureka.client.serviceUrl.defaultZone=http://eureka-0.eureka:8761/eureka/,http://eureka-1.eureka:8761/eureka/ - name: EUREKA_CLIENT_REGISTERWITHEUREKA value: "true" - name: EUREKA_CLIENT_FETCHREGISTRY value: "true" # The hostnames must match with the the eureka serviceUrls, otherwise the replicas are reported as unavailable in the eureka dashboard - name: EUREKA_INSTANCE_HOSTNAME value: ${MY_POD_NAME}.eureka # No need to start the pods in order. We just need the stable network identitypodManagementPolicy: "Parallel"
@Stefan Ocke i'm trying to the same setup the same, but with my own image of eureka server. but i keep getting this error
Request execution failed with message: java.net.ConnectException: Connection refused (Connection refused)2019-09-27 06:27:03.363 ERROR 1 --- [ main] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://eureka-1.eureka:8761/eureka/}
Here are configurations:
Eureka Spring Properties:
server.port=${EUREKA_PORT}spring.security.user.name=${EUREKA_USERNAME}spring.security.user.password=${EUREKA_PASSWORD}eureka.client.register-with-eureka=trueeureka.client.fetch-registry=trueeureka.instance.prefer-ip-address=falseeureka.server.wait-time-in-ms-when-sync-empty=0eureka.server.eviction-interval-timer-in-ms=15000eureka.instance.leaseRenewalIntervalInSeconds=30eureka.instance.leaseExpirationDurationInSeconds=30eureka.instance.hostname=${EUREKA_INSTANCE_HOSTNAME}eureka.client.serviceUrl.defaultZone=http://eureka-0.eureka:8761/eureka/,http://eureka-1.eureka:8761/eureka/
StatefulSet Config:
apiVersion: apps/v1kind: StatefulSetmetadata: name: eurekaspec: serviceName: "eureka" podManagementPolicy: "Parallel" replicas: 2 selector: matchLabels: app: eureka template: metadata: labels: app: eureka spec: containers: - name: eureka image: "my-image" command: ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar","/app/eureka-service.jar"] ports: - containerPort: 8761 env: - name: EUREKA_PORT value: "8761" - name: EUREKA_USERNAME value: "theusername" - name: EUREKA_PASSWORD value: "thepassword" - name: MY_POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: EUREKA_INSTANCE_HOSTNAME value: ${MY_POD_NAME}.eureka
Service Config:
apiVersion: v1kind: Servicemetadata: name: eureka labels: app: eurekaspec: clusterIP: None selector: app: eureka ports: - port: 8761 targetPort: 8761
Ingress Controller:
apiVersion: extensions/v1beta1kind: Ingressmetadata: name: ingress-service annotations: kubernetes.io/ingress.class: nginx nginx.ingress.kubernetes.io/rewrite-target: /spec: rules: - http: paths: - path: / backend: serviceName: eureka servicePort: 8761