The adapter pattern

An adapter is a container in the app's Pod that makes the app look standard from outside; how it works, redis_exporter beside Redis serving Prometheus metrics, the YAML, its three jobs, and when not to use one.

GazeekKUBERNETES SIDECARS·EPISODE 5
EPISODE 5

The adapter pattern

An adapter is a container in the app's Pod that makes the app look standard from outside; how it works, redis_exporter beside Redis serving Prometheus metrics, the YAML, its three jobs, and when not to use one.

0:00 / 4:29

Diagrams in this video · 3Free in archhy

Copy and edit in archhy: How an adapter works (new tab)
Copy and edit in archhy: A concrete pair: Redis and its exporter (new tab)
Copy and edit in archhy: In the cluster (new tab)

Now try it on a real cluster

The videos are free. Gazeek's hands-on labs give you a real Kubernetes cluster in your browser for every lab, with checks that tell you when you've got it right: the practice the CKA and CKAD exams need.

After this lesson you can

  • Explain what an adapter container does and how it reads the app
  • Describe redis_exporter beside Redis for Prometheus
  • Name an adapter's three jobs, from metrics to log streaming
  • Tell an adapter from an ambassador, and know when to skip one
Transcript

The adapter pattern

Space Llama here. Last time, the ambassador simplified the outside world for the app. Today, its mirror image. The adapter pattern: a container that makes your app look standard from the outside.

Every app reports its own way

Monitoring and logging systems want every app to look the same: one metrics format, one place for logs. Apps aren't built that way. One speaks JMX, Java Management Extensions. Another speaks StatsD. Google got uniform metrics by code convention, which, as the paper notes, only works if you build your software from scratch. Most of us run software we didn't write. So put a translator beside it: the adapter.

How an adapter works

Here's the pattern. Three Pods on the left, each with an app and an adapter. Prometheus, a monitoring system, is on the right. Every app speaks its own way. A Java app speaks JMX, another app speaks StatsD, and Redis has its own stats. Beside each app, in the same Pod, sits an adapter. It reads the app over localhost, or through a shared volume. Each adapter translates. Different inputs, one output format. So outside, Prometheus sees one interface. It reads every Pod the same way, at slash metrics. And no app changed. The app image stays as it is. The Pod just gains a container.

A concrete pair: Redis and its exporter

Now a concrete pair: Redis and Prometheus. Redis reports stats in its own format, and Prometheus can't read it. Rewriting Redis? It's not your code. So you run the Redis exporter beside it. Its README even ships an example for deploying it as a sidecar to Redis. The exporter reads Redis over localhost, and Prometheus scrapes the exporter on port 9121, at slash metrics. Redis is unchanged, its metrics match every other app, and the exporter is always scheduled with Redis.

In the cluster

In the cluster, the Pod shows two faces. Clients reach Redis through a Service, on port 6379. Prometheus scrapes the exporter, on port 9121. Both ports belong to the same Pod IP address, so they must not clash.

The exporter, line by line

Here's the YAML: the Pod template from a Redis Deployment. The exporter, our adapter, goes in the init containers list. Restart policy Always makes it a native sidecar, with a fixed place in the start and stop order. It names one port, 9121. That's where Prometheus comes to scrape. Redis goes in containers, on port 6379. Nothing about it has changed. And notice what's missing: settings. The exporter's defaults already fit. It connects to Redis on localhost, port 6379.

Three jobs, and when not to

An adapter does three kinds of job. Transposing data, like an exporter. Streaming log files to standard out, one sidecar per file. And enriching data. When not to? If you own the code, add a Prometheus client library instead. Exporters are for software you can't instrument directly. For a single log file, write straight to standard out. And a full logging agent in every Pod can use significant resources.

For the CKAD

For the CKAD, the Certified Kubernetes Application Developer exam, the adapter was named up to version 1.21. Now it's one of the others. A typical task: an app writes to a shared volume, and you add a container that reshapes or streams it. Prove it with kubectl logs, dash c.

Remember

Let's recap. An adapter makes the app look standard from outside, reading it over localhost or a shared volume. Prometheus exporters are today's everyday adapters. And remember the mirror: the ambassador simplifies the world for the app; the adapter simplifies the app for the world.

Practice and what's next

The full article, with a log-streaming example, is on the Gazeek blog. Read more from the link below. Build an adapter on your own in the lab in Gazeek Labs, and try the practice questions. Next episode, the last in the series: service mesh sidecars versus Istio ambient mode. See you there.

Hands-on labs

Now try it on a real cluster

Pod patterns on your own: a real cluster built for you, checked as you go.

Open the lab

Gazeek Flash · free

Exam questions on your phone

Free questions on Application Design and Build: quick-fire batches and timed exams, on the web or Android.