Create a Kubernetes Manifest Template Using Monokle

Aman Jaiswal
DevOps Learners
Published in
7 min readMar 18, 2022
Monokle

What is Monokle?

Monokle is an open-source tool by Kubeshop. It is MIT licensed. It’s a tool to inspect our Kubernetes manifests. Nowadays when we work with K8s, managing very long YAML files becomes a tough job as we get different issues. Monokle helps us to debug, manage and validate the changes before deploying them into the cluster. It also helps us to create our own manifest template. Some of the features of Monokle are:

  • Quickly get a high-level view of your manifests and their contained resources
  • Easily edit resources without having to learn or look up YAML syntax
  • Visualize and navigate resources in your clusters
  • Create a Resource from a Template
  • Manage multiple projects

You can go through the features of Monokle in more detail. ( kubeshop.github.io/monokle/features )

Installation of Monokle is quite easy. Go through the following document to install. (kubeshop.github.io/monokle/getting-started)

Using Template

Let’s see how can we use the templates to generate the k8s manifests file for our daily work.

  • Click on “Start from a template” option.
  • Give the name of the project and click on “Select a template” option. Here I have given monokle-demo in my case.
  • Now choose the template which you want to generate. Here in our case, we will choose a basic pod template. Click on “Use template” option.
  • These are the details for the template like The Author and the version of the plugin. Click on the Start option.
  • Now the UI will ask for pod details like the name of the pod, namespace, and the image which we want to use for the pod. Here in my case, I have given the pod name as demo-pod, namespace as none as I don’t want to use any namespace, and Nginx as image. You can use any image for your pod. Click on Submit option post that.

Note that if you won’t give the details, Monokle will use the default one.

  • UI will show the success message like this. Click on Done.
  • Yay, the template for the pod is generated. Now you can deploy this to the cluster by using the deploy option. We can also make the changes using the metadata option.

Creating Template

Now let’s see how can we create our own template plugin. Let’s create a GitHub repo for the same. I have created a repo named “monokle-template-pod-demo” in my GitHub and cloned it to my local machine. ( github.com/aj85430/monokle-template-pod-demo )

Create a package.json file for our template.

{
"name": "Templates plugins",
"description": "Custom templates plugin",
"version": "1.0.0",
"author": "Aman Jaiswal",
"repository": "https://github.com/aj85430/monokle-template-demo.git",
"monoklePlugin": {
"id":"com.github.aj85430.plugin.templates",
"helpUrl":"https://github.com/aj85430/monokle-templates-plugin",
"modules": [
{
"type": "template",
"path": "basic-pod-template"
}
]
}
}

Here we have provided the URL as a repository of the repo which we have created on Github. The field type will be the template inside the modules section in monoklePlugin and the path would be basic-pod-template. We can change the path name but note that whatever path name we will give here we have to use the same for the configuration. Create the “basic-pod-template” folder.

Monokle template contains 3 things:

  • a Monokle template configuration
  • a Form configuration
  • a Kubernetes YAML manifest with placeholders

Let’s create these 3 configurations.

Monokle template configuration

Create monokle-template.json file inside the basic-pod-template folder.

{
"name": "Basic Kubernetes Pod",
"id": "com.github.aj85430.plugin.templates.basic-pod-template",
"author": "Aman Jaiswal",
"version": "1.0.0",
"description": "Creates a Pod for a specified Image",
"repository": "",
"type": "vanilla",
"forms": [
{
"name": "Pod Settings",
"description": "Specify the settings for your Pod",
"schema": "form-schema.json",
"uiSchema": "form-ui-schema.json"
}
],
"manifests": [
{
"filePath": "template.yaml"
}
],
"resultMessage": "Pod resource created successfully!",
"helpUrl": "https://github.com/aj85430/monokle-templates-plugin"
}

monokle-template.json file is Monokle-specific configuration that defines what type of template we want and what we are creating. Basically, there are two types of templates in Monokle, vanilla, and helm-charts. We will use vanilla template for this tutorial. Here in this file, we can give the plugin name as I have given as Basic Kubernetes Pod. Same provide the author, version of the plugin, and description. The type would be vanilla. In forms section, there are two fields schema and uiSchema, these will be used by the Monokle to provide our users with the template with the input forms.

Form configuration

Create form-schema.json file inside the basic-pod-template folder.

{
"type": "object",
"required": [
"name",
"image"
],
"properties": {
"name": {
"type": "string",
"default": "kubernetes-pod"
},
"namespace": {
"type": "string"
},
"image": {
"type": "string"
}
}
}

form-schema.json file defines the basic fields that we will request from the user to create a Pod. As you can see the name and image field will be the required ones. All the properties of the pod like name, namespace, and the image are of types string. We can add more properties like ports, commands for the pod. The default option is used by Monokle when the user won’t provide any input. As here I have given the default pod name as kubernetes-pod.

Now let’s create form-ui-schema.json file inside the basic-pod-template folder.

{
"name": {
"ui:title": "Name",
"ui:help": "The name of the Pod"
},
"namespace": {
"ui:title": "Namespace",
"ui:help": "The target namespace for the Pod",
"ui:widget": "namespaceSelection"
},
"image": {
"ui:title": "Image",
"ui:help": "The image name to use for the Pod, for example nginx-ingress:latest"
}
}

form-ui-schema.json file defines how the form is going to appear. Here we can give any relevant name as the title and their description which will visible on UI to the users. Monokle uses the react-json-schema-form frontend component to render the forms for these templates. We are using a custom widget called namespaceSelection. This widget provides the user with a dropdown of available namespaces.

Kubernetes manifest with placeholders

Create a template.yaml file inside the basic-pod-template folder.

apiVersion: v1
kind: Pod
metadata:
name: [[forms[0].name]]
[[ forms[0].namespace ? " namespace: " + forms[0].namespace + "\n" : ""]]
spec:
containers:
- image: [[forms[0].image]]
name: [[forms[0].name]]
resources: {}

When the user will give the inputs for the template, all the details will be filed with the template in the YAML file. That is why we have created this template.yaml file. This will contain a pod template where all our inputs will be shown. We are using forms as an array because we can define multiple successive forms in our monokle-template.json, instead of having one huge form. In our case, we have only one form so we will use forms[0] to access it.

Note that the name of this template file should be the same as whatever we have given in the manifests section as filePath in monokle-template.json file.

Now our template is created. Just pushed all the files to your GitHub repo which we have created earlier. Note that we can also update and delete the template.

Importing Template

Now let’s see how can we import the template plugin which we have just created.

  • Go to the top-right side of the Monokle UI and click on the second icon which is plugin manager.
  • Click on the Install option.
  • Post the installation you can see your template plugin here.
  • The template plugin is imported and now we’re good to use this. Just try to use this template by following the “Using template” steps that we performed earlier in this tutorial. You can see your plugin when you’ll click on the “Select a template” option.

Conclusion

Similarly, we can create manifests and templates for job, service, deployment, etc, and deploy them to our clusters.

Just go through the Monokle documentation to learn and read more. (kubeshop.github.io/monokle) Monokle is a nice tool with a great flexible UI. It makes our lives easier and saves a lot of time. Thanks, Kubeshop for such a great tool.

Happy Templating:)

Note: If you are preparing for Kubernetes certification, check out the latest CKA Coupon to get discounts.

--

--