Skip to content

Commit

Permalink
avoid jq requirement in swarm example
Browse files Browse the repository at this point in the history
  • Loading branch information
displague committed Dec 18, 2018
1 parent b2993bc commit 6c525c3
Showing 1 changed file with 109 additions and 94 deletions.
203 changes: 109 additions & 94 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,6 @@ You will need a Linode APIv4 Personal Access Token. Get one here: <https://deve
docker-machine create -d linode --linode-token=<linode-token> --linode-root-pass=<linode-root-pass> linode
```

### Full Example

```bash
LINODE_TOKEN=e332cf8e1a78427f1368a5a0a67946ad1e7c8e28e332cf8e1a78427f1368a5a0 # Should be 65 lowercase hex chars
LINODE_ROOT_PASSWORD=$(openssl rand -base64 32); echo Password for root: $LINODE_ROOT_PASSWORD

docker-machine create -d linode --linode-token=$LINODE_TOKEN --linode-root-pass=$LINODE_ROOT_PASSWORD linode
eval $(docker-machine env linode)
docker run --rm -it debian bash
```

```bash
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
linode * linode Running tcp://45.79.139.196:2376 v18.05.0-ce

$ docker-machine rm linode
About to remove linode
WARNING: This action will delete both local reference and remote instance.
Are you sure? (y/n): y
(default) Removing linode: 8753395
Successfully removed linode
```

### Options

| Argument | Env | Default | Description
Expand Down Expand Up @@ -97,85 +73,124 @@ Detailed run output will be emitted when using the LinodeGo `LINODE_DEBUG=1` opt
LINODE_DEBUG=1 docker-machine --debug create -d linode --linode-token=$LINODE_TOKEN --linode-root-pass=$ROOT_PASS machinename
```

## Provisioning Docker Swarm

Let's create a docker swarm with master and worker node using private networking. Before starting make sure you have `docker-machine` and `jq` installed.
## Examples

1. Create install.sh bash script and replace LINODE_TOKEN with your actual linode access token.
```sh
#!/bin/bash
set -e
### Simple Example

LINODE_TOKEN=<YOUR LINODE TOKEN>
```bash
LINODE_TOKEN=e332cf8e1a78427f1368a5a0a67946ad1e7c8e28e332cf8e1a78427f1368a5a0 # Should be 65 lowercase hex chars
LINODE_ROOT_PASSWORD=$(openssl rand -base64 32); echo Password for root: $LINODE_ROOT_PASSWORD
LINODE_REGION=eu-central

create_node() {
local name=$1
docker-machine create \
-d linode \
--linode-label=$name \
--linode-instance-type=g6-nanode-1 \
--linode-image=linode/ubuntu18.04 \
--linode-region=$LINODE_REGION \
--linode-token=$LINODE_TOKEN \
--linode-root-pass=$LINODE_ROOT_PASSWORD \
--linode-create-private-ip \
$name
}

get_private_ip() {
local name=$1
echo $(docker-machine inspect $name | jq -r '.Driver.PrivateIPAddress')
}

init_swarm_master() {
local name=$1
local ip=$(get_private_ip $name)
docker-machine ssh $name "docker swarm init --advertise-addr ${ip}"
}

init_swarm_worker() {
local master_name=$1
local worker_name=$2
local master_addr=$(get_private_ip $master_name):2377
local join_token=$(docker-machine ssh $master_name "docker swarm \join-token worker -q")
docker-machine ssh $worker_name "docker swarm join --token=${join_token} ${master_addr}"
}

# create master node
create_node master01

# create worker node
create_node worker01

# init swarm master
init_swarm_master master01

# init swarm worker
init_swarm_worker master01 worker01
```

2. After provision is successful check docker swarm status.

```sh
docker-machine ssh master01 "docker node ls"
docker-machine create -d linode --linode-token=$LINODE_TOKEN --linode-root-pass=$LINODE_ROOT_PASSWORD linode
eval $(docker-machine env linode)
docker run --rm -it debian bash
```

Output should show active swarm leader and worker.
```bash
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
linode * linode Running tcp://45.79.139.196:2376 v18.05.0-ce

```
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
f8x7zutegt2dn1imeiw56v9hc * master01 Ready Active Leader 18.09.0
ja8b3ut6uaivz5hf98gah469y worker01 Ready Active 18.09.0
$ docker-machine rm linode
About to remove linode
WARNING: This action will delete both local reference and remote instance.
Are you sure? (y/n): y
(default) Removing linode: 8753395
Successfully removed linode
```

3. Cleanup resources

```sh
docker-machine rm worker01 -y
docker-machine rm master01 -y
```
### Provisioning Docker Swarm

The following script serves as an example for creating a [Docker Swarm](https://docs.docker.com/engine/swarm/) with master and worker nodes using the Linode Docker machine driver and private networking.

This script is provided for demonstrative use. A production swarm environment would require hardening.

1. Create an `install.sh` bash script using the source below. Run `bash install.sh` and provide a Linode APIv4 Token when prompted.

```sh
#!/bin/bash
set -e

read -p "Linode Token: " LINODE_TOKEN
# LINODE_TOKEN=...
LINODE_ROOT_PASSWORD=$(openssl rand -base64 32); echo Password for root: $LINODE_ROOT_PASSWORD
LINODE_REGION=eu-central

create_node() {
local name=$1
docker-machine create \
-d linode \
--linode-label=$name \
--linode-instance-type=g6-nanode-1 \
--linode-image=linode/ubuntu18.04 \
--linode-region=$LINODE_REGION \
--linode-token=$LINODE_TOKEN \
--linode-root-pass=$LINODE_ROOT_PASSWORD \
--linode-create-private-ip \
$name
}

get_private_ip() {
local name=$1
docker-machine inspect -f '{{.Driver.PrivateIPAddress}}' $name
}

init_swarm_master() {
local name=$1
local ip=$(get_private_ip $name)
docker-machine ssh $name "docker swarm init --advertise-addr ${ip}"
}

init_swarm_worker() {
local master_name=$1
local worker_name=$2
local master_addr=$(get_private_ip $master_name):2377
local join_token=$(docker-machine ssh $master_name "docker swarm join-token worker -q")
docker-machine ssh $worker_name "docker swarm join --token=${join_token} ${master_addr}"
}

# create master node
create_node master01

# create worker node
create_node worker01

# init swarm master
init_swarm_master master01

# init swarm worker
init_swarm_worker master01 worker01
```

1. After provisioning succeeds, check the Docker Swarm status. The output should show active an swarm leader and worker.

```sh
$ eval $(docker-machine env master01)
$ docker node ls

ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
f8x7zutegt2dn1imeiw56v9hc * master01 Ready Active Leader 18.09.0
ja8b3ut6uaivz5hf98gah469y worker01 Ready Active 18.09.0
```

1. [Create and scale Docker services](https://docs.docker.com/engine/reference/commandline/service_create/) (left as an excercise for the reader).

```bash
$ docker service create --name my-service --replicas 3 nginx:alpine
$ docker node ps master01 worker01
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
7cggbrqfqopn \_ my-service.1 nginx:alpine master01 Running Running 4 minutes ago
7cggbrqfqopn \_ my-service.1 nginx:alpine master01 Running Running 4 minutes ago
v7c1ni5q43uu my-service.2 nginx:alpine worker01 Running Running 4 minutes ago
2w6d8o3hdyh4 my-service.3 nginx:alpine worker01 Running Running 4 minutes ago
```

1. Cleanup the resources

```sh
docker-machine rm worker01 -y
docker-machine rm master01 -y
```

## Discussion / Help

Expand Down

0 comments on commit 6c525c3

Please sign in to comment.