nginxconsuldocker-swarma-recordsconsul-template

how to deploy applications on docker swarm and expose that ip address to A record


I have successfully setup the docker swarm cluster with nginx. on my ec2 instances. using this tutorial

https://botleg.com/stories/load-balancing-with-docker-swarm/

I am using like this

Manager 1

node 1

node 2

I am using consol with registrator for service discovery.

In Consul Machine docker run --restart=unless-stopped -d -p 8500:8500 -h consul progrium/consul -server -bootstrap

Swarm Manager docker run --restart=unless-stopped -d -p 3375:2375 swarm manage --replication --advertise=mangerip:3375 consul://consulip:8500/

docker run --restart=unless-stopped -d --name=registrator -h=host --volume=/var/run/docker.sock:/tmp/docker.sock gliderlabs/registrator:latest consul://consulip:8500

Node1 docker run --restart=unless-stopped -d swarm join --advertise=node1ip:2375 consul://consulip:8500/

docker run --restart=unless-stopped -d --name=registrator -h=host --volume=/var/run/docker.sock:/tmp/docker.sock gliderlabs/registrator:latest consul://consulip:8500

Node2 docker run --restart=unless-stopped -d swarm join --advertise=node2:2375 consul://consulip:8500/

docker run --restart=unless-stopped -d --name=registrator -h=host --volume=/var/run/docker.sock:/tmp/docker.sock gliderlabs/registrator:latest consul://consulip:8500

my Dockerfile

FROM nginx:latest

RUN apt-get update \
  && apt-get install -y unzip

ADD files/start.sh /bin/start.sh
RUN chmod +x /bin/start.sh
ADD files/default.ctmpl /templates/default.ctmpl

ADD https://releases.hashicorp.com/consul-template/0.12.2/consul-template_0.12.2_linux_amd64.zip /usr/bin/
RUN unzip /usr/bin/consul-template_0.12.2_linux_amd64.zip -d /usr/local/bin

EXPOSE 80
ENTRYPOINT ["/bin/start.sh"]

start.sh

#!/bin/bash
service nginx start
consul-template -consul=$CONSUL_URL -template="/templates/default.ctmpl:/etc/nginx/conf.d/default.conf:service nginx reload"

default.ctmpl

{{$app := env "APP_NAME"}}

upstream {{printf $app}} {
    least_conn;
    {{range service $app}}
    server {{.Address}}:{{.Port}} max_fails=3 fail_timeout=60 weight=1;{{end}}
}

server {
    listen 80 default;

    location / {
        proxy_pass http://{{printf $app}};
    }
}

docker-compose.yml

version: '2'

services:
  lb:
    build: .
    container_name: lb
    ports:
      - "80:80"
    environment:
      - APP_NAME=tutum-nodejs-redis
      - CONSUL_URL=${KV_IP}:8500
    depends_on:
      - web
    networks:
      - front-tier

  web:
    image: hanzel/tutum-nodejs-redis
    ports:
      - "4000"
    environment:
      - APP_PORT=4000
      - REDIS_IP=redis
      - REDIS_PORT=6379
    depends_on:
      - redis
    networks:
      - front-tier
      - back-tier

  redis:
    image: redis
    container_name: redis
    command: redis-server --appendonly yes
    volumes:
      - redis-data:/data
    networks:
      - back-tier

volumes:
  redis-data:
    driver: local

networks:
  front-tier:
    driver: overlay
  back-tier:
    driver: overlay

The problem with this, Which Ip Address I need point to A record Since docker swarm picking node automatically.

lets say First time I am running this it may pick up the first node. In case I am removing this and redoing althe steps that time it will pick different node and different ip. so that time I need to change my A record. Hw to solve this problem,

I googled it , and I found the wagl. that gives me DNS based service dicovery, that is cool. but it was very basic I guess , I dont know whether that was production ready or not.

Please any one tell me, How to do from A to Z flow, ie, from the deployment to configuring the IP address to A record. or just point me some link.


Solution

  • I don't think you need to change you A record for that. Since you already know the ip addresses of your cluster, and if you are deploying it using docker-compose you can add a constraint on your load-balancer environment: constraint:node==node1