<p>I am trying to run a kubernetes closer locally using minikube. This is my first try with kubernetes. Therefore
I am not familiar with all aspects of it. 
I am trying to deploy a spring boot app which connects to elastic search server.</p>

<p><strong>springboot deployment.yaml</strong></p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp
          image: myapp1:latest
          imagePullPolicy: Never
</code></pre>

<p><strong>Elastic search sever</strong> <strong>deployment.yaml</strong></p>

<pre><code>    apiVersion: apps/v1
kind: Deployment
metadata:
  name: elasticsearch
spec:
  selector:
    matchLabels:
      run: elasticsearch
  replicas: 1
  template:
    metadata:
      labels:
        run: elasticsearch
    spec:
      containers:
        - image: docker.elastic.co/elasticsearch/elasticsearch:6.6.1
          name: elasticsearch
          imagePullPolicy: IfNotPresent
          env:
            - name: discovery.type
              value: single-node
            - name: cluster.name
              value: elasticsearch
          ports:
          - containerPort: 9300
            name: nodes
          - containerPort: 9200
            name: client
</code></pre>

<p><strong>Exposed elastic search service</strong> as follows</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: elasticsearch
  labels:
    service: elasticsearch
spec:
  ports:
    - name: client
      port: 9200
      protocol: TCP
      targetPort: 9200
    - name: nodes
      port: 9300
      protocol: TCP
      targetPort: 9300
  type: NodePort

  selector:
    run: elasticsearch
</code></pre>

<p>Similarly, I exposed  service of springboot app also.
Now I am wondering how can I connect from springboot services to elastic search service.</p>

<p>When springbbot and elastic search was normal deployment on the same machine ( not in kubernetes), I connected using as </p>

<pre><code>RestClient.builder(new HttpHost("localhost", 9200))
                .build();
</code></pre>

<p>What's the best way to connect to the elastic search from springboot in kubernetes? </p>

<p>Save the ip of the elastic search service in an environment variable and use it in springboot or use the service name of the elastic search service?</p>

<p>Please advice</p>

<p>You should be able to get to the service, from within the cluster, using: </p>

<p><a href="http://servicename.servicenamespace:serviceport" rel="nofollow noreferrer">http://servicename.servicenamespace:serviceport</a></p>

<p>Kubernetes dns internal to the cluster will resolve the service name as a host name. If they are in the same namespace you probably don't need the serivcenamespace </p>

<p>Given the yaml above and if you used the default namespace for both elasticsearch and your myapp, then myapp process can connect via:</p>

<pre><code>http://elasticsearch:9200
</code></pre>
