K8s by Example: Ambassador Pattern
| The ambassador pattern uses a proxy container to broker connections between your application and external services. The app connects to localhost; the ambassador handles service discovery, sharding, load balancing, or protocol translation. Use for: database sharding, service brokering, A/B testing, circuit breaking. |
| ambassador-redis-shard.yaml | |
| A Redis sharding ambassador. The app connects to localhost:6379 thinking it’s a single Redis instance. The twemproxy ambassador distributes keys across multiple Redis shards automatically. | |
| The twemproxy ambassador listens on localhost:6379 and routes requests to the appropriate Redis shard based on key hashing. The app code doesn’t need sharding logic. | |
| twemproxy-config.yaml | |
| Twemproxy configuration defines the sharding strategy. | |
| ambassador-service-broker.yaml | |
| A service broker ambassador handles service discovery across environments. In dev, it connects to a local database; in prod, to a managed cloud service. The app always connects to localhost. | |
| ambassador-experiment.yaml | |
| An A/B testing ambassador splits traffic between production and experimental backends. 90% goes to prod, 10% to experiment. IP hashing ensures user session consistency. | |
| ambassador-circuit-breaker.yaml | |
| A circuit breaker ambassador (Envoy) protects the app from cascading failures. If the backend fails repeatedly, the circuit opens and returns errors immediately without waiting. | |
| terminal | |
| Debug ambassadors by checking their logs and connectivity. The app sees only localhost; the ambassador handles the complexity of multiple backends, failover, and load distribution. | |