Kubernetes

In this section we will learn how to deploy a local docker image to Kubernetes.

Requirements

  • An application you want to deploy ( we will use a simple akka-http server for demonstration )
  • A running kubernetes instance ( we will use minikube locally )

Web server

The akka-http webserver presents a simple hello world page on the index route /.

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer

object WebServer {
  def main(args: Array[String]) {

    implicit val system = ActorSystem("my-system")
    implicit val materializer = ActorMaterializer()
    // needed for the future flatMap/onComplete in the end
    implicit val executionContext = system.dispatcher

    val route =
      pathSingleSlash {
        get {
          complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
        }
      }

    // bind to 0.0.0.0 !
    Http().bindAndHandle(route, "0.0.0.0", 8080)

    println(s"Server online at http://localhost:0.0.0.0/\nPress RETURN to stop...")
  }
}

Minikube

We have minikube started and reusing the docker daemon from our minikube instance.

$ minikube start

Configuring sbt-native-packager

In your plugins.sbt add sbt-native-packager with

addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.1")

In your build.sbt configure the JavaAppPackaging plugin, which configures your build to generate a complete application with start scripts, configuration and library jars. We also set the docker specific setting dockerUpdateLatest to update a latest tag for our image.

// configure jvm application build
enablePlugins(JavaAppPackaging)
// add a username for the docker image
dockerAlias := dockerAlias.value.copy(username = Some("muki"))
// expose the docker port
dockerExposedPorts := Seq(8080)

Now we can publish a docker image to the minikube docker registry

$ eval $(minikube docker-env)
$ sbt docker:publishLocal

And start the image

$ kubectl run akka-http-webserver --image=muki/akka-http-webserver:1.0 --port=8080
$ kubectl expose deployment akka-http-webserver --type=NodePort
$ curl $(minikube service akka-http-webserver --url)

results matching ""

    No results matching ""