Event-Driven Scripting on Your K8s Infrastructure With Brigade
Event-Driven Scripting on Your K8s Infrastructure With Brigade
By leveraging Brigade, users are able to string multiple tasks together and execute them inside containers.
Join the DZone community and get the full member experience.Join For Free
What Is Brigade?
Brigade is a tool from the team that brought you Helm and Draft, which are designed to assist developers and operations teams to accomplish more work with event-driven scripting. The tool enables teams to build out any ordered workflow of containers in Kubernetes and trigger the workflow by listening for arbitrary events.
Using Kubernetes allows users to not only manage long-standing services, but also run quick short-term tasks that are commonly used for things like event-driven programming, code quality testing, and batch processing.
By leveraging Brigade, users are able to string multiple tasks together and execute them inside containers. It follows the same patterns found in other forms of serverless computing, where a Brigade pipeline is triggered by an event.
At first glance, Brigade:
- is Kubernetes-native, with one-line installation.
- supports projects and a customizable event model.
- enables arbitrarily complex (parallel or synchronous) pipelines.
- executes functions in containers.
- fully supports Kubernetes volumes, pods, role-based access control (RBAC), and secrets.
What Can I Build With Brigade?
- Batch processors.
- Unit tests.
- Slack notifications (Chat Ops).
- Cron Jobs.
How it Works
Brigade is an in-cluster runtime environment. It interprets scripts and executes them often by invoking resources inside of the cluster. Brigade is event-based scripting of Kubernetes pipelines.
- Event-based: A script execution is triggered by a Brigade event.
- Pipelines: A script expresses a series of related jobs that run-in parallel or serially.
Setting Up a CI Pipeline With Brigade
Throughout this tutorial, we’ll walk you through the creation of a basic web application with a Brigade CI pipeline for testing the application.
It’ll consist of two parts:
- A public site that lets people generate UUIDs.
- A brigade.js that tests the site.
We’ll assume you have Brigade, Git (a version control system), and pip (a package management system for Python) installed already.
You can tell if Brigade is installed and its version by running the following command in a shell prompt (indicated by the $ prefix):
Creating Your First Application
For this tutorial, we’ll be creating an example application written in Python, which uses Flask to provide a very simple UUID generator web server. Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions. (Note that this application is written for Python 3, specifically.)
Let’s write the web server. To create your app, type this command:
Open the file
uuid-generator/app/__init__.py and put the following Python code into it:
This is the simplest application possible in Flask. To call
hello(), we need to map it to a URL - in this case we want to map it to the root path (
Start the Development Server
Now that we’ve written the app, let’s run it!
Now, open a Web browser and go to “/” on your local domain – e.g., http://127.0.0.1:5000/. You should see “Hello World!”:
If you look back to the running flask app’s logs, you should see new logs pop up:
Let’s make the application generate a random UUID on every request. Edit the
uuid-generator/app/__init__.py file so that it looks like this:
Re-run the web server and open the browser back to http://127.0.0.1:5000/. You should now see a random UUID:
Keep refreshing the page. You should see new UUIDs being generated every time you refresh the page.
Create a Github Account
To use Github, you will need a Github account.
In your own browser:
- Open a new browser tab
- Navigate to https://github.com/
- Create an account
If you already have Github account, continue to the next exercise.
After you sign up, you will receive a verification e-mail. Be sure to verify your e-mail address to Github by following the instructions in that e-mail.
Initialize Your Repository
Now that you have a Github account and a working application, let’s create a git repository and push it up to Github!
Let’s first change working directories over to the Flask app we made in Tutorial 1.
Now, we’ll create a git repository and push the project to Github.
We created a .gitignore file so all compiled python files and python eggs (noted with the .pyc and .egg-info extensions, respectively) are not added to source control.
We’ll now walk through the process for configuring your newly created Github repository with Brigade for testing new features. We’ll configure a new Brigade project, and have Github push events to trigger Brigade builds.
We want to build our project each time a new commit is pushed to master, and each time we get a new Pull Request.
To do this, follow the Brigade GitHub App documentation to set up a GitHub App. During configuration, copy the shared secret above (
mDXUDZyDsTUHw4KZIMPOQMN1) and set this as the “Webhook secret” value for the App.
Create a Brigade Project
The Brigade server tracks separate configuration for each project you set up. To create and manage these configurations, we use the brig cli.
Here we create a project for our GitHub repo:
Note: To explore the advanced options, each prompt offers further info when
? is entered.
We’ll need to upgrade our Brigade server with our
brigade-github-app sub-chart configuration filled in:
We can then get the IP needed to update the “Webhook URL” entry for our App. Run this command on your Kubernetes cluster, and look for the
You will use the
EXTERNAL-IP address to form the full URL:
http://10.21.77.9/events/github. Update the App’s “Webhook URL” with this value. (Note: it is preferred that DNS be set up instead of a hard-coded IP. See the Brigade GitHub App docs for more.)
The next time you push to the repository, the webhook system should trigger a build.
Using Brigade can add a really useful tool for those trying to implement Kubernetes-native automation into their pipelines and processes. I hope this tutorial helped you learn something new in terms of automating your infrastructure, and I hope you have a chance to read more of my posts in the future. Thanks for reading!
Published at DZone with permission of Sudip Sengupta . See the original article here.
Opinions expressed by DZone contributors are their own.