/hello-server

Demo showing how to deploy a Java application to Docker and Kubernetes

Primary LanguageJava

Hello Server

You will need Docker and Java JDK installed to run this sdemo.

  • OpenJDK is available here: https://www.azul.com/downloads/zulu.

    • Scroll down to Zulu CommunityTM Downloads
    • Enter Java version, your platform and processor size.
  • Docker for Desktop is an easy option for Windows o MAcOS and has a one-click option to run Kubernetes as well.

Run as an Application

  1. To build:

    Windows:   mvnw package
    Linux:     ./mvnw package
  2. Should create an executable jar in target/. Run it:

    java -jar target/hello-server-1.0.0.jar
  3. Then either

    • Open your browser to http://localhost:8080 - returns JSON, so best viewed using the JSON add-on for your browser
    • Or, in a second CMD/Terminal window invoke curl http://localhost:8080

    IMPORTANT: Stop the application once you are satisfied it is working to free up port 8080.

Run Using Docker

  1. You might like to view the Dockerfile first to see what it configures.

  2. Create a docker container (where docker-id is your Docker Hub id. If you don't have one, just use your first name):

    docker build -t <docker-id>/hello-server .
  3. Check it exists - it should be in this list:

    docker images
  4. To run:

    docker run --name hello-server -p8080:8080 <docker-id>/hello-server

    You should see Spring output to the console as it runs.

  5. Again access http://localhost:8080 to check it's working

    IMPORTANT: Hit CTRL-C to stop the container to free up port 8080.

Run Using Kubernetes

  1. To put your image up at Docker Hub, you need to run docker push <docker-id>/hello-server.

    • However if you don't have a docker hub account or upoloading the image is too slow, you can use one I created earlier paulc4/hello-server.
  2. Make sure kubectl is pointing at your Kubernetes cluster.

  3. Create a dedicated namespace kubectl create namespace hello

  4. Make it your default: kubectl config set-context <cluster-name> --namespace=hello

  5. If you wish to use the image you just pushed, edit hello-server.yaml and change paulc4 to your docker-id, assuming you called the image <docker-id>/hello-server.

  6. Deploy the web server: kubectl apply -f hello-server.yaml

  7. What have we deployed:

    kubectl get pods
    kubectl get deployments
  8. Go to Kubernetes dashboard, select hello namespace and view the pods.

  9. Expose the web-service: kubectl expose deployment hello-server --type=LoadBalancer --port=8080 --target-port=8080

  10. To see what IP-address was allocated to the application: kubectl get services -n paul. The output looks like:

    $ kubectl get services
    NAME                  CLUSTER-IP   EXTERNAL-IP         PORT(S)    AGE
    hello-server          10.3.245.61  104.155.111.170     8080:32452/TCP   2m

    The external IP may be a DNS name (for example if your cluster is on AWS).

  11. Access http://<external-ip>:8080 to check it's working

Help

Reference Documentation

For further reference, please consider the following sections:

Spring Guides

The following guides illustrate how to use some features concretely: