Name or Service not Known when calling service via spring boot in Openshift Container Platform (a cloud-based Kubernetes container platform)

5/8/2021

I have an application which consists of a backend (spring boot) and a search engine (elasticsearch). After I deployed it into OCP, Initially I tested the connection between the two using the command "curl" to elasticsearch service (https://service-name.namespace.svc.cluster.local:9200) from backend pod and it worked. Here's the picture: enter image description here

However, when I try to access elasticsearch from within the deployed backend application, an error message appears as below: enter image description here

And here are my configuration in Spring Boot to connect with Elasticsearch that I did:

package com.siolbca.config;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

@Configuration
@EnableElasticsearchRepositories(basePackages = "com.siolbca.repository")
@ComponentScan(basePackages = "com.siolbca.services")
public class Config {
	@Bean
    public RestHighLevelClient client() {
        ClientConfiguration clientConfiguration 
            = ClientConfiguration.builder()
                .connectedTo("https://elasticsearch-siol-es-http.siolbca-dev.svc.cluster.local:9200")
                .usingSsl()
                .withBasicAuth("elastic","G0D1g6TurJ79pcxr1065pU0U")
                .build();

        return RestClients.create(clientConfiguration).rest();
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchRestTemplate(client());
    }

}

Some of the things I have done are: 1. Use the elasticsearch service IP address directly in the backend configuration

https://elasticsearch-service-ipaddress:9200

  1. Expose a route from elasticsearch service and put it in the backend configuration

https://elasticsearch-route:443

  1. Change the service url into

https://service-name.namespace.svc:9200

Does anyone know why my backend app can't communicate with the elasticsearch service even though the two pods are able to connect? Any answer would be very helpful. Thank you.


EDIT

Here's my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.3</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.siolbca</groupId>
	<artifactId>siolbca</artifactId>
	<version>1.0</version>
	<name>siolbca</name>
	<description>Backend project for SIOLBCA</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		</dependency>
		<dependency>
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter-tomcat</artifactId>
 		</dependency>
 		<dependency>
		    <groupId>org.springframework.data</groupId>
		    <artifactId>spring-data-elasticsearch</artifactId>
		    <version>4.0.0.RELEASE</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>
-- Achmad Fathur Rizki
elasticsearch
java
kubernetes
openshift
spring-boot

1 Answer

5/8/2021

I think the elasticsearch builder .connectedTo method requires the format host:port, i.e. without the protocol. So try:

.connectedTo("elasticsearch-siol-es-http.siolbca-dev.svc.cluster.local:9200")
-- jhyot
Source: StackOverflow