RestTemplate exchange call gives 503 error when running in Docker container at Kubernetes environment

9/23/2016

My Spring Boot application runs fine when I run on the server as traditional way. But, when I create a Docker image with this app and run the container at Kubernetes environment, I get "503 Service Unavailable" error back when the app makes RestTemplate call.

Here is the code snipped where the call is made:

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

...

private boolean isActiveUserExist(String userId, String password) {

    String restUrl = "http://10.3.0.176:8888/persons/application_id";

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic "+ Utils.base64Creds(userId, password));
    HttpEntity<String> request = new HttpEntity<String>(headers);

    //construct the URL with Request Parameters parameters    
    try {
      restUrl = restUrl + "?userId=" + java.net.URLEncoder.encode(userId, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      log.error(e.getMessage());
      throw e;
    }
    restUrl = restUrl + "&applicationPk=1"; //primarykey for this app

    log.debug("Rest URL to check user <{}>.", restUrl);

    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<Person> response = null;

    try{

      response = restTemplate.exchange(restUrl, HttpMethod.GET, request, 
          new ParameterizedTypeReference<Person>() {
      });
    } catch (Exception e) {
      log.error("Rest URL to check user returns error {}.", e.getMessage());
      throw e;
    }

    Person person = response.getBody();
    log.debug("isActiveUserExist({}) returns: {}",userId,person);

    return  ( person != null ) ;
}    
...

Do you see any reason why RestTemplate.exchange call would not work and return 503 when it runs in the Docker Container at Kubernetes?

-- turgos
docker
kubernetes
resttemplate
spring-boot

0 Answers