SpringBoot could not access ElasticSearch on kubernetes

6/12/2019

Spring Boot access ElasticSearch Problem on kubernetes Problem

If ElasticSearch installed locally then Spring Boot Application can access it via spring.data.elasticsearch.cluster-nodes=localhost:9300

But when both of them installed on K8s, SpringBoot could not access it anymore

the deployment yaml of spring boot is like below

apiVersion: apps/v1
kind: Deployment
metadata:
  name: springbootelastic
spec:
  selector:
    matchLabels:
      run: springbootelastic
  replicas: 1
  template:
    metadata:
      labels:
        run: springbootelastic
    spec:
      containers:
        - name: springbootelastic
          image: wangyan100/springbootelastic:latest
          imagePullPolicy: Always
          env:
            - name: CLUSTER_NODES
              value: elasticsearch:9300
            - name: CLUSTER_HOST
              value: elasticsearch
      #imagePullSecrets:
      #  - name: regcred

all yaml files and code you could find it at https://github.com/wangyan100/springbootexamples/tree/master/spring-boot-elasticsearch/src

The exception I got at spring boot 's pod log is as below shown

: Adding transport node : 10.108.175.123:9300 not part of the cluster Cluster [elasticsearch], ignoring...

: failed to load elasticsearch nodes : org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{RwIDGzHOQFqU1Lrl0yULkQ}{elasticsearch}{10.108.175.123:9300}]

elasticsearch running on pod, I could browser it via http://192.168.99.113:31183 (31183 is nodeport )

{
  "name" : "c97xU38",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "3FQq0XXeQjuzDRqXO2wY6w",
  "version" : {
    "number" : "6.4.3",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "fe40335",
    "build_date" : "2018-10-30T23:17:19.084789Z",
    "build_snapshot" : false,
    "lucene_version" : "7.4.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}
-- stewchicken
kubernetes
spring-boot

2 Answers

6/13/2019

Can you verify the output of http://elasticsearch:9200, you should see something like this:

{

    "name": "5M3kvCf",
    "cluster_name": "docker-cluster",
    "cluster_uuid": "tbbXSaIIQhearMZckxpEQQ",
    "version": {
        "number": "6.2.4",
        "build_hash": "ccec39f",
        "build_date": "2018-04-12T20:37:28.497551Z",
        "build_snapshot": false,
        "lucene_version": "7.2.1",
        "minimum_wire_compatibility_version": "5.6.0",
        "minimum_index_compatibility_version": "5.0.0"
    },
    "tagline": "You Know, for Search"
}

The cluster name should match with:

spring.data.elasticsearch.cluster-name

in your spring configuration.

-- earandap
Source: StackOverflow

6/15/2019

Problem solved. I wrote it at this link.

Deploy SpringBootApplication and ElasticSearch on Kubernetes

This demo will show you how to deploy SpringBootApplication and ElasticSearch on Kubernetes.

The demo will use the SpringBootApplication from this link.

Build docker image

  • gradle clean build

  • docker build -t wangyan100/springbootelastic .

Push docker image (wangyan100/springbootelastic) to docker hub

  • create wangyan100/springbootelastic as repository at Docker Hub website. Of course, you need register an account at Docker Hub website if you don't have one.

  • docker login

  • enter docker hub's username and password.

  • docker tag <image_id> wangyan100/springbootelastic:latest

  • docker push wangyan100/springbootelastic:latest

Write deployment and service yaml files

  • for SpringBootApplication, you could find example as shown below.

    1

  • for elasticsearch, you could find example as shown below.

    2

Install Kubernetes on your local machine

  • Follow the link to install Kubernetes on your local PC.

Deploy Application on Kubernetes

  • minikube start --memory 4096 (elasticsearch requires 4GB memory).

  • Deploy elasticsearch.

    cd spring-boot-elasticsearch/src/elastick8s
    kubectl apply -f . 
    
  • Wait until elasticsearch is up and running.

    kubectl get pods
    NAME                                 READY   STATUS    RESTARTS   
    elasticsearch-5b74bbdd86-wgw2n       1/1     Running   0        
  • Deploy SpringBootApplication.

    cd spring-boot-elasticsearch/src/springbootk8s
    kubectl apply -f . 
    
  • Get exposed URL of springboot and elasticsearch.

    minikube service springbootelastic --url
    http://192.168.99.113:31742
    
    minikube service elasticsearch --url
    http://192.168.99.113:30765
    http://192.168.99.113:32663
    
  • Use postman to send request to SpringBootApplication, it will create an entry at elasticsearch.

    3

  • Check result, it works.

    4

-- stewchicken
Source: StackOverflow