
You wanna try out Kubernetes (k8s) locally in your linux? Here is how:
This post combines Akkahttp, Docker and Kubernetes on your linux dev machine. You may use this setup for development and debug purposes.
What is Akkahttp, Docker and Kubernetes?
Akkahttp is made for building integration layers based on HTTP and as such tries to “stay on the sidelines”.
Docker is an open-source project that automates the deployment of linux applications inside software containers.
Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications.
akkahttp-playground is a simple project based on Akkahttp that exposes 2 endpoints to get & post data. Actually any web application that you can wrap into a docker image could be used here.
Using Docker as container format was a natural decision as there are so many resources on the web about docker for so many use cases and scenarios.
Kubernetes is a container cluster manager that allows you to run containers on different clouds including AWS , CoreOS and GCE and others.
Minikube allows you to run Kubernetes locally. Before you can follow the steps below, you need to make sure these tools and technologies exist on your linux:
Lets start up minikube in the directory of the akkahttp-playground clone:
minikube start
Now kubectl is configured to use the cluster.
You can run
minikube docker-env
to identify the IP address that sets up docker environment variables.
To set up the docker environment variables you run:
eval $(minikube docker-env)
The docker images that are created in the following steps shall be stored in a local docker registry – lets start one:
docker run -d -p 5000:5000 --name registry registry:2
Now its time to build the base docker image that is used for the akkahttp web app.
cd base/docker/java/
changes to the directory that contains the Dockerfile to build the java 8 base docker image:
docker build -t localhost:5000/java08:0.0.2 -f Dockerfile . && docker push localhost:5000/java08:0.0.2
The command above also pushes this image to the local docker registry.
The same approach is used for the scala/sbt docker image that is build on top of the java image:
cd ../scala/
docker build -t localhost:5000/scala:0.0.2 -f Dockerfile . && docker push localhost:5000/scala:0.0.2
You can go ahead to the project’s root directory
cd ../../..
Next steps would be to build the application, wrap it into a docker container and uploaded the image to the docker registry.
For that, there is the sbt-native-packager plugin that contains package formats including the docker plugin.
The build.sbt file contains the configuration to upload the docker image to the local registry.
sbt docker:publishLocal && sbt docker:publish
The akkahttp-playground docker container is now uploaded to the local private docker registry.
Now you should create user name & password used by minikube to pull the image:
docker login localhost:5000
should result in
Username (admin): [someuser] Password: [somepassword] Login Succeeded
Let’s tell minikube about those details:
kubectl create secret docker-registry myregistrykey --docker-server=localhost:5000 --docker-username=[someuser] --docker-password=[somepassword] --docker-email=[someemail]
kubectl get secrets
should show a respective item myregistrykey.
A kubernetes deployment works with pod config yaml files like this:
kubectl create -f pod-config.yaml
The status of the pod is available using the describe command:
kubectl describe pods/akkahttpplaygroundname
The output should be similar to
Name: akkahttpplaygroundname Namespace: default Node: minikube/192.168.99.100 Start Time: Thu, 13 Oct 2016 22:41:06 +0200 Labels: Status: Running IP: 172.17.0.5 ... Containers: akkahttpplayground: Container ID: docker://e638961e5dd9db76e30643523b20330afeac752ff76f9468f5cec6dfbd725275 Image: localhost:5000/akkahttp-playground:0.0.1 ... 26s 3s 3 {kubelet minikube} spec.containers{akkahttpplayground} Normal Pulling pulling image "localhost:5000/akkahttp-playground:0.0.1" 25s 3s 3 {kubelet minikube} spec.containers{akkahttpplayground} Normal Pulled Successfully pulled image "localhost:5000/akkahttp-playground:0.0.1" 3s 3s 1 {kubelet minikube} spec.containers{akkahttpplayground} Normal Created Created container with docker id 5ac511bf78d3; Security:[seccomp=unconfined] 3s 3s 1 {kubelet minikube} spec.containers{akkahttpplayground} Normal Started Started container with docker id 5ac511bf78d3
Similar to docker logs there is
kubectl logs akkahttpplaygroundname
The same output as with docker logs should be shown:
Server online at http://localhost:8181/hello Hit ENTER to stop...
However, in order to access the service you should run
curl -v http://192.168.99.100:8181/hello
and should get
... Connected to 192.168.99.100 (192.168.99.100) port 8181 (#0) > GET /hello HTTP/1.1 > User-Agent: curl/7.35.0 > Host: 192.168.99.100:8181 > Accept: */* > < HTTP/1.1 200 OK * Server akka-http/2.4.10 is not blacklisted < Server: akka-http/2.4.10 < Date: Tue, 18 Oct 2016 08:34:39 GMT < Content-Type: application/json < Content-Length: 16 < * Connection #0 to host 192.168.99.100 left intact {"msg":"my msg"}
Access the service via localhost would not work because the eval command (3rd step) at the beginning applies to the whole docker setup.
When you are done with debugging or developing you may don’t need the minikube anymore:
minikube delete
The step by step commands above described how to do use
- akkhttp
- docker
- kubernetes/minikube
on your local linux environment.
You can also debug using docker without kubernetes/minikube:
docker run -dit -p 8181:8181 --name akkahttp-playground localhost:5000/akkahttp-playground:0.0.1
curl http://localhost:8181/hello
should return something like
{"msg":"my msg"}
(That assumes you did not adapt the docker daemon using the eval command above)
In case you want to run the scala code only, you could use:
sbt "run-main info.lotharschulz.MyService"
Runing the scala tests would be:
sbt [clean] test
You can also upload the docker images to other docker registries. You’d adapt build.sbt e.g.
- https://github.com/lotharschulz/akkahttp-playground/blob/master/build.sbt#L34
- https://github.com/lotharschulz/akkahttp-playground/blob/master/build.sbt#L37
The docker base images described above can be also accessed via docker hub:
Finally I wanna thank Tobias, Raffaele and Oleksii for their tips regarding kubectl describe, logs and eval.
Leave a Reply