Need help with ksonnet-lib?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

455 Stars 87 Forks Apache License 2.0 265 Commits 22 Opened issues


Codegen for Kubernetes-API-compatible Jsonnet libraries

Services available


Need anything else?

Contributors list

ksonnet: Simplify working with Kubernetes

ksonnet (currently in beta testing) provides a simpler alternative to writing complex YAML for your Kubernetes configurations. Instead, you write template functions against the Kubernetes application API using the data templating language Jsonnet . Components called mixins also help simplify the work that's required to extend your configuration as your application scales up.

Jsonnet syntax highlighting

Other projects help simplify the work of writing a Kubernetes configuration by creating a simpler API that wraps the Kubernetes API. These projects include Kompose, OpenCompose, and compose2kube.

ksonnet instead streamlines the process of writing configurations that create native Kubernetes objects.


First, install Jsonnet.

Mac OS X

If you do not have Homebrew installed, install it now.

Then run:

brew install jsonnet


You must build the binary. For details, see the GitHub repository.


Fork or clone this repository, using a command such as:

git clone [email protected]:ksonnet/ksonnet-lib.git

Then add the appropriate import statements for the library to your Jsonnet code:

local k = import "ksonnet.beta.2/k.libsonnet";


statements look along a "search path" specified using
jsonnet -J 
. To use ksonnet, the search path should include the root of the
git repository. You should add additional
paths as you build up your own local libraries.

Jsonnet does not yet support ES2016-style imports, so it is common to "unpack" an import with a series of

local container = k.core.v1.container;
local deployment = k.extensions.v1beta1.deployment;


Developed in tandem with

, a static analysis toolset written as a Visual Studio Code plugin, meant to provide features such as autocomplete, syntax highlighting, and static analysis.

Get started

If you're not familiar with Jsonnet, check out the website and their tutorial. For usage, see the command line tool.

You can also start writing

files based on the examples in this readme. Then run the following command:
jsonnet -J /path/to/ksonnet-lib 

This command produces a JSON file that you can then run the appropriate

commands against, with the following syntax:
kubectl  - 

Write your config files with ksonnet

The YAML for the Kubernetes nginx hello world tutorial looks like this:

apiVersion: extensions/v1beta1
kind: Deployment
  name: nginx-deployment
  replicas: 2
        app: nginx
      - name: nginx
        image: nginx:1.7.9
        - containerPort: 80

Instead, you can write the following ksonnet code:

local k = import "ksonnet.beta.2/k.libsonnet";

// Specify the import objects that we need local container = k.extensions.v1beta1.deployment.mixin.spec.template.spec.containersType; local containerPort = container.portsType; local deployment = k.extensions.v1beta1.deployment;

local targetPort = 80; local podLabels = {app: "nginx"};

local nginxContainer ="nginx", "nginx:1.7.9") + container.ports(containerPort.containerPort(targetPort));

local nginxDeployment ="nginx-deployment", 2, nginxContainer, podLabels);

Save the file as

, then run:
jsonnet -J  helloworld.libsonnet > deployment.json

This command creates the

file that the ksonnet snippet defines.

You can now apply this deployment to your Kubernetes cluster by running the following command:

kubectl apply -f deployment.json

The ksonnet libraries

The ksonnet project organizes libraries by the level of abstraction they approach. For most users, the right entry point is:

  • ksonnet.beta.2/k.libsonnet
    : higher-level abstractions and methods to help create complex Kubernetes objects out of smaller objects

is built on top of a utility library,
, that is generated directly from the OpenAPI definition.


Mixins are a core feature of ksonnet. Conceptually, they provide dynamic inheritance, at runtime instead of compile time, which lets you combine them freely to modify objects or create new ones.

ksonnet ships with a large library of built-in mixins, or you can write your own custom mixins. The tutorial shows you how to create a custom mixin that you can then easily add as a Sidecar container to your Kubernetes cluster.


Thanks for taking the time to join our community and start contributing!

Before you start

  • Please familiarize yourself with the Code of Conduct before contributing.
  • See for instructions on the developer certificate of origin that we require.

Pull requests

  • We welcome pull requests. Feel free to dig through the issues and jump in.

Contact us

Have any questions or long-form feedback? You can always find us here:

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.