0

I created a simple spring boot application and created a docker image. Tested the docker image on local (windows 10 professional) machine and it worked. Then I tried to run the image using kubectl run command, but it doesn't work.

Here are the details : Environment - Windows 10 Professional, Docker Desktop, Java 8, Created Spring Boot app in Spring Tool Suite

  1. Spring Boot project application.properties has following entry
server.port=8085

The controller contains following

@RestController
public class TomcatController {

       @GetMapping("/hello")
        public Collection<String> sayHello() {
            return IntStream.range(0, 10)
              .mapToObj(i -> "Hello number " + i)
              .collect(Collectors.toList());
        }
}
  1. Dockerfile contains following
FROM openjdk:8
ADD target/demowar1.jar demowar1.jar
EXPOSE 8085
ENTRYPOINT ["java" , "-jar" , "demowar1.jar"]

  1. Used docker build command to create the image

docker build -t sudhirj8/demowar1 .

  1. Ran the docker run command and verified it works

docker run --name demowar1 -p 8085:8085 sudhirj8/demowar1

  1. In browser, used localhost:8085 and see proper output

  2. Removed the running container

  3. Ran following command to run in Kubernetes

kubectl run demowar1 --image sudhirj8/demowar1

Pod is created successfully But when I run localhost:8085/hello browser, I get error

Unable to connect
  1. Ran command kubectl logs demowar1 and see that tomcat started on port 8085 properly
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.1)

2021-06-11 23:52:15.037  INFO 1 --- [           main] com.example.demo.Demowar1Application     : Starting Demowar1Application v0.0.1-SNAPSHOT using Java 1.8.0_292 on demowar1-pod with PID 1 (/demowar1.jar started by root in /)
2021-06-11 23:52:15.039  INFO 1 --- [           main] com.example.demo.Demowar1Application     : No active profile set, falling back to default profiles: default
2021-06-11 23:52:15.937  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8085 (http)
2021-06-11 23:52:15.948  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-06-11 23:52:15.948  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.46]
2021-06-11 23:52:15.999  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-06-11 23:52:15.999  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 915 ms
2021-06-11 23:52:16.491  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8085 (http) with context path ''
2021-06-11 23:52:16.499  INFO 1 --- [           main] com.example.demo.Demowar1Application     : Started Demowar1Application in 1.83 seconds (JVM running for 2.22)
1
  • The problem is you can't access the pod directly, you need to expose it as a service, is the Kubernetes cluster is local? Commented Jun 12, 2021 at 6:48

1 Answer 1

1

The Pod here can't be reached from your machine until it's exposed. You can do that by creating a service which directs your requests to the pods.

https://kubernetes.io/docs/concepts/services-networking/service/

Alternatively, If you want to test only without exposing your app, you can do port-forward from your machine to pods, like below

kubectl port-forward pods/demowar1 8085:8085

and then do localhost:8085 on your machine.

Documentation for the same -> https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.