DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Tomorrow’s Cloud Today: Unpacking the Future of Cloud Computing
  • Which Is Better for IoT: Azure RTOS or FreeRTOS?
  • RAML vs. OAS: Which Is the Best API Specification for Your Project?
  • What to Pay Attention to as Automation Upends the Developer Experience

Trending

  • Tomorrow’s Cloud Today: Unpacking the Future of Cloud Computing
  • Which Is Better for IoT: Azure RTOS or FreeRTOS?
  • RAML vs. OAS: Which Is the Best API Specification for Your Project?
  • What to Pay Attention to as Automation Upends the Developer Experience
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. DevOps and CI/CD
  4. Event-Driven Scripting on Your K8s Infrastructure With Brigade

Event-Driven Scripting on Your K8s Infrastructure With Brigade

Sudip Sengupta user avatar by
Sudip Sengupta
CORE ·
Aug. 13, 20 · Tutorial
Like (2)
Save
Tweet
Share
3.76K Views

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.
  • uses JavaScript, making a Brigade pipeline an application.
  • 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?

You can use Brigade to easily automate just about any process you can think of in your workflow using Javascript. If it can be run with Javascript, you can likely automate it with Brigade. Many users have automated things such as:

  • Batch processors.
  • Unit tests.
  • Slack notifications (Chat Ops).
  • Cron Jobs.
  • CI/CD.

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 vs scripting vs pipelines

  • Event-based: A script execution is triggered by a Brigade event.
  • Scripting: Programs are expressed as JavaScript files that declare one or more event handlers.
  • 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):

Shell
xxxxxxxxxx
1
 
1
$ helm status brigade-server

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:

Shell
xxxxxxxxxx
1
 
1
$ mkdir -p uuid-generator/app


Open the file uuid-generator/app/__init__.py and put the following Python code into it:

Python
xxxxxxxxxx
1
 
1
from flask import Flask, Response 
2
app = Flask(__name__) 
3
4
@app.route("/") 
5
def hello():    
6
  return "Hello World!"


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!

Shell
xxxxxxxxxx
1
 
1
$ echo "Flask" > requirements.txt 
2
$ pip install -r requirements.txt 
3
$ FLASK_APP=uuid-generator/app/__init__.py flask run 
4
  * Serving Flask app "app" 
5
  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)


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!”:

Hello World output

If you look back to the running flask app’s logs, you should see new logs pop up:

Plain Text
xxxxxxxxxx
1
 
1
127.0.0.1 - - [18/Aug/2017 12:56:16] "GET / HTTP/1.1" 200 -

Generating UUIDs

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:

Python
xxxxxxxxxx
1
 
1
from flask import Flask, Response 
2
import uuid 
3
4
app = Flask(__name__) 
5
6
@app.route("/") 
7
def hello():
8
    return Response(str(uuid.uuid4()), status=200, mimetype='text/plain')


Re-run the web server and open the browser back to http://127.0.0.1:5000/. You should now see a random UUID:

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:

  1. Open a new browser tab
  2. Navigate to https://github.com/
  3. 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.

Shell
xxxxxxxxxx
1
 
1
$ cd uuid-generator


Now, we’ll create a git repository and push the project to Github.

Shell
xxxxxxxxxx
1
 
1
$ git init 
2
$ echo -e "*.pyc\n*.egg-info" > .gitignore 
3
$ git commit -am "initial commit" 
4
$ git push origin master


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.

Configuring GitHub

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.

Webhook URL

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:

Shell
xxxxxxxxxx
1
10
 
1
$ brig project create 
2
? VCS or no-VCS project? VCS 
3
? Project Name bacongobbler/uuid-generator 
4
? Full repository name github.com/bacongobbler/uuid-generator 
5
? Clone URL (https://github.com/your/repo.git) https://github.com/bacongobbler/uuid-generator.git 
6
? Add secrets? No 
7
Auto-generated a Shared Secret: "mDXUDZyDsTUHw4KZIMPOQMN1" 
8
? Configure GitHub Access? No 
9
? Configure advanced options No 
10
Project ID: brigade-5ea0b3d7707afb5d04d55544485da6aff4f58006c1633f4ae0cb11


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:

Shell
xxxxxxxxxx
1
 
1
$ helm inspect values brigade/brigade > brigade-values.yaml 
2
$ # Add configuration under the `brigade-github-app` section 
3
$ helm upgrade brigade brigade/brigade -f brigade-values.yaml


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 brigade-github-app line:

Shell
xxxxxxxxxx
1
 
1
$ kubectl get service 
2
NAME                                TYPE           CLUSTER-IP     EXTERNAL-IP    PORT(S)        AGE 
3
brigade-server-brigade-api          ClusterIP      10.0.34.228    <none>         7745/TCP       1d 
4
brigade-server-brigade-github-app   LoadBalancer   10.0.69.248    10.21.77.9     80:31980/TCP   1d


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.

Final Thoughts

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!

Kubernetes app GitHub application Continuous Integration/Deployment Infrastructure Python (language) shell Pipeline (software)

Published at DZone with permission of Sudip Sengupta. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Tomorrow’s Cloud Today: Unpacking the Future of Cloud Computing
  • Which Is Better for IoT: Azure RTOS or FreeRTOS?
  • RAML vs. OAS: Which Is the Best API Specification for Your Project?
  • What to Pay Attention to as Automation Upends the Developer Experience

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: