This guide will provide you with the steps needed for deploying a simple Spring Boot application in a Docker container onto your Kubernetes Raspberry Pi cluster. The application we are going to deploy, is the one build in my previous post. The application is a very simple REST API, that we will deploy on our Kubernetes Raspberry Pi cluster. If you don't already have a Kubernetes cluster, visit this post.
First thing we need to be sure of is that Docker is up and running on your node. If you haven't please have a look at how to get up and running with HypriotOS and Docker.
The Raspberry Pi is build on the ARM architecture, which means that we cannot simply build our container on our host machines, that probably are running on the x86 or x64 architectures, and deploy them directly on our Raspberry Pis. This means we have to move how project to the Raspberry Pi, build the container and then deploy it onto our Kubernetes cluster.
Great, with that out of the way, let's get to it.
There are 2 ways of doing this, you could either do it by yourself or you could automate this with a build server. In this post we'll explore how to do it manually. However, later on we will publish a guide to setting up a build server with Jenkins. Stay tuned for that!
Go ahead and download the code that we will deploy onto our cluster here. Make sure the application works and is starting the tomcat server at port
To package our application into a Docker container, we need to create a
Dockerfile. A Dockerfile is simply a text document that contains a list of commands we would call on the commandline to assemble the image.
Dockerfile in the root of your applications directory containing the following
# Using rpi image with java 8 FROM hypriot/rpi-java # Build number hard-coded so far ADD target/spring-boot-rest-example-0.0.1-SNAPSHOT.jar app.jar EXPOSE 8080 CMD ["java", "-jar", "app.jar"]
Now we need to move our project to the Raspberry Pi and build the Docker container. There's a lot ways to do this, i use FileZilla, but you could use any SFTP-program or simply secure copy the files using
When on the Raspberry Pi, change directory to the root of your application directory and type in the following command.
docker build -t pieapi .
This will build the image and name it
pieapi. Pretty easy! Now we have packaged our application in a Docker container and are ready to see it run for the first time.
docker run -d -p 8080:8080 pieapi
Verify that everything works as expected by opening a browser and enter
<ip of the Raspberry Pi>:8080/pies, or
curl <ip of the Raspberry Pi>:8080/pies.
Fancy stuff, now our application is packaged and running in Docker. The last thing we need to do is to run it in Kubernetes.
Before we can deploy our application in Kubernetes we need to publish our container to the container registry at Docker Hub. Create a new repository at Docker hub and find the ID of your image with
$ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE pieapi latest b7768bb4fd9d 9 minutes ago 360.5 MB
next tag the container with the
latest-tag before pushing it to docker hub.
docker tag <IMAGE ID> <YOUR DOCKER HUB REPOSITORY>:latest docker push <YOUR DOCKER HUB REPOSITORY>
You can run a pod with a single container in Kubernetes as follows:
kubectl run example --image=<YOUR DOCKER HUB REPOSITORY>
To verify that everything still works check that your pod is running with the following command.
$ kubectl get pods NAME READY STATUS RESTARTS AGE example-sok9k 0/1 Running 0 3m
If you want to see it all visualized have a look at this post to set it up. Or go straight to our fork of the gcp-live-k8s-visualizer originally created by Brendand Burns. Our fork is based on@ArjenWassink and @saturnism awesome work.
To get things visualized, remember to add a label when running the pod as follows:
kubectl run example --image=rpicloud/spring-boot-rest-docker-example -l visualize=true,run=example
(Don't mind the red node, we had a minor fallout.)
That's it! As mentioned stay tuned for more on how to automate this process with a Jenkins Build Server.
The finished project can be downloaded from github here.
Happy hacking - over and out!