Skip to content

Instantly share code, notes, and snippets.

@mssio
Last active August 29, 2015 14:09
Show Gist options
  • Save mssio/12d02e90bcd12b484e26 to your computer and use it in GitHub Desktop.
Save mssio/12d02e90bcd12b484e26 to your computer and use it in GitHub Desktop.
Multiple Node Service Template for Core OS

#Multiple Node Service Template for Core OS

Create 2 directory to manage using fleetctl for this service:

$ mkdir {templates,instances}

Create this file in templates/[email protected]

[Unit]
Description=Express Node App

# Requirements
Requires=etcd.service
Requires=docker.service
Requires=node-discovery@%i.service

# Dependency ordering
After=etcd.service
After=docker.service
Before=node-discovery@%i.service

[Service]
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker kill nodeapp
ExecStartPre=-/usr/bin/docker rm nodeapp
ExecStartPre=/usr/bin/docker pull mssio/node-express-demo
ExecStart=/usr/bin/docker run --name nodeapp --rm -p 21832:3000 mssio/node-express-demo
ExecStop=/usr/bin/docker stop nodeapp

Create this file in templates/[email protected]

[Unit]
Description=Node app #%i etcd registration

# Requirements
Requires=etcd.service
Requires=node@%i.service

# Dependency ordering
After=etcd.service
After=node@%i.service
BindsTo=node@%i.service

[Service]
# Get CoreOS environmental variables
EnvironmentFile=/etc/environment

# Start
## Test whether service is accessible and then register useful information
ExecStart=/bin/bash -c '\
  while true; do \
    curl -f ${COREOS_PRIVATE_IPV4}:21832; \
    if [ $? -eq 0 ]; then \
      etcdctl set /services/node/${COREOS_PRIVATE_IPV4} \'${COREOS_PRIVATE_IPV4}:21832\' --ttl 30; \
    else \
      etcdctl rm /services/node/${COREOS_PRIVATE_IPV4}; \
    fi; \
    sleep 20; \
  done'

# Stop
ExecStop=/usr/bin/etcdctl rm /services/node/${COREOS_PRIVATE_IPV4}

[X-Fleet]
# Schedule on the same machine as the associated Node service
X-ConditionMachineOf=node@%i.service

If you want to create 3 node instances type this on the shell command:

$ ln -s templates/[email protected] instances/[email protected]
$ ln -s templates/[email protected] instances/[email protected]
$ ln -s templates/[email protected] instances/[email protected]
$ ln -s templates/[email protected] instances/[email protected]
$ ln -s templates/[email protected] instances/[email protected]
$ ln -s templates/[email protected] instances/[email protected]

Finally just run this command from shell and you Core OS Instance should be up and run perfectly

$ fleetctl start instances/*

If your fleetctl is not working ensure that fleetctl tunnel is set properly run this command to set it if you are using Vagrant:

$ export FLEETCTL_TUNNEL=127.0.0.1:2222
$ ssh-add ~/.vagrant.d/insecure_private_key

# Remove the known host if you have previously destroyed another instance
$ rm ~/.fleetctl/known_hosts

PS:

  1. You can change 21832 with any other of random port number
  2. You should change mssio/node-express-demo with your own node application image
  3. Based on tutorial in Digital Ocean
  4. This tutorial is working on all current Core OS Version of: Stable (444.5.0), Beta (444.5.0), Alpha (494.0.0) in Thu, November 13 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment