Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implementing Triton CNS
Refactoring to use Triton CNS automated DNS.
  • Loading branch information
misterbisson committed Mar 4, 2016
1 parent f787baf commit 7be8272
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 61 deletions.
8 changes: 0 additions & 8 deletions .env.example

This file was deleted.

2 changes: 1 addition & 1 deletion .gitignore
@@ -1 +1 @@
.env
_env
15 changes: 3 additions & 12 deletions README.md
Expand Up @@ -10,21 +10,18 @@ This repo is a demonstation of a multi-tier container-native application archite
- [Couchbase](https://www.joyent.com/blog/how-to-dockerize-a-complete-application#couchbase), for the data tier
- Consul, acting as a discovery service
- Containerbuddy, to help with service discovery
- [CloudFlare-watcher](https://www.joyent.com/blog/how-to-dockerize-a-complete-application#cloudflare-watcher), to update DNS
- CloudFlare DNS, to make the site accessible by domain name on the Internet
- Triton, our container-native infrastructure platform

![Diagram of Touchbase architecture](./doc/triton-touchbase.png)

### Running the example

You can run this entire stack using the [`start.sh` script](https://github.com/tgross/triton-touchbase/blob/master/start.sh) found at the top of the repo. You'll need a CloudFlare account and a domain for which you've delegated DNS to CloudFlare, but if you'd like to skip that part you can simply comment out `startCloudflare` line.
You can run this entire stack using the [`start.sh` script](https://github.com/tgross/triton-touchbase/blob/master/start.sh) found at the top of the repo.

Once you're ready:

1. [Get a Joyent account](https://my.joyent.com/landing/signup/) and [add your SSH key](https://docs.joyent.com/public-cloud/getting-started).
1. Install the [Docker Toolbox](https://docs.docker.com/installation/mac/) (including `docker` and `docker-compose`) on your laptop or other environment, as well as the [Joyent CloudAPI CLI tools](https://apidocs.joyent.com/cloudapi/#getting-started) (including the `smartdc` and `json` tools)
1. Have your CloudFlare API key handy.
1. [Configure Docker and Docker Compose for use with Joyent](https://docs.joyent.com/public-cloud/api-access/docker):

```bash
Expand All @@ -37,7 +34,7 @@ At this point you can run the example on Triton:

```bash
./start.sh env
# here you'll be asked to fill in the .env file
# here you'll be asked to fill in the _env file
./start.sh

```
Expand All @@ -50,15 +47,9 @@ or in your local Docker environment (note that you may need to increase the memo

```

The `.env` file that's created will need to be filled in with the values described below:
The `_env` file that's created will need to be filled in with the values described below:

```
CF_API_KEY=<your CloudFlare API key>
CF_AUTH_EMAIL=<the email address associated with your CloudFlare account>
CF_ROOT_DOMAIN=<the root domain you want to manage. ex. example.com>
SERVICE=nginx <the name of the service you want to monitor>
RECORD=<the A-record you want to manage. ex. my.example.com>
TTL=600 <the DNS TTL you want, in seconds. min 120, max 2147483647>
COUCHBASE_USER=<the administrative user you want for your Couchbase cluster>
COUCHBASE_PASS=<the password you want for that Couchbase user>
Expand Down
2 changes: 2 additions & 0 deletions _env.example
@@ -0,0 +1,2 @@
COUCHBASE_USER=
COUCHBASE_PASS=
23 changes: 9 additions & 14 deletions docker-compose-local.yml
Expand Up @@ -16,6 +16,8 @@ consul:
- 8500
dns:
- 127.0.0.1
labels:
- triton.cns.services=tb-consul

# Manually bootstrap the first instance, then...
# Scale this tier and each additional container/instance will automatically
Expand All @@ -35,10 +37,12 @@ couchbase:
- 11211
- 18091
- 18092
env_file: .env
env_file: _env
environment:
- CONSUL_HOST=http://consul:8500
- COUCHBASE_SERVICE_NAME=couchbase
labels:
- triton.cns.services=tb-couchbase

# the main application
touchbase:
Expand All @@ -53,6 +57,8 @@ touchbase:
/opt/containerbuddy/containerbuddy
-config file:///opt/containerbuddy/touchbase.json
/usr/local/bin/run-touchbase.sh
labels:
- triton.cns.services=tb-touchbase

# Nginx as a load-balancing tier and reverse proxy
nginx:
Expand All @@ -69,16 +75,5 @@ nginx:
/opt/containerbuddy/containerbuddy
-config file:///opt/containerbuddy/nginx.json
nginx -g "daemon off;"
# Support dynamic DNS for the load balancing tier
# https://www.joyent.com/blog/automatic-dns-updates-with-containerbuddy
cloudflare:
image: 0x74696d/triton-cloudflare
mem_limit: 128m
links:
- consul:consul
restart: always
env_file: .env
command: >
/opt/containerbuddy/containerbuddy
-config file:///opt/containerbuddy/cloudflare.json
labels:
- triton.cns.services=tb-nginx
23 changes: 9 additions & 14 deletions docker-compose.yml
Expand Up @@ -13,6 +13,8 @@ consul:
- 8500
dns:
- 127.0.0.1
labels:
- triton.cns.services=tb-consul

# Manually bootstrap the first instance, then...
# Scale this tier and each additional container/instance will automatically
Expand All @@ -32,10 +34,12 @@ couchbase:
- 11211
- 18091
- 18092
env_file: .env
env_file: _env
environment:
- CONSUL_HOST=http://consul:8500
- COUCHBASE_SERVICE_NAME=couchbase
labels:
- triton.cns.services=tb-couchbase

# the main application
touchbase:
Expand All @@ -50,6 +54,8 @@ touchbase:
/opt/containerbuddy/containerbuddy
-config file:///opt/containerbuddy/touchbase.json
/usr/local/bin/run-touchbase.sh
labels:
- triton.cns.services=tb-touchbase

# Nginx as a load-balancing tier and reverse proxy
nginx:
Expand All @@ -65,16 +71,5 @@ nginx:
command: >
/opt/containerbuddy/containerbuddy
nginx -g "daemon off;"
# Support dynamic DNS for the load balancing tier
# https://www.joyent.com/blog/automatic-dns-updates-with-containerbuddy
cloudflare:
image: 0x74696d/triton-cloudflare
mem_limit: 128m
links:
- consul:consul
restart: always
env_file: .env
command: >
/opt/containerbuddy/containerbuddy
-config file:///opt/containerbuddy/cloudflare.json
labels:
- triton.cns.services=tb-nginx
108 changes: 96 additions & 12 deletions start.sh
Expand Up @@ -19,20 +19,103 @@ usage() {
}

env() {
if [ ! -f ".env" ]; then
if [ ! -f "_env" ]; then
echo 'You need a configuration file with credentials.'
echo 'Copying .env.example to .env'
cp .env.example .env
cat .env.example
echo 'Copying _env.example to _env'
cp _env.example _env
cat _env.example
exit 1
else
. .env
. _env
fi
export COUCHBASE_USER=${COUCHBASE_USER:-Administrator}
export COUCHBASE_PASS=${COUCHBASE_PASS:-password}
CB_RAM_QUOTA=${CB_RAM_QUOTA:-100}
}


tritonConfigured() {

# is node-triton installed?
local triton_installed=0
command -v triton >/dev/null 2>&1 && triton_installed=1
if [ "0" = "$triton_installed" ]; then
echo
echo "Error!"
echo "The Triton CLI tool does not appear to be installed!"
echo
echo "Please visit:"
echo "https://www.joyent.com/blog/introducing-the-triton-command-line-tool"
echo "for installation instructions."
echo

sleep 3
command -v open >/dev/null 2>&1 && `open https://www.joyent.com/blog/introducing-the-triton-command-line-tool` || true

exit
fi

# Get username from Docker
local docker_user=$(docker info 2>&1 | grep "SDCAccount:" | awk -F": " '{print $2}' OFS="/")

# Get username from Triton
local triton_user=$(triton profile get | grep "account:" | awk -F": " '{print $2}' OFS="/")

# Get DC from Docker
local docker_dc=$(echo $DOCKER_HOST | awk -F"/" '{print $3}' OFS="/" | awk -F"\." '{print $1}' OFS="/")

# Get DC from Triton
local triton_dc=$(triton profile get | grep "url:" | awk -F"/" '{print $3}' OFS="/" | awk -F"\." '{print $1}' OFS="/")

if [ ! "$docker_user" = "$triton_user" ] || [ ! "$docker_dc" = "$triton_dc" ]; then
echo
echo "Error!"
echo "The Triton CLI configuration does not match the Docker CLI configuration!"
echo
echo "Docker user: ${docker_user}"
echo "Triton user: ${docker_user}"
echo
echo "Docker data center: ${docker_dc}"
echo "Triton data center: ${triton_dc}"
echo
echo "The Triton CLI tool must be configured to use the same user and data center as the Docker CLI."
echo
echo "Please visit:"
echo "https://www.joyent.com/blog/introducing-the-triton-command-line-tool#using-profiles"
echo "for instructions on how to configure and set profiles for Triton."
echo

sleep 3
command -v open >/dev/null 2>&1 && `open "https://www.joyent.com/blog/introducing-the-triton-command-line-tool#using-profiles"` || true

exit
fi

# Is Triton CNS enabled
local triton_cns_enabled=$(triton account get | grep cns | awk -F": " '{print $2}' OFS="/")

if [ ! "true" = "$triton_cns_enabled" ]; then
echo
echo "Notice!"
echo "Triton CNS is not enabled for this account."
echo
echo "Triton CNS, an automated DNS built into Triton, is not required, but this blueprint demonstrates its use."
echo
echo "Please visit:"
echo "https://www.joyent.com/blog/NEW-URL-HERE"
echo "for information and usage details for Triton CNS."
echo
echo "Enable Triton CNS with the following command:"
echo
echo "triton account update triton_cns_enabled=true"
echo

sleep 3
command -v open >/dev/null 2>&1 && `open "https://www.joyent.com/blog/NEW-URL-HERE"` || true
sleep 3
fi
}

prep() {
echo "Starting example application"
echo "project prefix: $PREFIX"
Expand All @@ -43,10 +126,15 @@ prep() {
}

# get the IP:port of a container via either the local docker-machine or from
# sdc-listmachines.
# triton inst get $instance_name.
getIpPort() {
if [ -z "${COMPOSE_CFG}" ]; then
local ip=$(sdc-listmachines --name ${PREFIX}_$1_1 | json -a ips.1)
# try to get a DNS name from Triton CNS
local ip=$(triton inst get ${PREFIX}_$1_1 | json -a dns_names | grep "\.svc\." | tail -1 | awk -F"\"" '{print $2}')
if [ -z "$ip" ]; then
# fail back to the IP number, if TCNS is not active
local ip=$(triton inst get ${PREFIX}_$1_1 | json -a ips.1)
fi
local port=$2
else
local ip=$(docker-machine ip default 2>/dev/null || true)
Expand Down Expand Up @@ -189,10 +277,6 @@ startNginx() {
command -v open >/dev/null 2>&1 && `open http://${NGINX}` || true
}

startCloudflare() {
${COMPOSE} up -d cloudflare
}

# scale the entire application to 2 Nginx, 3 app servers, 3 CB nodes
scale() {
echo
Expand Down Expand Up @@ -236,14 +320,14 @@ if [ ! -z "$cmd" ]; then
exit
fi

tritonConfigured
env
prep
startDatabase
showConsoles
setupCouchbase
startApp
startNginx
startCloudflare

echo
echo 'Touchbase cluster is launched!'
Expand Down

0 comments on commit 7be8272

Please sign in to comment.