2015-06-07 23:07:20 -04:00
|
|
|
<!--[metadata]>
|
|
|
|
+++
|
2016-01-23 23:36:40 -05:00
|
|
|
aliases = ["/engine/articles/ambassador_pattern_linking/"]
|
2015-06-07 23:07:20 -04:00
|
|
|
title = "Link via an ambassador container"
|
|
|
|
description = "Using the Ambassador pattern to abstract (network) services"
|
|
|
|
keywords = ["Examples, Usage, links, docker, documentation, examples, names, name, container naming"]
|
|
|
|
[menu.main]
|
2016-01-23 23:36:40 -05:00
|
|
|
parent = "engine_admin"
|
2016-07-19 00:40:07 -04:00
|
|
|
weight = 15
|
2015-06-07 23:07:20 -04:00
|
|
|
+++
|
|
|
|
<![end-metadata]-->
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-04-21 11:50:09 -04:00
|
|
|
# Link via an ambassador container
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
Rather than hardcoding network links between a service consumer and
|
2014-05-14 13:22:49 -04:00
|
|
|
provider, Docker encourages service portability, for example instead of:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
(consumer) --> (redis)
|
|
|
|
|
2014-05-14 13:22:49 -04:00
|
|
|
Requiring you to restart the `consumer` to attach it to a different
|
|
|
|
`redis` service, you can add ambassadors:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
(consumer) --> (redis-ambassador) --> (redis)
|
|
|
|
|
2014-05-14 13:22:49 -04:00
|
|
|
Or
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
(consumer) --> (redis-ambassador) ---network---> (redis-ambassador) --> (redis)
|
|
|
|
|
2014-05-14 13:22:49 -04:00
|
|
|
When you need to rewire your consumer to talk to a different Redis
|
|
|
|
server, you can just restart the `redis-ambassador` container that the
|
|
|
|
consumer is connected to.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-14 13:22:49 -04:00
|
|
|
This pattern also allows you to transparently move the Redis server to a
|
2014-04-15 20:53:12 -04:00
|
|
|
different docker host from the consumer.
|
|
|
|
|
2014-05-14 13:22:49 -04:00
|
|
|
Using the `svendowideit/ambassador` container, the link wiring is
|
|
|
|
controlled entirely from the `docker run` parameters.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-04-21 11:50:09 -04:00
|
|
|
## Two host example
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-14 13:22:49 -04:00
|
|
|
Start actual Redis server on one Docker host
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
big-server $ docker run -d --name redis crosbymichael/redis
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-14 13:22:49 -04:00
|
|
|
Then add an ambassador linked to the Redis server, mapping a port to the
|
2014-04-15 20:53:12 -04:00
|
|
|
outside world
|
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
big-server $ docker run -d --link redis:redis --name redis_ambassador -p 6379:6379 svendowideit/ambassador
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
On the other host, you can set up another ambassador setting environment
|
2014-05-14 13:22:49 -04:00
|
|
|
variables for each remote port we want to proxy to the `big-server`
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
client-server $ docker run -d --name redis_ambassador --expose 6379 -e REDIS_PORT_6379_TCP=tcp://192.168.1.52:6379 svendowideit/ambassador
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-14 13:22:49 -04:00
|
|
|
Then on the `client-server` host, you can use a Redis client container
|
|
|
|
to talk to the remote Redis server, just by linking to the local Redis
|
|
|
|
ambassador.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
client-server $ docker run -i -t --rm --link redis_ambassador:redis relateiq/redis-cli
|
2014-04-15 20:53:12 -04:00
|
|
|
redis 172.17.0.160:6379> ping
|
|
|
|
PONG
|
|
|
|
|
|
|
|
## How it works
|
|
|
|
|
2014-05-14 13:22:49 -04:00
|
|
|
The following example shows what the `svendowideit/ambassador` container
|
|
|
|
does automatically (with a tiny amount of `sed`)
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-14 13:22:49 -04:00
|
|
|
On the Docker host (192.168.1.52) that Redis will run on:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
# start actual redis server
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker run -d --name redis crosbymichael/redis
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
# get a redis-cli container for connection testing
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker pull relateiq/redis-cli
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
# test the redis server by talking to it directly
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker run -t -i --rm --link redis:redis relateiq/redis-cli
|
2014-04-15 20:53:12 -04:00
|
|
|
redis 172.17.0.136:6379> ping
|
|
|
|
PONG
|
|
|
|
^D
|
|
|
|
|
|
|
|
# add redis ambassador
|
2015-10-15 10:01:55 -04:00
|
|
|
$ docker run -t -i --link redis:redis --name redis_ambassador -p 6379:6379 alpine:3.2 sh
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-14 13:22:49 -04:00
|
|
|
In the `redis_ambassador` container, you can see the linked Redis
|
|
|
|
containers `env`:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-10-15 10:01:55 -04:00
|
|
|
/ # env
|
2014-04-15 20:53:12 -04:00
|
|
|
REDIS_PORT=tcp://172.17.0.136:6379
|
|
|
|
REDIS_PORT_6379_TCP_ADDR=172.17.0.136
|
|
|
|
REDIS_NAME=/redis_ambassador/redis
|
|
|
|
HOSTNAME=19d7adf4705e
|
2015-10-15 10:01:55 -04:00
|
|
|
SHLVL=1
|
|
|
|
HOME=/root
|
2014-04-15 20:53:12 -04:00
|
|
|
REDIS_PORT_6379_TCP_PORT=6379
|
|
|
|
REDIS_PORT_6379_TCP_PROTO=tcp
|
|
|
|
REDIS_PORT_6379_TCP=tcp://172.17.0.136:6379
|
|
|
|
TERM=xterm
|
|
|
|
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
|
|
|
PWD=/
|
2015-10-15 10:01:55 -04:00
|
|
|
/ # exit
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-14 13:22:49 -04:00
|
|
|
This environment is used by the ambassador `socat` script to expose Redis
|
|
|
|
to the world (via the `-p 6379:6379` port mapping):
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker rm redis_ambassador
|
2015-10-15 10:01:55 -04:00
|
|
|
$ CMD="apk update && apk add socat && sh"
|
|
|
|
$ docker run -t -i --link redis:redis --name redis_ambassador -p 6379:6379 alpine:3.2 sh -c "$CMD"
|
|
|
|
[...]
|
|
|
|
/ # socat -t 100000000 TCP4-LISTEN:6379,fork,reuseaddr TCP4:172.17.0.136:6379
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-14 13:22:49 -04:00
|
|
|
Now ping the Redis server via the ambassador:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-14 13:22:49 -04:00
|
|
|
Now go to a different server:
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-10-15 10:01:55 -04:00
|
|
|
$ CMD="apk update && apk add socat && sh"
|
|
|
|
$ docker run -t -i --expose 6379 --name redis_ambassador alpine:3.2 sh -c "$CMD"
|
|
|
|
[...]
|
|
|
|
/ # socat -t 100000000 TCP4-LISTEN:6379,fork,reuseaddr TCP4:192.168.1.52:6379
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-14 13:22:49 -04:00
|
|
|
And get the `redis-cli` image so we can talk over the ambassador bridge.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker pull relateiq/redis-cli
|
|
|
|
$ docker run -i -t --rm --link redis_ambassador:redis relateiq/redis-cli
|
2014-04-15 20:53:12 -04:00
|
|
|
redis 172.17.0.160:6379> ping
|
|
|
|
PONG
|
|
|
|
|
|
|
|
## The svendowideit/ambassador Dockerfile
|
|
|
|
|
2015-10-15 10:01:55 -04:00
|
|
|
The `svendowideit/ambassador` image is based on the `alpine:3.2` image with
|
2015-10-06 06:43:06 -04:00
|
|
|
`socat` installed. When you start the container, it uses a small `sed`
|
2014-05-14 13:22:49 -04:00
|
|
|
script to parse out the (possibly multiple) link environment variables
|
|
|
|
to set up the port forwarding. On the remote host, you need to set the
|
|
|
|
variable using the `-e` command line option.
|
|
|
|
|
|
|
|
--expose 1234 -e REDIS_PORT_1234_TCP=tcp://192.168.1.52:6379
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-14 13:22:49 -04:00
|
|
|
Will forward the local `1234` port to the remote IP and port, in this
|
|
|
|
case `192.168.1.52:6379`.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
#
|
2015-10-06 06:43:06 -04:00
|
|
|
# do
|
|
|
|
# docker build -t svendowideit/ambassador .
|
2014-04-15 20:53:12 -04:00
|
|
|
# then to run it (on the host that has the real backend on it)
|
2015-10-06 06:43:06 -04:00
|
|
|
# docker run -t -i -link redis:redis -name redis_ambassador -p 6379:6379 svendowideit/ambassador
|
2014-04-15 20:53:12 -04:00
|
|
|
# on the remote host, you can set up another ambassador
|
2015-10-06 06:43:06 -04:00
|
|
|
# docker run -t -i -name redis_ambassador -expose 6379 -e REDIS_PORT_6379_TCP=tcp://192.168.1.52:6379 svendowideit/ambassador sh
|
|
|
|
# you can read more about this process at https://docs.docker.com/articles/ambassador_pattern_linking/
|
|
|
|
|
|
|
|
# use alpine because its a minimal image with a package manager.
|
|
|
|
# prettymuch all that is needed is a container that has a functioning env and socat (or equivalent)
|
|
|
|
FROM alpine:3.2
|
|
|
|
MAINTAINER SvenDowideit@home.org.au
|
|
|
|
|
|
|
|
RUN apk update && \
|
|
|
|
apk add socat && \
|
|
|
|
rm -r /var/cache/
|
2015-10-15 10:01:55 -04:00
|
|
|
|
2016-01-08 15:52:11 -05:00
|
|
|
CMD env | grep _TCP= | (sed 's/.*_PORT_\([0-9]*\)_TCP=tcp:\/\/\(.*\):\(.*\)/socat -t 100000000 TCP4-LISTEN:\1,fork,reuseaddr TCP4:\2:\3 \&/' && echo wait) | sh
|