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
- 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());
}
}
- Dockerfile contains following
FROM openjdk:8
ADD target/demowar1.jar demowar1.jar
EXPOSE 8085
ENTRYPOINT ["java" , "-jar" , "demowar1.jar"]
- Used docker build command to create the image
docker build -t sudhirj8/demowar1 .
- Ran the docker run command and verified it works
docker run --name demowar1 -p 8085:8085 sudhirj8/demowar1
In browser, used localhost:8085 and see proper output
Removed the running container
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
- 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)