Kubernetes can't deploy simple image from Docker hub via Github

4/7/2019

I am really new on this kind of stuff, new on Kubernetes and Docker, but already have some experience on Java.

I tried using Docker Hub by connecting it to GitHub.

On my Git-hub there are only 2 codes:

  1. Dockerfile
  2. Simple hello world Java code.

Every time I run it on Kubernetes and check it with kubectl get pods, I always get CrashLoopBackOff.

I don't understand what's the problem, I already check the code and try to run it on Docker and it works, it prints out hello world. But not on Kubernetes.

This is the code on Dockerfile

FROM openjdk:8
COPY helloworld.java .
RUN javac helloworld.java
ENTRYPOINT ["java", "helloworld"]

This is the code on simple helloworld java

public class helloworld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

What I expected is: as I run this on Kubernetes I hope it says that it's ready and I can deploy it to an IP and show simple hello world.

-- newbielearner
docker
java
kubernetes

3 Answers

4/7/2019

You need to provide the location for copy, run and entrypoint commands. Prefix the filename with the path, say

COPY helloworld.java /helloworld.java RUN javac /helloworld.java ENTRYPOINT ["java", "/helloworld]

-- P Ekambaram
Source: StackOverflow

4/7/2019

Assuming some things too. Assuming that the image runs fine, and that the only issue is that a docker container gets shut down when there are no processes running, what you have to do is to force a process to be running for while.

So, you can do:

kubectl run hello-world --image YOUR-IMAGE sleep 100

This will create a deployment hello-world. A pod hello-world-xxxxxx-xxxx will run for 100 seconds, then it will die, and a new container will be create for another 100 seconds, and so on.

When you get the confirmation, you do:

kubectl get po

Copy the name of the pod, and get the logs:

kubectl logs YOUR-POD-NAME

And you should see the hello world.

-- suren
Source: StackOverflow

4/7/2019

Since you didn't specify how you executed it, I will assume you've been using kubectl run (by default creates a deployment) or a manifest defining a deployment. If so, then the CrashLoopBackOff is expected because deployments are for long-running processes. You Java code is not long-running. It prints something and then exits, that is, doesn't have an endless loop in there.

So either do the System.out.println in a loop (with a sleep inbetween?) or use a run command or a workload type (such as jobs) that is for one-off execution.

BTW, even with deployments, you should still be able to use kubectl logs to see the output from the first execution.

-- Michael Hausenblas
Source: StackOverflow