dsugden / skuber

A Scala Kubernetes client library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Skuber

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

Prerequisites

The client supports v1.0 through v1.3 of the Kubernetes API, so should work against all supported Kubernetes releases in use today.

You need Java 8 to run Skuber.

Release

You can include the current Skuber release in your application by adding the following to your sbt project:

resolvers += Resolver.url(
  "bintray-skuber",
  url("http://dl.bintray.com/oriordan/skuber"))(
  Resolver.ivyStylePatterns)

libraryDependencies += "io.doriordan" %% "skuber" % "1.3.0"

The current release has been built for Scala 2.11 - other Scala language versions such as 2.12 will be supported in future.

Building

Building the library from source is very straightforward. Simply run sbt testin the root directory of the project to build the library (and examples) and run the unit tests to verify the build.

Quick Start

The quickest way to get started with Skuber:

  • If you don't already have Kubernetes installed, then follow the instructions here to install minikube, which is now the recommended way to run Kubernetes locally.

  • Ensure Skuber configures itself from the default Kubeconfig file ($HOME/.kube/config) :

    export SKUBER_CONFIG=file

  • Try one or more of the examples: if you have cloned this repository run sbt in the 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 other Kubernetes setups, see the Configuration guide for details on how to tailor the configuration for your clusters security, namespace and connectivity requirements.

Example Usage

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 or 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

Features

  • Comprehensive Scala case class representations of Kubernetes kinds (e.g. Pod, Service etc.)
  • Reading and writing Kubernetes resources in the standardised Kubernetes JSON format
  • The API has create, get, delete, list, update, and watch operations on Kubernetes resources
    • These are asynchronous operations (i.e. the API operations return their results in Futures)
    • Each operations results in a HTTP request to the Kubernetes REST API server
    • Strongly-typed API e.g. k8s get[Deployment]("myDepl") returns a value of type Future[Deployment]
  • Support for the full core Kubernetes API group
  • Support for the extensions API group (e.g. HorizontalPodAutoScaler,Ingress)
  • Fluent API helps build or modify 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.

Status

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 added over time.

License

This code is licensed under the Apache V2.0 license, a copy of which is included here.

About

A Scala Kubernetes client library

License:Apache License 2.0


Languages

Language:Scala 100.0%