Skip to content

Ansible playbook to automatically deploy OpenFaaS with Docker Swarm on Cherryservers infrastructure

Notifications You must be signed in to change notification settings

lukaskalvenas/CherryServers-Ansible-OpenFaaS-example

Repository files navigation

CherryServers-Ansible-OpenFaaS-example

This example will use Ubuntu as the base operating system to deploy one master node and a user-specified amount (e.g. two) worker nodes on Docker swarm. Those will then automatically join the master node via public IP address and Docker swarm token combination.

Requirements

  • Ansible
  • The Cherry Servers module connects to Cherry Servers Public API via cherry-python package. You need to install it with pip (this might need to be done as sudo):
  •   pip3 install cherry-python --user
    

Before you start

You will need a Cherry Servers account with credit in balance to order services with hourly billing.

Create an API key at https://portal.cherryservers.com/#/settings/api-keys/ and export it to your working terminal session

export CHERRY_AUTH_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9"

Configure the global variables on the "group_vars/all.yml" file. Download and run the cherryctl script to get a list of server plan IDs.

$ vim Downloads/CherryServers-Ansible-OpenFaaS-example-master/group_vars/all.yml
project_id: 88513
plan_id: 161
region: EU-East-1
number_of_masters: 1
number_of_workers: 2
image: 'Ubuntu 16.04 64bit'
key_file: '/home/lukas/.ssh/id_rsa'
key_file_pub: "{{ key_file }}.pub"
master_hostname: master.node%02d
worker_hostname: worker.node%02d

Please note that by this time you should already have exported your CherryServers API token, otherwise none of the playbooks will run.

Adding your SSH key to CherryServers

Run "ansible-playbook ssh_add_keys.yml" playbook to upload your SSH key to CherryServers.

$ cd Downloads/CherryServers-Ansible-OpenFaaS-example-master/
$ ansible-playbook ssh_add_keys.yml 
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not
match 'all'

PLAY [Cherry Servers API] ***************************************************************************************

TASK [Gathering Facts] ******************************************************************************************
ok: [localhost]

TASK [Add SSH key] **********************************************************************************************
changed: [localhost]

PLAY RECAP ******************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0   

Running the deployment playbook

Before running the playbook, make sure you have the necessary files in Ansible's working directory

Downloads/CherryServers-Ansible-OpenFaaS-example-master$ tree .
.
├── ansible.cfg
├── group_vars
│   └── all.yml
├── hosts
├── library
│   ├── cherryservers_ips.py
│   ├── cherryservers_server.py
│   └── cherryservers_sshkey.py
├── openfaas_deploy.yml
├── README.md
├── server_terminate.yml
└── ssh_add_keys.yml

2 directories, 10 files

Once you're ready, execute "ansible-playbook openfaas_deploy.yml" playbook. The full process may take up to 20 minutes to complete. For detailed playbook output, run "ansible-playbook -vvv openfaas_deploy.yml"

$ cd Downloads/CherryServers-Ansible-OpenFaaS-example-master/
$ ansible-playbook openfaas_deploy.yml 
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not
match 'all'


PLAY [OpenFaaS deployment on Cherry Servers] ********************************************************************

TASK [Gathering Facts] ******************************************************************************************
ok: [localhost]

TASK [Deploying master node] ************************************************************************************
changed: [localhost]

TASK [Register master node] *************************************************************************************
changed: [localhost]

TASK [set_fact] *************************************************************************************************
ok: [localhost]

PLAY [Preparing the master node and registering necessary variables] ***************

It will first deploy the master node and register all the necessary variables. Once that's done, the specified amount of worker servers will follow to deploy.

The worker servers will then be automatically added to the Docker swarm. When the playbook finishes, log into the master node, change the working directory to "/faas" and run the following commands

cd /faas
git checkout 0.8.9
./deploy_stack.sh

This will install the default OpenFaaS function stack for you. Use the provided login credentials to access the master GUI control panel at http://$master_ip:8080 and begin working. Alternatively, you can use the OpenFaaS CLI tools to perform the tasks from the terminal.

Good luck!

When no longer needed

$ cd Downloads/CherryServers-Ansible-OpenFaaS-example-master/
$ ansible-playbook server_terminate.yml

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not
match 'all'


PLAY [Cherry Servers API module] ********************************************************************************

TASK [Gathering Facts] ******************************************************************************************
ok: [localhost]

TASK [Terminating master node(s)] *******************************************************************************
changed: [localhost]

TASK [Terminating worker node(s)] *******************************************************************************
changed: [localhost]

PLAY RECAP ******************************************************************************************************
localhost                  : ok=3    changed=2    unreachable=0    failed=0 

You may need to edit the playbook according to your preference to terminate all servers succesfully. Keep in mind that all Docker swarm members will be terminated and the data will get permanently wiped.

About

Ansible playbook to automatically deploy OpenFaaS with Docker Swarm on Cherryservers infrastructure

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages