How to Deploy Droplet DigitalOcean with Vagrant

02 August 2020 | Category : DevOps

How to Deploy Droplet DigitalOcean with Vagrant

DigitalOcean is Cloud Provider the most often used developers for developing or as a production Cloud to serve their projects. Many services are offered on DigitalOcean such as Droplet and Kubernetes which famous service often used. In this article, we will share about How to Deploy Droplet on DigitalOcen with Vagrant.

In the last article, we have been explained about How to Install Vagrant on Ubuntu 18.04. Before deploying a Droplet on DigitalOcean with Vagrant, you must install Vagrant on your local machine and must have an account on DigitalOcean. If you have to do 2 requirements are we explain before, now you can follow step by step this.

Deploy Droplet DigitalOcean with Vagrant

To deploy a Droplet on DigitalOcean with vagrant, the vagrant required :

  • Install plugin vagrant-digitalocean
  • Get Access Token Key to DigitalOcean
  • Add SSH Key Local Machine to DigitalOcean

If the requirement is completed, you can use Vagrant to Deploy a Droplet. To do install plugin vagrant-digitalocean on your local machine you can this step.

  1. Install Plugin DigitalOcean on Vagrant

You can run this command to install vagrant plugin digitalocean on your local machine :

vagrant plugin install vagrant-digitalocean

Next, after the plugin successfully installed you need to add box-digitalocean as the provider of your Virtual Machine (VM). to add the box-digitalocean you can follow this command :

vagrant box add digital_ocean https://github.com/smdahlen/vagrant-digitalocean/raw/master/box/digital_ocean.box

The command above will be downloading the box-digitalocean into your local machine and adding the box-digitalocean as a provider Virtual Machine on your local machine.

  1. Get Access Token Key to DigitalOcean

After adding the box-digitalocean the next step is Get Access Token Key to DigitalOcean. This Access Token Key will be used to access the API code on DigitalOcean to create a Droplet. Here is the step to get Access Token Key on DigitalOcean :

  • Sign in to the DigitalOcean client account

  • Go to the API Menu > Tokens/Keys > Generate New Token

  • Input your Token Name with whatever name, ex. vagrant token > Generate Token

  • Copy the Access Token Key and save the Access Token Key into your note.

Now, you get the Access Token Key to accessing the API code on DigitalOcean to create a Droplet.

  1. Add SSH Key Local Machine to DigitalOcean

Here is the last step before we create Droplet with Vagrant, in this step you must add your SSH Key into the DigitalOcean. To do that you can follow this step.

If you don’t have any SSH Key on your local machine you can generate first the SSH Key with the command below :

ssh-keygen -t rsa

Then to view your SSH Key you can run the command :

cat ~/.ssh/id_rsa.pub

The output above command will show your SSH Key like this :

cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDC…………..

Next, you can Add the SSH Key into the DigitalOcean with this instruction :

  • Sign in to the DigitalOcean client account

  • Go to Settings menu > Security > Add SSH Key

  • Input your SSH Key to SSH Key Content > input the Name > Add SSH Key

Your SSH Key now has been added to DigitalOcean, and the requirements to Deploy a Droplet on DigitalOcean with Vagrant has been completed. So, next you can create a vagrant script to Deploy a Droplet.

  1. Getting Started

You have been completed the requirements to deploy Droplet with Vagrant. In the last section, you just create a simple script Vagrantfile to deploy a Droplet. To create the Vagrantfile and run it you can follow this step.

First, you need to create a folder to organize the Vagrantfile and create the file inside the folder with this command :

mkdir ~/vagrant && cd ~/vagranttouch Vagrantfile

Then edit file Vagrantfile with this command :

vim Vagrantfile

In this file, you can add this code :


Vagrant.configure("2") do |config|
   config.vm.define "digitalocean" do |config|
      config.vm.provider :digital_ocean do |provider, override|
         override.ssh.private_key_path = "~/.ssh/id_rsa"
         override.vm.box = "digital_ocean"
         override.vm.box_url = "https://github.com/devopsgroup-io/vagrant-digitalocean/raw/master/box/digital_ocean.box"
         override.nfs.functional = false
         override.vm.hostname = "YOUR_HOSTNAME_VM"
         override.vm.synced_folder ".", "/vagrant", disabled: true
         provider.token = "YOUR_ACCESS_TOKEN_KEY"
         provider.ssh_key_name = "YOUR_SSH_KEY_NAME"
         provider.image = "ubuntu-18-04-x64"
         provider.region = "lon1"
         provider.size = "s-1vcpu-1gb"
      end
   end
end

Note:
At line YOUR_HOSTNAME_VM, YOUR_ACCESS_TOKEN_KEY, and YOUR_SSH_KEY_NAME you can input with your Hostname, Access Token Key, and you SSH Key Name on DigitalOcean.

But, at line provider.image, provider.region, and provider.size if you want to change you can check the list are available in API code with this instruction :

  • Check Images on DigitalOcean
    To check the images list are available on DigitalOcean you can run this command :
vagrant digitalocean-list images YOUR_ACCESS_TOKEN_KEY

The output of the command above will show :

vagrant digitalocean-list images YOUR_ACCESS_TOKEN_KEY
Description                                        Slug                           ID

CentOS 6.9 x32                                     centos-6-x32                   31354013
CentOS 6.9 x64                                     centos-6-x64                   34902021
CentOS 7.6 x64                                     centos-7-x64                   50903182
CentOS 8.2 x64                                     centos-8-x64                   65619134
…
…
…

 

  • Check Regions on DigitalOcean

To check the regions list are available on DigitalOcean you can run this command :

vagrant digitalocean-list regions YOUR_ACCESS_TOKEN_KEY

The output of the command above will show :

vagrant digitalocean-list regions YOUR_ACCESS_TOKEN_KEY
Description                    Slug

Amsterdam 2                    ams2
Amsterdam 3                    ams3
Bangalore 1                    blr1
Frankfurt 1                    fra1
London 1                       lon1
New York 1                     nyc1
New York 2                     nyc2
New York 3                     nyc3
San Francisco 1                sfo1
San Francisco 2                sfo2
San Francisco 3                sfo3
Singapore 1                    sgp1
Toronto 1                      tor1

 

  • Check Droplet Size on DigitalOcean

To check the droplet sizes list are available on DigitalOcean you can run this command :

vagrant digitalocean-list sizes YOUR_ACCESS_TOKEN_KEY

The output of the command above will show :

vagrant digitalocean-list sizes YOUR_ACCESS_TOKEN_KEY
Memory          CPUs            Slug

49152MB         12              s-12vcpu-48gb
512MB           1               512mb
2048MB          1               s-1vcpu-2gb
1024MB          1               1gb
1024MB          3               s-3vcpu-1gb
2048MB          2               s-2vcpu-2gb
3072MB          1               s-1vcpu-3gb
4096MB          2               s-2vcpu-4gb
2048MB          2               2gb
8192MB          4               s-4vcpu-8gb
4096MB          2               c-2
4096MB          2               4gb
8192MB          2               g-2vcpu-8gb
8192MB          2               gd-2vcpu-8gb
16384MB         8               s-8vcpu-16gb
16384MB         6               s-6vcpu-16gb
8192MB          4               c-4
8192MB          4               8gb
16384MB         2               m-2vcpu-16gb
32768MB         8               s-8vcpu-32gb
16384MB         8               16gb
1024MB          1               s-1vcpu-1gb

The droplet size will determine the price of your droplet and will charge every beginning of the month.

Now, with your Vagrantfile and with your configuration of vagrant to bring up the Droplet you need run this command :

vagrant up

And the above command will show output like this :

vagrant up
Bringing machine ‘digitalocean' up with 'digital_ocean' provider...
==> nimetech: Using existing SSH key: mac
==> nimetech: Creating a new droplet...
==> nimetech: Assigned IP address: 143.xxx.xxx.xxx

After the droplet created to remote the droplet you can use command :

vagrant ssh

With that command, you will automatically remote the droplet.

And to destroy the droplet you can use command :

vagrant destroy

Conclusion

DigitalOcean is a famous Cloud Provider for Developers to deployment their project. But in some cases to deploy a Droplet on DigitalOcean the Developers are needed fast action examples with run a command to create a Droplet. With Vagrant this is one of many options to deploy a Droplet with a simple action and advantage use Vagrant maybe can use automation deployment which the script automation can customize with the requirement of the project

Share this article :