Learn how your Kubernetes clusters respond to failure using Gremlin and Grafana
Tags: devops, kubernetes, postman, chaos
Published: 2019-11-18 09:05:58

---
title: Learn how your Kubernetes clusters respond to failure using Gremlin and Grafana
published: true
date: 2019-08-20 18:35:29 UTC
tags: devops,kubernetes,postman,chaos
canonical_url: https://medium.com/better-practices/chaos-d3ef238ec328
cover_image: https://cdn-images-1.medium.com/max/1024/1*Yeu19v1w20PpePBBPISaJQ.jpeg
description: Building resilient APIs with chaos engineering

---

**Building resilient APIs with chaos engineering.**

I don’t deal with failure well. But I’ve gotten better at it because I fail a lot. In fact, the more I fail, the better I anticipate all the ways I can possibly crash and burn and shore up those vulnerabilities.

This anticipation _could_ keep me up at night, but I’m stronger for it instead.

![](https://cdn-images-1.medium.com/max/1024/1*Yeu19v1w20PpePBBPISaJQ.jpeg)

On [Better Practices](https://medium.com/better-practices), we’ve previously talked about how working in the cloud is growing increasingly complex as teams continue to adopt microservice and distributed architectures. Some approaches are to [enable flexibility with an API-first approach](https://medium.com/better-practices/api-first-software-development-for-modern-organizations-fdbfba9a66d3) or to [increase velocity by using containers and an orchestration engine](https://dev.to/loopdelicious/deploying-a-scalable-web-application-with-docker-and-kubernetes-2p0k-temp-slug-5480716).

Another approach is with chaos engineering.

Distributed systems are inherently chaotic, and a number of organizations are embracing this chaos as they move to the cloud. They intentionally inject failure into their system in order to identify previously unknown vulnerabilities.

![](https://cdn-images-1.medium.com/max/1024/1*-z-zoW1bHmA6NOScjpdyeg.png)<figcaption>What happens when one of your dependencies fail?</figcaption>

What happens when one of your dependencies fail? By proactively controlling the conditions of a failure, you can learn from your system’s response on your own terms, instead of during an unanticipated outage.

### What is Chaos engineering?

When Netflix famously transitioned from a monolithic architecture over to a distributed cloud architecture, they worried about [how potential downtime would impact their users](https://medium.com/netflix-techblog/lessons-netflix-learned-from-the-aws-outage-deefe5fd0c04). The Netflix team introduced [Chaos Monkey](https://github.com/Netflix/chaosmonkey) to pseudo-randomly kill instances in order to simulate random server failure.

They wanted to make sure they could survive this type of failure and better understand the impact of such a failure on their customers. The goal was to fortify their distributed systems to expect and tolerate failure from other systems on which they depend.

[![](https://cdn-images-1.medium.com/max/400/0*5_pPW8QHUJ3CmnEi.jpg)](https://www.oreilly.com/ideas/chaos-engineering)

> [[Chaos engineering] incentivizes engineers to build their services to anticipate that some servers will suddenly go away… so they have to build their services to be redundant, highly available, and fault tolerant.](https://youtu.be/9MvcXO4L4Tk)

> [-Casey Rosenthal](https://twitter.com/caseyrosenthal?), CEO at [Verica](https://www.verica.io/)

Shortly thereafter, the Netflix team introduced a virtual [Simian Army](https://medium.com/netflix-techblog/the-netflix-simian-army-16e57fbab116), with each new Monkey tasked with inducing another type of failure — such as degrading a service or killing an entire region. Since then, the chaos community has developed [a number of tools](https://github.com/dastergon/awesome-chaos-engineering) to more precisely cause chaos in a controlled manner.

Today, there’s a number of companies who have embraced [the principles of chaos engineering](https://principlesofchaos.org/). Some teams might focus on failure injection testing or disaster recovery training, while adopting only the tools and practices that work for their specific goals.

Across all of these programs, a disciplined approach teaches them how to improve their systems so they can better tolerate future, _unplanned_ failures.

> Why worry about something that isn’t going to happen?

> - [HBO miniseries “Chernobyl”](https://www.hbo.com/chernobyl)

### Why look for trouble?

A chaos experiment is like running a fire drill. Imagine that you’ve already created an emergency protocol in case of a fire, sent it out to everyone, and you test the fire alarm once a month to make sure it’s functioning.

![](https://cdn-images-1.medium.com/max/1024/1*1N9OdZBqgzEgbBg0O6Mu3Q.jpeg)

Now what happens when you run the fire drill? You might discover that there’s a few rooms where the alarm can’t be heard, most people don’t remember where they’re supposed to meet, while others ignore the drill and stay at their desks.

By proactively testing how a system responds to failure conditions, you can identify and fix failures before they become public-facing outages. Failures in production are costly, so Chaos Engineering lets you validate what you think will happen with what is actually happening in your systems.

“Breaking things on purpose” in order to build more resilient systems prepares your team in the event of an earthquake, a zombie attack, or whatever else comes your way.

### Who is responsible for Chaos engineering?

In a microservice architecture, the engineers building the services frequently have responsibility for deployment and uptime. In an organization with a traditional DevOps team, those DevOps engineers may own these service levels. Some companies have specialized teams of Site Reliability Engineers (SRE) or Production Engineers (PE) tasked with continuous improvement and production support.

Initially, the people with the highest motivation to implement chaos engineering are those who feel the pain of a failure in production, like the ones on call.

> I started doing [chaos engineering] so I would get woken up less in the middle of the night and better understand my software.

> It boils down to who gets paged — if that’s an SRE or Ops team, they have the most incentive to start doing this work and making their lives better.

> — [Kolton Andrus](https://twitter.com/koltonandrus), CEO at [Gremlin](https://www.gremlin.com/)

Other people with a vested interest are the ones tasked with incident management or post-mortem analysis. The difference here is that incident management is a reactive process with steps taken to prevent reoccurrence. With chaos engineering, your experiments are conducted proactively by anticipating what _could_ go wrong.

![](https://cdn-images-1.medium.com/max/1024/1*0CaYOqTQndarPsYONHAjYQ.jpeg)

Right now, the vast majority of people implementing chaos engineering tend to be quality-driven and production-focused operations engineers.

However, it also makes sense to introduce the responsibility of resiliency earlier in the development cycle, when the cost of bugs is lowest, to reduce financial and other consequences in production. As such, there’s an emerging trend of testers who focus on production testing in addition to traditional pre-release testing.

So who owns Chaos?

- **Specialized roles ** — Site Reliability Engineers (SRE), Production Engineers (PE)
- **Functional teams**  — DevOps, Test and Quality Assurance (QA), Research and Development (R&D)
- **Domain knowledge experts**  — Traffic, database, data, storage

The initial owners of chaos engineering will be determined by your team’s current infrastructure, talent, and goals. Furthermore, this responsibility may shift as more organizations migrate to the cloud, and chaos engineering becomes more mainstream and begins to augment traditional testing.

### How can you start a chaos program?

You don’t need a special job title or even Netflix-level traffic to begin dabbling in chaos. The chaos community has developed a [number of shared resources](https://github.com/dastergon/awesome-chaos-engineering) to help advance this emerging discipline. Organizations like [Google](https://cloud.google.com/solutions/dr-scenarios-planning-guide), [Twilio](https://www.twilio.com/blog/2017/11/chaos-engineering-ratequeue-ha.html), [PagerDuty](https://www.pagerduty.com/blog/chaoscat-automating-fault-injection/), and many more have adopted their own approaches to chaos engineering.

In some cases, it’s a curious tester who kicks off a single chaos experiment after notifying the rest of her team. Maybe it’s a handful of engineers huddled together to plan their failures during a [gameday](https://aws.amazon.com/gameday/). Or it’s a directive from senior management to scope out a chaos program after one particularly costly outage.

![](https://cdn-images-1.medium.com/max/1024/1*bwtXA4A_ntUSEGHKtLaMZA.jpeg)

Once again, the implementation you choose will be determined by your team’s current infrastructure, talent, and goals.

> [Perhaps aggregate bits and pieces from different [resilience engineering] frameworks that appeal to you, and then create a practice around it. You’ll likely be the first person to create a similar practice in your particular context.](https://www.linkedin.com/pulse/would-chaos-any-othername-casey-rosenthal/)

> [I wish the best of luck to you in that undertaking, but I wouldn’t wager that you get it right on your first try. Or your second.](https://www.linkedin.com/pulse/would-chaos-any-othername-casey-rosenthal/)

> -[Casey Rosenthal](https://twitter.com/caseyrosenthal?), CEO at [Verica](https://www.verica.io/)

A common pitfall is not clearly communicating the reasons why you are adopting chaos engineering practices in the first place. When the rest of the organization doesn’t yet understand or believe in the benefit, there’s going to be fear and confusion. Worse yet, there’s the perception that you’re just breaking stuff randomly and without a legitimate hypothesis.

### How do you run a chaos experiment?

While there’s many ways to run your chaos experiments, most processes echo the scientific method with a continuous cycle of hypothesis and experimentation.

![](https://cdn-images-1.medium.com/max/1024/1*wdrHw9NmGHE38TkaeiOMJw.png)<figcaption>Make improvements, and automate the experiments to run continuously</figcaption>

**Plan an experiment.** First, create a hypothesis about the steady-state behavior of your systems. Focus on if your systems _do_ work, not _how_ they work. Then think about what could go wrong. Perhaps a server goes down (as they do). Maybe it’s your third-party payment system, a specific cluster, or an entire region that’s experiencing an outage.

**Start small**. Initiate an attack that’s small enough to give you information about how your systems react in a production environment. You can’t always predict how users will behave. Instead of testing code in an isolated environment, [test a complete system in production](https://techbeacon.com/app-dev-testing/test-production-yes-you-can-you-should) comprised of users, code, environment, infrastructure, a singular point in time, and more.

**Measure the impact**. Observe the impact of your attack by comparing it to your steady-state metrics to reveal any known and unknown issues. At this point, you can either turn up the juice on your attack or roll it back if there were unintended and potentially harmful side effects.

**Learn more about your systems.** Validate or update your hypothesis, and shore up your vulnerabilities. Make these improvements, and then be prepared to automate your experiments to run continuously.

![](https://cdn-images-1.medium.com/max/1024/1*EuRFRJQ0iqf216oYYIyr_w.png)

Just like a fire drill, your team is developing a muscle memory by practicing how to respond when the stakes are controlled, instead of practicing during an actual emergency. By methodically performing these automated tests, you can learn more about your systems’ real-world behavior.

If you’re just getting your feet wet with chaos engineering, consider starting with your APIs. We’ve previously talked about using APIs [to create additional load](https://dev.to/loopdelicious/don-t-get-techcrunched-performance-testing-for-your-http-apis-396a-temp-slug-6713183) or [security injections and other user behavior](https://dev.to/loopdelicious/reverse-engineering-an-api-3e3-temp-slug-4060600). Now you can test your fallbacks by simulating the outage of external third-party APIs or killing your own internal APIs.

> [When a company measures their critical services, APIs are often considered second-class citizens.](https://www.gremlin.com/blog/making-your-apis-more-resilient-with-gremlin/)

> [But APIs are a core part of an organization’s infrastructure, and not understanding their weaknesses can lead to performance issues and downtime.](https://www.gremlin.com/blog/making-your-apis-more-resilient-with-gremlin/)

> - [Tammy Butow](https://twitter.com/tammybutow), Principal Site Reliability Engineer at [Gremlin](https://www.gremlin.com/)

One way to experience an API outage is by simply unplugging a server. But what if the servers aren’t yours, or they’re hard to reach? Or you realize you’ve unplugged the wrong machine? Oops. You can’t just plug it back in and expect service to resume right away. **Don’t do that**.

Another way to experience an API outage is by using [a Postman mock](https://learning.getpostman.com/docs/postman/mock_servers/intro_to_mock_servers) to return a 500 internal server error. This works, but it’s still just a simulation. While we can simulate outages in a test environment, there’s only one way to capture the unquantifiable conditions that cannot be replicated in an isolated test environment.

Eventually we want to break stuff in production 😈

### A Postman recipe for creating chaos with Amazon EKS and Gremlin

![](https://cdn-images-1.medium.com/max/1024/1*17uoCPDXzuaT3pLtLaZKnw.png)

Let’s start with an example e-commerce app where users can browse items, add them to the cart, and purchase them. Then we’ll shut down a container and see what hijinks ensue.

- **Trigger:** [Gremlin](https://www.gremlin.com/) is a failure-as-a-service and offers a free version with limited attack types. We’ll be using Postman with the Gremlin API to trigger our attacks. _Spoiler alert_ . . . this is so we can easily automate these chaos tests with our continuous integration pipeline.
- **Target:** We’ve previously talked about [deploying scalable apps with Docker and Kubernetes](https://dev.to/loopdelicious/deploying-a-scalable-web-application-with-docker-and-kubernetes-2p0k-temp-slug-5480716). [Amazon EKS](https://aws.amazon.com/eks/) is a managed Kubernetes service that runs on AWS. It ain’t cheap. If you’re already running on hosts, containers, or another cloud platform, swap out EKS with your own target.
- **Observability:** We’ll use [Prometheus](https://prometheus.io/) as our time-series database and [Grafana](https://grafana.com/) to visualize the effects of our attacks. Both are open-source and have a free version. If you’re already using something else for steady-state monitoring, swap out Prometheus and Grafana with your own toolset.

![](https://cdn-images-1.medium.com/max/1024/1*80dJwHJuggQp4G3W_Oh31A.png)<figcaption>a Postman recipe for creating chaos</figcaption>

#### Set up Gremlin and create a Kubernetes cluster on EKS

If you want to jump to the end, go ahead and[clone this example](https://github.com/postmanlabs/kubernetes-chaos). Otherwise, let’s begin with [this helpful guide to install Gremlin to use with Amazon EKS](https://www.gremlin.com/community/tutorials/how-to-install-and-use-gremlin-with-eks/). You’ll need an AWS account, the AWS CLI configured to use eksctl to create the EKS cluster, and a Gremlin account.

- Step 0 — [Verify your account AWS CLI Installation](https://www.gremlin.com/community/tutorials/how-to-install-and-use-gremlin-with-eks/#step-0---verify-your-account-aws-cli-installation)
- Step 1 — [Create an EKS cluster using eksctl](https://www.gremlin.com/community/tutorials/how-to-install-and-use-gremlin-with-eks/#step-1---create-an-eks-cluster-using-eksctl)
- Step 2 — [Load up the kubeconfig for the cluster](https://www.gremlin.com/community/tutorials/how-to-install-and-use-gremlin-with-eks/#step-2---load-up-the-kubeconfig-for-the-cluster)
- Step 3 — [Deploy Kubernetes Dashboard](https://www.gremlin.com/community/tutorials/how-to-install-and-use-gremlin-with-eks/#step-2---deploy-kubernetes-dashboard)
- Step 4 — [Deploy a Microservice Demo application](https://www.gremlin.com/community/tutorials/how-to-install-and-use-gremlin-with-eks/#step-4---deploy-a-microservice-demo-application)
- Step 5 — [Run a Shutdown Container Attack using Gremlin](https://www.gremlin.com/community/tutorials/how-to-install-and-use-gremlin-with-eks/#step-5---run-a-shutdown-container-attack-using-gremlin) (skip)

After that, you should have a sample app deployed on AWS EKS and Gremlin running on your [Kubernetes dashboard](https://github.com/kubernetes/dashboard).

![](https://cdn-images-1.medium.com/max/1024/1*bDCR1rpyKYAq5BPATleIGQ.png)

![](https://cdn-images-1.medium.com/max/1024/1*JczM0UeTBIDNk3BDKuacOQ.png)<figcaption>1) Sample app deployed in EKS, and 2) Gremlin installed using Kubernetes Dashboard</figcaption>

#### Set up Grafana and Prometheus

If you’re still with me, let’s set up [Monitoring Kubernetes Clusters with Grafana](https://medium.com/htc-research-engineering-blog/monitoring-kubernetes-clusters-with-grafana-e2a413febefd). This particular guide starts with Google Kubernetes Engine (GKE) instead of EKS, but most of the remaining steps are the same after you create your cluster.

- Step 0 — Create a GKE cluster (skip)
- Step 1 — Lots and lots and lots of yaml configuration
- Step 2 — Configure your cluster settings on Grafana (skip)

Instead of configuring your cluster settings on Grafana, you can simply import an existing dashboard if your monitoring tools are running on your cluster. Check out the README in[the sample repo](https://github.com/postmanlabs/kubernetes-chaos) for more details on how to manage external access to the apps running inside the cluster.

After that, you should be able to observe the steady-state metrics of all the nodes in your cluster.

![](https://cdn-images-1.medium.com/max/1024/1*5D-3PXllhDYbXKUk2uVISQ.png)

![](https://cdn-images-1.medium.com/max/1024/1*CCFCxwesY3vRLlHHg4jS7g.png)<figcaption>1) Add a datasource of Prometheus type, and 2) import dashboard 3131 for an overview of all nodes in your cluster</figcaption>

#### Programmatically manage your chaos experiments

A [Kubernetes pod](https://kubernetes.io/docs/concepts/workloads/pods/pod/) is composed of one or more containers that share a network stack. If we attack a single container within a pod, the impact should be observed for all containers collocated within that pod. Optionally, we can further specify container ports to restrict the impact of our attack.

In the last step of the Gremlin tutorial, we shut down a container using Gremlin’s UI. We could also use an API to run our attacks so that we can programmatically manage our chaos experiments.

Let’s use Postman to shut down a single container via the [Gremlin API](https://www.gremlin.com/docs/api-reference/overview/).

In the Postman app, [import the template](https://learning.getpostman.com/docs/postman/launching_postman/newbutton/#templates) Chaos engineering which includes a corresponding environment chaosEngineering. Check out the [Chaos engineering documentation](https://documenter.getpostman.com/view/1559645/SVfH1sqD) for step-by-step instructions.

[![](https://cdn-images-1.medium.com/max/330/1*wN3EP_GfXrJ60CT36K3jPA.png)](https://app.getpostman.com/run-collection/b07acbdc8e6f2d2b59f8#?env%5BchaosEngineering%5D=W3sia2V5IjoibW9ja191cmwiLCJ2YWx1ZSI6Imh0dHBzOi8vYmQwMTVmMWUtNjQwMS00MzdkLTliYjYtYTk4NDg4MmVlMzNlLm1vY2sucHN0bW4uaW8iLCJlbmFibGVkIjp0cnVlfSx7ImtleSI6ImdyZW1saW5fYXBpIiwidmFsdWUiOiJodHRwczovL2FwaS5ncmVtbGluLmNvbS92MSIsImVuYWJsZWQiOnRydWV9LHsia2V5IjoiZ3JlbWxpbl9hcGlfa2V5IiwidmFsdWUiOiJ5b3VyLWdyZW1saW4tYXBpLWtleSIsImVuYWJsZWQiOnRydWV9LHsia2V5IjoieW91cl9kZXBsb3llZF9hcHBfVVJMIiwidmFsdWUiOiJ5b3VyLWRlcGxveWVkLWFwcC11cmwiLCJlbmFibGVkIjp0cnVlfV0=)<figcaption>Click this enticing button to import the template</figcaption>

You’ll first need [to update the Postman environment](https://learning.getpostman.com/docs/postman/environments_and_globals/manage_environments/#editing-an-active-environment) with your `gremlin_api_key` and `your_deployed_app_url`. Then look for the folder called Shut down a container.

1. Get a list of our active containers
2. Shut down a specified container
3. Verify the health of our app
4. Stop the attack (if you need to)

Our hypothesis is that if we shut down the container running cartservice, EKS will give us a new one, and we won’t suffer any downtime. However, if we experience unintended effects, let’s be prepared to stop the attack.

![](https://cdn-images-1.medium.com/max/1024/1*jh3Qb2fo7KHKsDtwcCdWbw.png)

![](https://cdn-images-1.medium.com/max/1024/1*02i86pPFsxs3kCgFyfxH4Q.png)<figcaption>1) Shut down a container using the Gremlin API, and 2) see the effects in your browser</figcaption>

Yikes! When we shut down the cartservice container, we see a 500 error on our shopping cart page. We see this in our browser and also in Postman if we make a GET request to our deployed app’s URL.

If you completed the last step of the Gremlin tutorial, you already know this is because cartservice [uses Redis](https://github.com/GoogleCloudPlatform/microservices-demo/blob/master/kubernetes-manifests/cartservice.yaml) instead of Redis Cluster. Now that we have revealed this vulnerability, we can have a broader discussion with other team members about your data solution.

In the meantime, let’s make a DELETE request using the Gremlin API to stop our attack, and add some code to Postman to _only_ stop our attack if we see a 500 internal server error.

![](https://cdn-images-1.medium.com/max/1024/1*Vkhq2tkRf81EnJaTPl-SVQ.png)

![](https://cdn-images-1.medium.com/max/1024/1*E44EAGKik4Bzd-Nmqrru9A.png)<figcaption>1) Stop the attack if we see a 500 internal server error in Postman, and 2) Run our chaos tests automatically in the collection runner</figcaption>

In this manner, we can use Postman and the Gremlin API to explore what happens when we break a specific part of our system. Beyond manual testing, however, we’ve also seen how to automatically run our integration tests [using the collection runner](https://learning.getpostman.com/docs/postman/collection_runs/starting_a_collection_run) and a little bit of code.

> 🎓 **Advanced challenge:** now that we know how to use the Postman app to manage our chaos experiments, how can we can automate chaos testing with our continuous integration (CI) pipeline or continuous testing process? Here’s [a clue](https://gph.is/1sFFzLq). And here’s [a better clue](https://medium.com/better-practices/deploying-a-scalable-web-application-with-docker-and-kubernetes-a5000a06c4e9#ed88).

### A final thought about Chaos Engineering

**So what happens when one of your dependencies fail?** A valuable chaos test will not only teach you about your systems, but also your people. The human response is frequently one of the more difficult aspects to test in a proactive and systematic fashion. It’s probably easier to test the impact of a server crash.

When chaos experiments impact production traffic and users in real life, other teams will be impacted and hopefully notified before any experiments begin. Your team’s ability to communicate and collaborate will inevitably impact the resiliency of your systems.

![](https://cdn-images-1.medium.com/max/1024/1*oGoUT4CZ9CQlQOi5TfCsZw.jpeg)

> The biggest limitation in the fear of delivering software faster is the focus on adding more pre-release testing.

> Chaos engineering is all about building trust in our resiliency and mean time to recovery. In time, we have less fear that any one change will bring down our products and when issues do occur, we are practiced in triaging and deploying fixes faster, building confidence that we aren’t fragile.

> - [Abby Bangser](https://twitter.com/a_bangser), Platform Test Engineer at [MOO](https://www.moo.com)

For many teams, chaos engineering will require a mental shift in how failures are perceived in the organization. It’s great to identify a failure and bring it to the attention of the rest of your team. However, it’s even better to create a process that anticipates potential failures, and then reveals new information to help shore up your vulnerabilities.

Chaos engineering is not just about building more resilient software, but also building a culture of resiliency within an organization. A team that truly celebrates failures, instead of hiding them, will enable the broader organization to learn and grow stronger from these experiences.

* * *