[DOCKER] I made a useful tool for Digital Ocean

Introduction

I have created a DigitalOcean management tool (dioc), so I would like to introduce it. (Please use at your own risk.) The operation uses Docker and Python 3.5 on Ubuntu. For Windows, [How to use Docker with Ubuntu](http://qiita.com/Tsutomu-KKE@github/items/29414e2d4f30b2bc94ae#ubuntu%E3%82%92%E5%85%A5%E3% 82% 8C% E3% 81% A6docker% E3% 82% 92% E4% BD% BF% E3% 81% 86% E6% 96% B9% E6% B3% 95), VirtualBox, Ubuntu14, Docker [ ^ 3] Please install. With Docker, you can get started right away with the Docker image dioc-python-3.5.

[^ 3]: On Linux, "wget -qO- https://get.docker.com/ | sh"

1.png

Terminology

-DigitalOcean: EC2 of AWS Cloud services like .com / jp / ec2 /). --Droplet: An instance of the virtual OS. -Snapshot: A saved state of Droplet. You can create a Droplet based on a Snapshot. --Image: The source of Droplet. You can choose between the official one and the private one created by Snapshot. Some of the official ones are OS-only and some include the app [^ 1](shown below). --Billing: 5 \ $, 10 \ $, 20 \ $, 40 \ $, 80 \ $, 160 \ $, 320 \ $, 480 \ $, 640 \ $ per month, memory is priced It is proportional. When you create a Droplet, you will be charged even if it is stopped (Power Off) until it is destroyed (Destroy). To avoid unnecessary charges, you can stop Droplet, create a Snapshot from Droplet, and discard Droplet. It's free to create a snapshot now, but it seems that you will be charged in the future. In addition, you will be charged even if the amount of data transferred exceeds the limit.

2.png

[^ 1]: App provided by DigitalOcean

-Cassandra: NoSQL database -Discourse: Forum management -Django: Web framework -Docker: Container management -Dokku: Heroku (free cloud server) clone -Drone: Continuous integration -Drupal: Content Management System -Elixir: Programming language

Why Digital Ocean?

--Cheap. There is a payment limit. From $ 5 a month.

  • early. Start within 1 minute. All SSD.
  • Easy. Simple management screen. -Many users. ――Now with a 10 $ privilege! ??

Disadvantages of DigitalOcean

--There is no Tokyo region. Choose Singapore. --Compared to AWS, it does not have the following functions, so it will take some time on a large scale.

  • Reserved Instance
  • Marketplace
  • AvailabilityZone
  • SecurityGroup
  • ElasticIP
  • ELB
  • VPC

reference: AWS instance is too expensive. If you look for it, it is cheap and delicious. DigitalOcean summary I wanted to know before using

Benefits of using Docker with DigitalOcean

--Compared to distributing the application, it can be operated more reliably. --You can create an execution environment locally without charge. --With CoreOS, it's easy to deploy. --It works lightly.

Reference: Docker official OS list

DigitalOcean account creation

Please note that from here you will need a credit card or PayPal and you will be charged.

  1. Open DigitalOcean [^ 2], enter your e-mail and password, and press [Create Account]. --You will receive an email, click the link and enter your credit card or other method.

[^ 2]: If you register from this URL, you will get points for me.

Let's use DigitalOcean

Droplet login passwords are usually emailed to you. It takes time to log in after confirming the password by e-mail (waiting for a few minutes), so I will explain how to use SSH Key.

SSH Key settings

--Run the following on Ubuntu. Remember the passphrase as you will need it to log in.

  • ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa
  • ssh-add --Execute the following and copy the displayed contents.
  • cat ~/.ssh/id_rsa.pub --Log in to DigitalOcean and select Settings from in the upper right. --Select [Security] on the left and press [Add SSH Key]. --Set [Name] to [id_rsa], paste the copied content into [Public SSH Key], and press [Create SSH Key].

Try to create the first Droplet

-Press [Create Droplet]. -Change [Choose an image] to [CoreOS]. -Change [Choose a size] to the cheapest "$ 5 / mo". -Change [Choose a datacenter region] to "Singapore", which is closer to Japan. -Check "id_rsa" for [Add your SSH keys]. --Click the [Create] button. Billing will start. ――It will start in a few tens of seconds. --Copy [IP Address]. --Run the following on Ubuntu. The first time you will be asked for your passphrase. --ssh core @ [copied IP Address] --You can see the specifications by executing the following.

  • cat /proc/cpuinfo --Please log out. -Destroy the Droplet created on the Droplet screen. --Please note that if you do not Destroy, you will be charged all the time.

Issuance of API token

An API token is required to operate Digital Ocean programmatically. Please get the token by the following method. If you have a token, you are free to use Digital Ocean. Please take good care of your tokens. Reference: A story about a beginner who made a mistake on AWS and was abused and charged $ 6,000, almost crying.

--Log in to DigitalOcean and select API at the top. -Press [Generate new token]. -Enter "apitok" in [Token Name]. Leave [Write] checked. --Press [Generate Token]. --Please make a note of the token displayed on the screen. If you close this screen, the same token will not be displayed again. --If you forget your token, delete it and recreate it.

Introduction of Dioc

Environment

--Install cryptography. For Anaconda, do the following: - "conda install -y cryptography" --If you want to run locally --Please do "pip install dioc" while using Python3.5. After that, add it to ".bashrc" as shown below (only once is OK). --If you want to run inside a Docker container --Please add to ".bashrc" locally as shown below (only once is OK). After that, "docker run -it -v ~: / root tsutomu7 / dioc-python-3.5 "Please.

ubuntu


cat << eof >> .bashrc
export DIOC_TOKEN="Digital Ocean API Token"
export DIOC_DEFAULT_SSHKEY=id_rsa
export DIOC_DEFAULT_SIZE=512mb
export DIOC_DEFAULT_REGION=sgp1
if [ -x /usr/local/bin/dio -o -x /opt/conda/bin/dio ]; then
  eval "$(_DIO_COMPLETE=source dio)"
fi
eof
source ~/.bashrc

When using with Bash

For Bash operations, the command is "dio". You can use bash-completion. The "image name" should be, for example, "'89 9.17.0 (stable)'". A list of images is displayed with "dio list image".

ubuntu


#Creating a Droplet
dio create "Droplet name" "Image name"

#Creating a Droplet from a Snapshot
dio create "Droplet name" "Snapshot name"

#Command execution with SSH
dio ssh "Droplet name" "command"

#File copy
dio scp "Droplet name:File bus" "Droplet name:File bus"

#IP address confirmation
dio ip "Droplet name"

#Droplet list check
dio list

#Check other list(The target is droplet, image, private, ssh, size,region can be selected)
dio list "Target"

#Droplet removal
dio destroy "Droplet name"

Mongodb server startup example

ubuntu


dio create test
dio ssh test
mkdir mongo
docker run -d -p 27017:27017 -v ~/mongo:/data/db --name mongo mongo
# docker exec -it mongo mongo
exit
dio destroy test

Web server startup example

With DigitalOcean, you can easily start the server. Below, the GoTour server is running.

ubuntu


dio create test '' user_data='"#!/bin/bash\ndocker run -p 80:8080 tsutomu7/gotour"'
firefox `dio ip test`
# do something
dio destroy test

When using from Python 3.5

It's basically a wrapper for python-digitalocean, so refer to that for methods. You can also start Droplet with the Droplet method. The SSH client will automatically retry up to 6 times.

python


from dioc import *
#Creating a Droplet
d = Droplet('dgoc') #Create a CoreOS Droplet named dgoc

#Creating a Droplet from a Snapshot
d = Droplet('dgoc', 'test') #Create a Droplet with the name dgoc from a snapshot called test

#Confirmation of IP address
print(d.ip_address)

#Creating an SSH client
c = ssh_client(d)

#Command execution with SSH
c.exec_command(command)

#Droplet removal
d.destroy()

that's all


Recommended Posts