<p>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.</p>
<p>Here are the details :
Environment - Windows 10 Professional, Docker Desktop, Java 8, Created Spring Boot app in Spring Tool Suite</p>
<ol>
<li>Spring Boot project
application.properties has following entry</li>
</ol>
<pre><code>server.port=8085

</code></pre>
<p>The controller contains following</p>
<pre><code>@RestController
public class TomcatController {

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

</code></pre>
<ol start="3">
<li>Used docker build command to create the image</li>
</ol>
<p>docker build -t sudhirj8/demowar1 .</p>
<ol start="4">
<li>Ran the docker run command and verified it works</li>
</ol>
<p>docker run --name demowar1 -p 8085:8085 sudhirj8/demowar1</p>
<ol start="5">
<li><p>In browser, used localhost:8085 and see proper output</p>
</li>
<li><p>Removed the running container</p>
</li>
<li><p>Ran following command to run in Kubernetes</p>
</li>
</ol>
<pre><code>kubectl run demowar1 --image sudhirj8/demowar1
</code></pre>
<p>Pod is created successfully
But when I run localhost:8085/hello browser, I get error</p>
<pre><code>Unable to connect
</code></pre>
<ol start="8">
<li>Ran command kubectl logs demowar1 and see that tomcat started on port 8085 properly</li>
</ol>
<pre><code> /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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)
</code></pre>

Sudhir Jangam
<p>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.</p>
<p><a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/service/</a></p>
<p>Alternatively, If you want to test only without exposing your app, you can do port-forward from your machine to pods, like below</p>
<pre><code>kubectl port-forward pods/demowar1 8085:8085
</code></pre>
<p>and then do <code>localhost:8085</code> on your machine.</p>
<p>Documentation for the same -&gt; <a href="https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/</a></p>
