Skip to content

Instantly share code, notes, and snippets.

@jonasrosland
Last active August 23, 2020 02:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonasrosland/ca278ee883edea3536e1 to your computer and use it in GitHub Desktop.
Save jonasrosland/ca278ee883edea3536e1 to your computer and use it in GitHub Desktop.
Use Docker Swarm with a data persistence layer

Use Docker Swarm with a data persistence layer

Create a Docker Swarm discovery token

curl -X POST https://discovery.hub.docker.com/v1/clusters
YOURSWARMTOKEN

Export the correct environment variables

export AWS_ACCESS_KEY_ID="YOURKEY"
export AWS_INSTANCE_TYPE="t2.micro"
export AWS_DEFAULT_REGION="us-east-1"
export AWS_ROOT_SIZE="16"
export AWS_SECRET_ACCESS_KEY="YOURSECRETKEY"
export AWS_SECURITY_GROUP="default"
export AWS_SUBNET_ID="YOURSUBNET"
export AWS_VPC_ID="YOURVPC"
export AWS_ZONE="a"
export SWARM_TOKEN="YOURSWARMTOKEN"

Create the Docker Swarm Master

docker-machine create --driver amazonec2 --amazonec2-access-key $AWS_ACCESS_KEY_ID  --amazonec2-instance-type $AWS_INSTANCE_TYPE --amazonec2-region $AWS_DEFAULT_REGION --amazonec2-root-size $AWS_ROOT_SIZE --amazonec2-secret-key $AWS_SECRET_ACCESS_KEY --amazonec2-security-group $AWS_SECURITY_GROUP --amazonec2-subnet-id $AWS_SUBNET_ID --amazonec2-vpc-id $AWS_VPC_ID --amazonec2-zone $AWS_ZONE --engine-install-url "https://get.docker.com" --swarm --swarm-master --swarm-discovery token://$SWARM_TOKEN swarm-master

Map your shell to the Swarm Master

eval $(docker-machine env --swarm swarm-master)

Create the Docker Swarm Nodes

docker-machine create --driver amazonec2 --amazonec2-access-key $AWS_ACCESS_KEY_ID --amazonec2-instance-type $AWS_INSTANCE_TYPE --amazonec2-region $AWS_DEFAULT_REGION --amazonec2-root-size $AWS_ROOT_SIZE --amazonec2-secret-key $AWS_SECRET_ACCESS_KEY --amazonec2-security-group $AWS_SECURITY_GROUP --amazonec2-subnet-id $AWS_SUBNET_ID --amazonec2-vpc-id $AWS_VPC_ID --amazonec2-zone $AWS_ZONE --engine-install-url "https://get.docker.com" --swarm --swarm-discovery token://$SWARM_TOKEN swarm-node1
docker-machine create --driver amazonec2 --amazonec2-access-key $AWS_ACCESS_KEY_ID --amazonec2-instance-type $AWS_INSTANCE_TYPE --amazonec2-region $AWS_DEFAULT_REGION --amazonec2-root-size $AWS_ROOT_SIZE --amazonec2-secret-key $AWS_SECRET_ACCESS_KEY --amazonec2-security-group $AWS_SECURITY_GROUP --amazonec2-subnet-id $AWS_SUBNET_ID --amazonec2-vpc-id $AWS_VPC_ID --amazonec2-zone $AWS_ZONE --engine-install-url "https://get.docker.com" --swarm --swarm-discovery token://$SWARM_TOKEN swarm-node2

Install and configure REX-Ray

for each in $(docker-machine ls -q); do; docker-machine ssh $each "curl -sSL https://dl.bintray.com/emccode/rexray/install | sh -" ; done
for each in $(docker-machine ls -q); do; docker-machine ssh $each "echo REXRAY_STORAGEDRIVERS=ec2 | sudo tee --append /etc/environment"; done
for each in $(docker-machine ls -q); do; docker-machine ssh $each "echo AWS_ACCESS_KEY=$AWS_ACCESS_KEY_ID | sudo tee --append /etc/environment"; done
for each in $(docker-machine ls -q); do; docker-machine ssh $each "echo AWS_SECRET_KEY=$AWS_SECRET_ACCESS_KEY | sudo tee --append /etc/environment"; done
for each in $(docker-machine ls -q); do; docker-machine ssh $each "sudo service rexray start"; done

Verify that REX-Ray is running

for each in $(docker-machine ls -q); do; docker-machine ssh $each "sudo service rexray status"; done
REX-Ray is running at pid 31509

Run a container with a data volume attached to it

docker run -d --volume-driver=rexray -p 6379:6379 -v redis-data:/data redis redis-server --appendonly yes

Verify that a volume has been created

docker-machine ssh $(docker inspect -f '{{ .Node.Name }}' $(docker ps -q)) "rexray volume"
- name: redis-data
  volumeid: vol-cb5a6f26
  availabilityzone: us-east-1a
  status: in-use
  volumetype: standard
  iops: 0
  size: "16"
  networkname: ""
  attachments:
  - volumeid: vol-cb5a6f26
    instanceid: i-5a803fe5
    devicename: /dev/xvdb
    status: attached

Connect to your Redis database

redis-cli -h $(docker inspect -f '{{ .Node.IP }}' $(docker ps -q))
YOURIPHERE:6379> get hello
(nil)
YOURIPHERE:6379> set hello world
OK
YOURIPHERE:6379> get hello
"world"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment