<p>Spring Boot access ElasticSearch Problem on kubernetes Problem</p>

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

<p>But when both of them installed on K8s, SpringBoot could not access it anymore</p>

<p>the deployment yaml of spring boot is like below</p>

<pre><code>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


</code></pre>

<p>all yaml files and code you could find it at 
<a href="https://github.com/wangyan100/springbootexamples/tree/master/spring-boot-elasticsearch/src" rel="nofollow noreferrer">https://github.com/wangyan100/springbootexamples/tree/master/spring-boot-elasticsearch/src</a></p>

<p>The exception I got at spring boot 's pod log is as below shown</p>

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

<p>: 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}]</p>

<p>elasticsearch running on pod, I could browser it via <a href="http://192.168.99.113:31183" rel="nofollow noreferrer">http://192.168.99.113:31183</a> (31183 is nodeport )</p>

<pre><code>{
  "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"
}
</code></pre>

stewchicken
<p>Problem solved. I wrote it <a href="https://github.com/wangyan100/springbootexamples/tree/master/spring-boot-k8s" rel="nofollow noreferrer">at this link</a>.</p>

<h3>Deploy SpringBootApplication and ElasticSearch on Kubernetes</h3>

<p>This demo will show you how to deploy SpringBootApplication and ElasticSearch on Kubernetes.</p>

<p>The demo will use the SpringBootApplication from <a href="https://github.com/wangyan100/springbootexamples/tree/master/spring-boot-elasticsearch" rel="nofollow noreferrer">this link</a>.</p>

<p><strong>Build docker image</strong></p>

<ul>
<li><p>gradle clean build</p></li>
<li><p>docker build -t wangyan100/springbootelastic .</p></li>
</ul>

<p><strong>Push docker image (wangyan100/springbootelastic) to docker hub</strong></p>

<ul>
<li><p>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.</p></li>
<li><p>docker login</p></li>
<li><p>enter docker hub's username and password.</p></li>
<li><p>docker tag &lt;image_id&gt; wangyan100/springbootelastic:latest</p></li>
<li><p>docker push wangyan100/springbootelastic:latest</p></li>
</ul>

<p><strong>Write deployment and service yaml files</strong></p>

<ul>
<li><p>for SpringBootApplication, you could find example as shown below. </p>

<p><a href="https://i.stack.imgur.com/gwfsk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gwfsk.png" alt="1"></a></p></li>
<li><p>for elasticsearch, you could find example as shown below.</p>

<p><a href="https://i.stack.imgur.com/5MZmO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5MZmO.png" alt="2"></a></p></li>
</ul>

<p><strong>Install Kubernetes on your local machine</strong></p>

<ul>
<li>Follow <a href="https://kubernetes.io/docs/tasks/tools/install-minikube/" rel="nofollow noreferrer">the link</a> to install Kubernetes on your local PC.</li>
</ul>

<p><strong>Deploy Application on Kubernetes</strong></p>

<ul>
<li><p>minikube start --memory 4096 (elasticsearch requires 4GB memory).</p></li>
<li><p>Deploy elasticsearch.</p>

<pre><code>cd spring-boot-elasticsearch/src/elastick8s
kubectl apply -f . 
</code></pre></li>
<li><p>Wait until elasticsearch is up and running.</p>

<pre><code>kubectl get pods
NAME                                 READY   STATUS    RESTARTS   
elasticsearch-5b74bbdd86-wgw2n       1/1     Running   0        
</code></pre></li>
<li><p>Deploy SpringBootApplication.</p>

<pre><code>cd spring-boot-elasticsearch/src/springbootk8s
kubectl apply -f . 
</code></pre></li>
<li><p>Get exposed URL of springboot and elasticsearch.</p>

<pre><code>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
</code></pre></li>
<li><p>Use postman to send request to SpringBootApplication, it will create an entry at elasticsearch. </p>

<p><a href="https://i.stack.imgur.com/rgx5a.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rgx5a.png" alt="3"></a></p></li>
<li><p>Check result, it works. </p>

<p><a href="https://i.stack.imgur.com/AFvfS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AFvfS.png" alt="4"></a>  </p></li>
</ul>
