Skuber is a Scala client library for Kubernetes. It provides a fully featured, high-level and strongly typed Scala API for managing Kubernetes cluster resources (such as Pods, Services, Deployments, ReplicaSets, Ingresses etc.) via the Kubernetes REST API server
The client supports v1.0, v1.1 and v1.2 of the Kubernetes API, so should work against all supported Kubernetes releases in use today.
There is currently no release version, so the following sections assume you have locally cloned this repository and installed the following (if not already installed):
- sbt version 0.13 or later
- Java 8
The quickest way to get started with Skuber:
-
If you don't already have Kubernetes installed, then follow the instructions here. This should establish a cluster listening for API requests on port 8080 on the local host, which is the configuration Skuber assumes by default.
-
Try one or more of the examples: run
sbt
in the skuber top-level directory to start sbt in interactive mode and then:
> project examples
> run
[warn] Multiple main classes detected. Run 'show discoveredMainClasses' to see the list
Multiple main classes detected, select one to run:
[1] skuber.examples.deployment.DeploymentExamples
[2] skuber.examples.fluent.FluentExamples
[3] skuber.examples.guestbook.Guestbook
[4] skuber.examples.scale.ScaleExamples
[5] skuber.examples.ingress.NginxIngress
Enter number:
For non-default setups, see the Configuration guide for details on how to tailor the configuration for your clusters security, namespace and connectivity requirements.
Although there isn't yet a release version of skuber, building the library from source is very straightforward. Simply run sbt test
in the root directory of the project to build the library (and examples) and run the unit tests to verify the build.
You can then import the library jar you just built into your project e.g. by copying it into your projects lib folder.
The code block below illustrates the simple steps required to create a replicated nginx service on a Kubernetes cluster that can be accessed by clients (inside outside the cluster) at a stable address.
It creates a Replication Controller that ensures five replicas (pods) of an nginx container are always running in the cluster, and exposes these to clients via a Service that automatically proxies any request received on port 30001 on any node of the cluster to port 80 of one of the currently running nginx replicas.
import skuber._
import skuber.json.format._
val nginxSelector = Map("app" -> "nginx")
val nginxContainer = Container("nginx",image="nginx").exposePort(80)
val nginxController= ReplicationController("nginx",nginxContainer,nginxSelector)
.withReplicas(5)
val nginxService = Service("nginx")
.withSelector(nginxSelector)
.exposeOnNodePort(30001 -> 80)
import scala.concurrent.ExecutionContext.Implicits.global
val k8s = k8sInit
val createOnK8s = for {
svc <- k8s create nginxService
rc <- k8s create nginxController
} yield (rc,svc)
createOnK8s onComplete {
case Success(_) => System.out.println("Successfully created nginx replication controller & service on Kubernetes cluster")
case Failure(ex) => System.err.println("Encountered exception trying to create resources on Kubernetes cluster: " + ex)
}
k8s.close
- Comprehensive Scala case class representations of Kubernetes kinds (e.g.
Pod
,Service
etc.) - Complete JSON support for reading and writing Kubernetes kinds.
- Support for Kubernetes object, list and simple kinds
- Support for
create
,get
,delete
,list
,update
, andwatch
methods on Kubernetes resources.- Asynchronous methods (i.e. the methods return their results in Futures)
- Each method is implemented by a corresponding HTTP request to the Kubernetes API server
- Strongly-typed API e.g.
k8s get[Deployment]("myDepl")
returns a value of typeFuture[Deployment]
- Support for full core Kubernetes API group
- Support for the extensions API group (e.g.
HorizontalPodAutoScaler
,Ingress
) - Fluent API for building / modifying the desired specification (
spec
) of a Kubernetes resource - Watching Kubernetes objects and kinds returns
Iteratees
for reactive processing of events from the cluster - Highly configurable via kubeconfig files or programmatically
- Full support for label selectors
- includes a mini-DSL for building expressions from both set-based and equality-based requirements.
See the programming guide for more details.
The coverage of the core Kubernetes API functionality by Skuber is extensive.
Support of more recent extensions group functionality is not yet entirely complete: full support (with examples) is included for Deployments, Horizontal pod autoscaling and Ingress / HTTP load balancing; support for other Extensions API group features including Daemon Sets and Jobs is expected shortly.
This code is licensed under the Apache V2.0 license, a copy of which is included here.