VultrとRancherで作るLaravel環境
あけましておめでとうございます。
初詣で引いたおみくじが大吉で、ちょっとうれしい加藤です。
今回は、VultrとRancherでLaravelが動作する環境を構築します。
Vultr
https://www.vultr.com/
1時間単位で利用できるSSD VPSサービスです。
東京リージョンが存在するため、日本国内からのレイテンシも10ms以下と快適に利用できます。
Rancher
http://rancher.com/
洗練されたWeb UI でDockerのクラスタ及びコンテナ管理ができます。ついに、日本語対応しました!
1. Vultrにインスタンスを作成する
iPXEブートでRancherOSを起動します。
事前に、Startup scriptを登録しておきます。
#!ipxe
# Boots RancherOS in Ramdisk with persistent storage on disk /dev/vda
# Location of Kernel/Initrd images
set base-url http://releases.rancher.com/os/latest
kernel ${base-url}/vmlinuz rancher.state.formatzero=true rancher.state.autoformat=[/dev/vda] rancher.password=[初回ログインパスワード]
initrd ${base-url}/initrd
boot
登録したstartup Scriptを使用し、インスタンス追加ししばらくすると、下記のユーザーで接続できるようになります。
ユーザー名:rancher
パスワード:startup scriptで設定した値
起動後、RancherOSのインストールを行います。
下記の内容で、cloud-config.ymlファイルを作成します。
hostname: rancher-laravel
ssh_authorized_keys:
- [公開鍵]
上記のファイルを使い、ディスクにRancherOS をインストールします。
sudo ros install -c cloud-config.yml -d /dev/sda
2. Rancher serverのインストール、各種設定、Dockerホストの追加
Qiitaの記事が素晴らしいです!
こちらを参考にDockerホストの追加まで行ってください。
Vultrを使用している場合、Rancher上でDockerホストの追加ができるようになります。
-
Qiita - RancherでDockerクラスタを構築する-② VultrにRancher Server を立ち上げる
http://qiita.com/TongTheDopeness/items/58713ed877cb0f409de8 -
Qiita - RancherでDockerクラスタを構築する-③ RancherからVULTR APIでDockerホストを追加する
http://qiita.com/TongTheDopeness/items/e469ea72bcd2aaeb6af3
3. Rancherでスタックを追加
Rancherのスタック追加画面で、下記の情報を指定し、追加します。
-
(参考)PAYPER TRAIL - Docker, Rancher, and Laravel: Easy and Safe Scalability! By Luca Critelli
https://paypertrail.com/blog/tech/docker-rancher-and-laravel-easy-and-safe-scalability
docker-compose.yml
postgres:
image: 'postgres:latest'
environment:
- POSTGRES_USER=pguser
- POSTGRES_PASSWORD=pgpass
redis:
image: redis:3
restart: always
tty: true
stdin_open: true
web:
image: 'ttaranto/docker-nginx-php7'
restart: always
links:
- postgres:postgres
- redis:redis
- beanstalk:queue
environment:
- APP_DEBUG=true
- DB_HOST=postgres
- DB_DATABASE=testdb
- DB_USERNAME=testuser
- DB_PASSWORD=testpasswd
- QUEUE_DRIVER=redis
- USERMOD="1000 www-data"
ports:
- '80:80'
beanstalk:
image: 'schickling/beanstalkd:latest'
restart: always
tty: true
stdin_open: true
rancher-compose.yml
web:
scale: 1
upgrade_strategy:
start_first: true
health_check:
port: 80
interval: 30000
unhealthy_threshold: 4
response_timeout: 20000
request_line: GET / HTTP/1.0
healthy_threshold: 2
redis:
scale: 1
health_check:
port: 6379
interval: 2000
unhealthy_threshold: 10
response_timeout: 2000
healthy_threshold: 2
beanstalk:
scale: 1
health_check:
port: 11300
interval: 2000
unhealthy_threshold: 10
response_timeout: 2000
healthy_threshold: 2
postgres:
scale: 1
health_check:
port: 5432
interval: 2000
unhealthy_threshold: 10
response_timeout: 2000
healthy_threshold: 2
4. 表示確認
作成した環境に接続して、phpinfo();の内容が出力さればOKです。
5. 既存のLaravelプロジェクトで使用する場合
下記のプロジェクトをフォークし内容を書き換えます。
GitHub - ttaranto/docker-nginx-php7
https://github.com/ttaranto/docker-nginx-php7
■書き換え例
Dockerfile
既存のプロジェクトファイルを使用するように書き換えます。
- # set WWW public folder
- RUN mkdir -p /var/www/public
- ADD build/index.php /var/www/public/index.php
+ RUN git clone --depth 1 https://github.com/[ユーザー名]/[プロジェクトブランチ名].git /var/www
+ RUN cd /var/www/ && composer install
build/setup.sh
スクリプトの下部に、初期設定を行う設定を追記します
+ printenv > /var/www/.env
+ touch /var/www/storage/logs/laravel.log
+ php /var/www/artisan generate:key
+ php /var/www/artisan config:clear
+ php /var/www/artisan config:cache
+ php /var/www/artisan route:cache
+ php /var/www/artisan optimize --force
6. まとめ
AWSがちょっと高い!Herokuがちょっと高い!
という人には、VultrとRancherという組み合わせは、かなり良いのではないかと思います!
以上です!