Skip to content

Instantly share code, notes, and snippets.

@yamamoto-febc
Last active April 10, 2016 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamamoto-febc/245eecb60705a75124115afb878f6ba3 to your computer and use it in GitHub Desktop.
Save yamamoto-febc/245eecb60705a75124115afb878f6ba3 to your computer and use it in GitHub Desktop.
さくらのクラウドでAuto Scaling ~ Docker活用編 ~ ref: http://qiita.com/yamamoto-febc/items/9c21f1efac9ebc6ae30b
① さくらのAPI経由でCPU負荷を取得、判定
② DockerMachineでさくらのクラウド上にマシンを作成
マシン作成と同時にGSLBヘの登録、Dockerコンテナの起動が行われます。
③ GSLBが死活監視で追加されたサーバでのサービス起動を確認します。
確認ができたら新サーバのIPをDNS応答するようになります。
curl -L https://github.com/yamamoto-febc/docker-machine-sakuracloud/releases/download/v0.0.10-p1/docker-machine-driver-sakuracloud-`uname -s`-`uname -m` >/usr/local/bin/docker-machine-driver-sakuracloud && \
chmod +x /usr/local/bin/docker-machine/docker-machine-driver-sakuracloud
$ docker-machine rm [マシン名]
git clone https://github.com/yamamoto-febc/sakura-auto-scaling-with-docker.git
cd sakura-auto-scaling-with-docker
cp env.sample .env # 設定ファイルひな形をコピー
vi .env # 設定ファイルを編集
#==============================================================================
# APIキー(アクセストーン、シークレット)を記入してください
export SAKURACLOUD_ACCESS_TOKEN=先ほど控えたトークンを入力
export SAKURACLOUD_ACCESS_TOKEN_SECRET=先ほど控えたシークレットを入力
# 利用するリージョン(石狩第1、石狩第2、東京いづれか)を指定してください。
export SAKURACLOUD_REGION=is1a / is1b / tk1a のいずれかを入力
#==============================================================================
#******************************************************************************
# Name : CPU_TIME_SCALE_OUT_THRESHOLD
# Description: スケールアウト閾値(CPU時間:)
# 1コアあたり1の時100%の使用率となる(あくまで目安らしい)
# http://cloud-news.sakura.ad.jp/activity/#server
# 複数コアのマシンでスケールアウトさせる場合は計算に注意
#******************************************************************************
CPU_TIME_SCALE_OUT_THRESHOLD=0.6 #60%以上のCPU負荷でスケールアウト
#******************************************************************************
# Name : CPU_TIME_SCALE_IN_THRESHOLD
# Description: スケールイン閾値(CPU時間:)
# CPU_TIME_SCALE_OUT_THRESHOLDの説明を参照
#******************************************************************************
CPU_TIME_SCALE_IN_THRESHOLD=0.4 #40%以下のCPU負荷でスケールイン
chmod +x auto_scaling.sh
./auto_scaling.sh
$./auto_scaling.sh
Creating CA: /Users/hogehoge/.docker/machine/certs/ca.pem
Creating client certificate: /Users/hogehoge/.docker/machine/certs/cert.pem
Running pre-create checks...
Creating machine...
(sakura-autoscaling-master) Creating SSH public key...
(sakura-autoscaling-master) password is not set, generated password:xxxxx
(sakura-autoscaling-master) Created Server ID: 11280xxxxx
(sakura-autoscaling-master) Created Disk ID: 112800xxxxxx
(sakura-autoscaling-master) Waiting for disk to become available
(sakura-autoscaling-master) Editted Disk Id: 112800xxxxxxx
(sakura-autoscaling-master) Waiting for disk to become available
(sakura-autoscaling-master) Setting SakuraCloud GSLB: sakura-autoscaling-gslb
(sakura-autoscaling-master) Added GSLB,Please Set CNAME Record : ex. 'your-lb-hostname IN CNAME [site-1128003xxxxx.gslb3.sakura.ne.jp]'
(sakura-autoscaling-master) Waiting for server to become Running
(sakura-autoscaling-master) Waiting for server to become Stopped
(sakura-autoscaling-master) Waiting for server to become Running
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with ubuntu(upstart)...
Installing Docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: /usr/local/bin/docker-machine env sakura-autoscaling-master
Creating network "apps_default" with the default driver
Building web
Step 1 : FROM php:apache
apache: Pulling from library/php
efd26ecc9548: Pull complete
a3ed95caeb02: Pull complete
589a9d9a7c64: Pull complete
4683f98d2423: Pull complete
d5f645469de1: Pull complete
2950971584b0: Pull complete
6fdfa3810627: Pull complete
75061bf48855: Pull complete
0f5751ac9b60: Pull complete
d14da6152c4e: Pull complete
ff7b20e586d5: Pull complete
c068e3c63576: Pull complete
f0b34f661a58: Pull complete
Digest: sha256:f14c45f43edd52947ca4e17db4f796ca4aebb72687ad63b2e19e6e5583a09715
Status: Downloaded newer image for php:apache
---> dc7c95ca2afc
Step 2 : MAINTAINER Kazumichi Yamamoto <yamamoto.febc@gmail.com>
---> Running in 3fd84af01152
---> 473bd6db11d3
Removing intermediate container 3fd84af01152
Step 3 : COPY html/ /var/www/html
---> 8037f0951d27
Removing intermediate container 1053bbc54724
Successfully built 8037f0951d27
Creating apps_web_1
******************************************************************
MasterNode created. Please re-run this script after a few minutes.
******************************************************************
crontab -e
# 5分ごとに実行する場合
# パスは各自読み替えてください。
*/5 * * * * /path/to/your/directory/auto_scaling.sh >> /var/log/sakura-auto-scaling.log 2>&1
# 現在実行中のマシンの確認
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
sakura-autoscaling-client1 - sakuracloud Running tcp://153.127.193.231:2376 v1.10.3
sakura-autoscaling-master - sakuracloud Running tcp://153.120.171.11:2376 v1.10.3
# 負荷をかけたいマシンへ接続
$docker-machine ssh [マシン名]
# 接続後、以下コマンドを実施
$ openssl speed #CPU利用率がほぼ100%になります。5分程度で終了します。
# ログを見たいマシンのDockerデーモンを使うように環境変数設定
$ eval $(docker-machine env [マシン名])
# ログ確認
$ docker logs -f `docker ps -aq`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment