Walkthrough
Let's do something useful: generate some Kubernetes-bound YAML. We'll create the resource descriptors for a tiny Pod running nginx along with a Service.
We will recreate the hello-kerbi project from the examples folder. Read along or clone the folder to play with it locally. The directory structure by the end of the tutorial will be:
├───kerbifile.rb
├───pod-and-service.yaml.erb
├───consts.rb
├───helpers.rb
├───values
│ ├───values.yaml
│ └───production.yaml1. Basic Pod & Service
Starting simple, almost with static YAML, only interpolating release_name:
class HelloWorld < Kerbi::Mixer
def mix
push file("pod-and-service")
end
end
Kerbi::Globals.mixers << HelloWorldapiVersion: v1
kind: Pod
metadata:
name: "hello-kerbi"
namespace: <%= release_name %>
spec:
containers:
- name: main
image: nginx
---
apiVersion: v1
kind: Service
metadata:
name: "hello-kerbi"
namespace: <%= release_name %>
spec:
selector:
app: "hello-kerbi"
ports:
- port: 80A few Observations:
release_name gets its value - demo - from our command line argument
file("pod-and-service") omits the .yaml.erb extension and still works.
push ``file() is just passing an Array<Hash> returned by file(), explained here.
require ``"kerbi" is nowwhere to be found. That's normal, the kerbi executable handles it.
2. Adding Values
The whole point of templating engines is to modulate the output based on information passed at runtime. Like in Helm, the primary mechanism for this is values.
Running kerbi template default . yields the output you would expect. We can also choose to also apply our production.yaml by using -f flag in the command:
This makes our Service become a LoadBalancer:
Finally, we can use --set to achieve the same effect, but without creating a new values file:
3. Patching After Loading
Suppose we want to add labels and annotations to our Pod and Service. Because this will happen a lot, we can use patched_with to patch several resources in one shot, rather than per-resource.
Notice how patched_with() method accepts the same Hash | Array<Hash> as push() that we have been using up to now. This means you can use the same methods to construct arbitrarily complex patches.
4. Getting DRY & Organized
The great think about Kerbi is that it's just a normal Ruby program! You can do whatever makes sense for your project, such as DRYing up our ERB. We'll do three such things to inspire you:
Start using an outer namespace -
HelloKerbi- to prevent any name collisionsCreate a module to store constants -
HelloKerbi::ConstsCreate a helper module we use in our template files -
HelloKerbi::Helpers
5. Interactive Console
Another thing that sets Kerbi apart is the ability to touch your code. Using the kerbi console command, we'll open up an IRB session do what we please with our code:
6. State
You need a way to keep track of the values you use to generate your latest manifest. If you applied a templated manifest that used --set backend.image=2 and then later --set frontend.image=2, then the second invokation would revert backend.image to its default from values.yaml. Big problem.
Kerbi has an inbuilt state mechanism that lets you store the values it computes during $ kerbi template, and then retrieve those values again. Kerbi uses a ConfigMap (or Secret if you tell it to)in your cluster to store the data. Tell Kerbi to create that ConfigMap:
We now have one release:
6. Writing State
Now let's template again, but with a new option --write-state:
Let's use Kerbi's state inspection commands: list and show:
The meanings of special words like @candidate, @new-candidate, and @latest are covered in the State System guide.
7. Promoting the Candidate State
Now for the sake of realism, let's run kubectl apply -f manifest. That worked, so we feel good about these values. Let's promote our latest state:
The name of our state has changed:
8. Retrieving State
It's finally time to make use of the state we saved. Let's template the manifest again, with a new value assignment, but also with the old pod.image=ruby assignment:
We see in the manifest that the values from the old state (pod.image=ruby) were successfully applied which is what we wanted to do. Inspecting the state shows we have a new entry, as expected:
9. In Your CD Pipeline
Putting it all together, the following shows what a simple Kubernetes deployment script using Kerbi could look like.
10. Revisions
Work in progress.
The following is a preview. The command group $ kerbi revision does not work.
Last updated