Seven Story Rabbit Hole

Sometimes awesome things happen in deep rabbit holes. Or not.

   images

Nginx Proxy for Sync Gateway Using Confd

This will walk you through setting up Sync Gateway behind nginx. The nginx conf will be auto generated based on Sync Gateway status.

Launch CoreOS instances on EC2

Recommended values:

  • ClusterSize: 3 nodes (default)
  • Discovery URL: as it says, you need to grab a new token from https://discovery.etcd.io/new and paste it in the box.
  • KeyPair: the name of the AWS keypair you want to use. If you haven’t already, you’ll want to upload your local ssh key into AWS and create a named keypair.

Wait until instances are up

screenshot

ssh into a CoreOS instance

Go to the AWS console under EC2 instances and find the public ip of one of your newly launched CoreOS instances.

screenshot

Choose any one of them (it doesn’t matter which), and ssh into it as the core user with the cert provided in the previous step:

1
$ ssh -i aws.cer -A core@ec2-54-83-80-161.compute-1.amazonaws.com

Spin up Sync Gateway containers

1
2
$ etcdctl set /couchbase.com/enable-code-refresh true
$ sudo docker run --net=host tleyden5iwx/couchbase-cluster-go update-wrapper sync-gw-cluster launch-sgw --num-nodes=2 --config-url=http://git.io/hFwa --in-memory-db

Verify etcd entries

1
2
3
4
5
$ etcdctl ls --recursive /
...
/couchbase.com/sync-gw-node-state
/couchbase.com/sync-gw-node-state/10.169.70.114
/couchbase.com/sync-gw-node-state/10.231.220.110

Create data volume container

1
2
$ wget https://raw.githubusercontent.com/lordelph/confd-demo/master/confdata.service
$ fleetctl start confdata.service

Launch sync-gateway-nginx-confd.service

1
2
3
$ wget https://raw.githubusercontent.com/lordelph/confd-demo/master/confd.service
$ sed -i -e 's/lordelph\/confd-demo/tleyden5iwx\/sync-gateway-nginx-confd/' confd.service
$ fleetctl start confd.service

Launch nginx service

1
2
$ wget https://raw.githubusercontent.com/lordelph/confd-demo/master/nginx.service
$ fleetctl start nginx.service

Verify

Try a basic http get.

1
2
3
$ nginx_ip=`fleetctl list-units | grep -i nginx | awk '{print $2}' | awk -F/ '{print $2}'`
$ curl $nginx_ip
{"couchdb":"Welcome","vendor":{"name":"Couchbase Sync Gateway","version":1},"version":"Couchbase Sync Gateway/master(a47a17f)"}

Add ‘-v’ flag to see which Sync Gateway server is servicing the request

1
2
3
4
$ curl -v $nginx_ip
...
X-Handler: 10.231.220.110:4984
...

If you repeat that a few more times, you should see different ip addresses for the handler.

Take a sync gateway out of rotation

1
$ fleetctl stop sync_gw_node@1.service sync_gw_sidekick@1.service

Now try hitting nginx again, and should not see the Sync Gw that you just shutdown as a handler.

1
2
3
4
$ curl -v $nginx_ip
...
X-Handler: 10.231.220.114:4984
...

Put sync gateway back into rotation

1
$ fleetctl start sync_gw_node@1.service sync_gw_sidekick@1.service

Now try hitting nginx again, and should again see the Sync Gw that you just restarted as being a handler.

1
2
3
4
$ curl -v $nginx_ip
...
X-Handler: 10.231.220.110:4984
...

References

Comments