CloudflontとS3で503を返すメンテナンスページ作成
S3だけでメンテページを作成しようとしたが、S3単体だと503を返すことができないので、
CloudfrontとS3でメンテナンスページを作成したときのメモ。
s3バケットを作成
・まずマネジメントコンソールに入り、S3のバケットを作成。
・作ったバケットの横の虫眼鏡を押して、プロパティを表示。
・「静的サイトホスティング」をクリックし、「ウェブサイトのホスティングを有効にする」を選択。
・「インデックスドキュメント」はなくては作れないので、適当に「index.html」としておく。
・「アクセス許可」を選択し、「バケットポリシーの編集」を選択。
{ "Version": "2012-10-17", "Statement": [ { "Sid": "PublicReadGetObject", "Effect": "Allow", "Principal": "*", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::[バケット名]/*" } ] }
上記のようにポリシーを作成する(これでアップロードしたものすべてが自動で閲覧可になる)
s3にリソースアップロード
・s3マネジメントコンソールで対象のバケット名をクリックし、右上の「アクション」ボタンを押して、「アップロード」を選択。
・ドラッグアンドドロップでhtmlファイル等リソースをアップロード。
・ちゃんと表示されているかどうか先ほどのプロパティ画面の「静的サイトホスティング」のところにある「エンドポイント」にアクセス
(「バケット名.s3-website-ap-northeast-1.amazonaws.com」とかなっているやつ。そこから「/maintenance.html」みたいにリソース名でアクセスできる)
cloudflontを作成
・「create Distribution」を選択。「Web」のほうの「Get Started」を選択。
・「Origin Settings」の「Origin Domain Name」にバケット名を入れる(文字入力すればサジェストされる)
・「Restrict Bucket」をYESにする。
・「Origin Access Identity」をCreate a New Identityにする。
・「Grant Read Permissions on Bucket」をYes, Update Bucket Policyにする。
・下のほうにある「Distribution Settings」内にある「Alternate Domain Names (CNAMEs)」というところに、先ほどのs3のエンドポイントを入力
・あとはデフォルトのまま「Create Distribution」を押して作成する。
Error Pages設定
・cloudflontのサイドメニュー「Distributions」を選択したら、先ほど作ったDistribution一覧にあると思うので、IDをクリック。
・タブメニューの「Error Pages」を選択し、「Create Custom Error Response」を選択。
・「HTTP Error Code」は「403:Forbidden」、「Error Caching Minimum TTL」はデフォルト。
・「Customize Error Response」を「Yes」にする。
・「Response Page path」という設定項目がでてくるので、s3に設置したメンテページのhtmlのパスを設定。
(直下に「maintenance.html」というファイルを置いていたら「/maintenance.html」)
・「HTTP Response Code」は「503:Service Unavailable」を選択し、「Create」を選択。
疎通確認
・15分~30分くらいで表示されるようになるので、その後cloudflontのサイドメニュー「Distributions」の一覧のところにある要素「Domain Name」にアクセスする。
(ランダム文字列.cloudfront.netみたいなやつ)
・無事メンテページが表示されて、503がレスポンスで返ってくれば準備完了。
・実際メンテナンスに入る前に、DNSの設定を疎通確認したクラウドフロントの「Domain Name」にすればOK。
※Route53のAlias設定でcloudflontディストリビューションを設定する場合は、Route53の料金がかかりません!
追記
・作りたてのS3は、307 Temporary Redirectを上記設定で疎通を行うと返してくることがたまにある。
対策を調べたのですがこれというものは見つからず、事前にs3バケットのみ用意しておくのがよさそうです。(データ置いとくだけなら1GB月4円くらいなので)
Linuxサーバーなどで、自分の外向きIPアドレスが知りたいとき
curl ifconfig.co
phantomjsとcasperjsのインストール
phantomjsのインストール
バイナリをダウンロードするやり方。ビルドはすごい時間がかかる。
cd /usr/local/src wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2 tar jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2 mv phantomjs-2.1.1-linux-x86_64 phantomjs cd phantomjs ln -sf `pwd`/bin/phantomjs /usr/local/bin/phantomjs phantomjs --version #エラーになる場合は、だいたい以下を入れれば直る sudo yum -y install fontconfig-devel
casperjsのインストール
cd /usr/local/src git clone https://github.com/casperjs/casperjs.git cd casperjs/ # シンボリックリンクを作成 ln -sf `pwd`/bin/casperjs /usr/local/bin/casperjs # 動作確認 casperjs --version 1.1.2
casperjsでjQueryを使う
cd /usr/local/src/casperjs mkdir lib cd lib wget https://code.jquery.com/jquery-1.9.1.min.js mv jquery-1.9.1.min.js jquery.min.js
※casperjsのソース内で使うときはcreate時に以下のように設定
var casper = require('casper').create({ clientScripts: [phantom.casperPath + "/lib/jquery.min.js"], });
おまけメモ
バッチ実行する場合のsyntaxError回避 ソースの一番上にこれ入れる
#!/usr/local/bin/phantomjs /usr/local/bin/casperjs
MongoDBを少ない容量で起動させる
MongoDBを普通に起動させると、ジャーナルファイルが3GBくらい一気に容量を確保してしまうので、 開発環境などでは不便。
以下のように設定すればジャーナルファイルのサイズが128MBになる。
sudo serivce mongod stop #失敗したら sudo killall mongod sudo rm /var/lib/mongodb/journal/* #設定によっては場所が違う場合もあるので注意 sudo vi /etc/mongod.conf #(または、mongodb.conf) #以下を追加 ---------- journal=true smallfiles=true ---------- sudo service mongod start
dfコマンドなどで容量がすくなくなっていればOK。
Vagrantで良く使うコマンドメモ。
Vagrantで良く使うコマンドのまとめ。
box系
boxの追加
以下のコマンド。1.5以降BOXの名前のみで良くなりました。
$vagrant box add chef/centos-7.1
※chefとしていますが、chef入りのというのではなく、chefが作ってるBOXです。
※chef/ 以下にosを指定すると色々なものに対応しています。(centos-6.5等)
box一覧
$vagrant box list
box削除
$vagrant box remove [box名]
vm操作系
vm初期化
$vagrant init
vm起動
$vagrant up
※initしたときに作られたvagrantfileに各種設定(指定するboxなど)を書き込んでおく
vmへログイン
$vagrant ssh
※windowsは使えない。以下の設定でteraterm等でログイン
host: 127.0.0.1
port: 2222
user: vagrant
password: vagrant
rootpass: vagrant
※公開鍵認証でもログイン可「[vagrant initしたフォルダ].vagrant\machines\default\virtualbox\」の「private_key」ファイルが秘密鍵
vmシャットダウン
$vagrant halt
vm削除
$vagrant destroy
KVMでHDDを追加する時のメモ
CentOSやRHL上で動かすKVMのHDD容量を追加する時の手順。
イメージファイルを増量
まずKVMを動かしているホスト機で、増やしたいVMのイメージを追加する。
$ qemu-img resize vmtest.img +10G
※vmtestというイメージに10G追加したい場合
fdiskでパーティションを増量
VM内にログインし、fdiskコマンドでdisk容量が増えているか確認。
$ssh vmtest
$fdisk -l
Disk /dev/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e180d
Device Boot Start End Blocks Id System
/dev/vda1 * 3 1018 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2 1018 20806 9972736 8e Linux LVM
Partition 2 does not end on cylinder boundary.
この場合は元が10Gでさらに10G足したので無事増えているので、/dev/vdaのパーティションを区切ります。
$fdisk /dev/vda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
パーティション一覧を確認します。
Command (m for help): p
Disk /dev/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e180d
Device Boot Start End Blocks Id System
/dev/vda1 * 3 1018 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2 1018 20806 9972736 8e Linux LVM
Partition 2 does not end on cylinder boundary.
パーティションが2つあるので、3つ目に新たに追加します。
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
次にどこのcylinderから始めるかと聞かれるので、この場合は2つ目の最後20806から+1した20807を入力します。
First cylinder (1-41610, default 1): 20807
Last cylinder, +cylinders or +size{K,M,G} (20807-41610, default 41610): 41610
ラストはデフォルトの最後まででOKです。
またpコマンドで無事追加されたのを確認します。
Command (m for help): p
Disk /dev/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e180d
Device Boot Start End Blocks Id System
/dev/vda1 * 3 1018 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2 1018 20806 9972736 8e Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/vda3 20807 41610 10485216 83 Linux
3つ目の追加したパーティションもLVMにします。
Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/vda: 21.5 GB, 21474836480 bytes
16 heads, 63 sectors/track, 41610 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000e180d
Device Boot Start End Blocks Id System
/dev/vda1 * 3 1018 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2 1018 20806 9972736 8e Linux LVM
Partition 2 does not end on cylinder boundary.
/dev/vda3 20807 41610 10485216 8e Linux LVM
無事変わったのを確認したらセーブして再起動します。
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
$reboot -h now
追加パーティションを物理Vol化&論理Vol拡張
再度VMに入り、追加パーティションを物理ボリューム化します。
$ssh vmtest
$pvcreate /dev/vda3
Physical volume "/dev/vda3" successfully created
追加されたか確認
$pvdisplay
--- Physical volume ---
PV Name /dev/vda2
VG Name vg_vmtestimg1
PV Size 9.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 2434
Free PE 0
Allocated PE 2434
PV UUID XCTuf9-07iA-iwnO-CAh0-QT0u-Temb-xymCGY
"/dev/vda3" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/vda3
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID EZN6pR-s8OX-SY1f-faQq-y7T2-VJAe-cnPxgB
VG名を取得
$vgdisplay
--- Volume group ---
VG Name vg_vmtestimg1
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 9.51 GiB
PE Size 4.00 MiB
Total PE 2434
Alloc PE / Size 2434 / 9.51 GiB
Free PE / Size 0 / 0
VG UUID Wfi6Mw-tK8R-SZmH-xOBm-mmuq-1bmL-hFn40B
VGを拡張&確認
$vgextend vg_vmtestimg1 /dev/vda3
Volume group "vg_vmtestimg1" successfully extended
$vgdisplay
--- Volume group ---
VG Name vg_vmtestimg1
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 19.50 GiB
PE Size 4.00 MiB
Total PE 4993
Alloc PE / Size 2434 / 9.51 GiB
Free PE / Size 2559 / 10.00 GiB
VG UUID Wfi6Mw-tK8R-SZmH-xOBm-mmuq-1bmL-hFn40B
論理ボリューム名を確認
$lvdisplay
--- Logical volume ---
LV Path /dev/vg_vmtestimg1/lv_root
LV Name lv_root
VG Name vg_vmtestimg1
LV UUID JtIgxT-mBPj-LO1H-Uc9u-13t3-1gwX-Y11UUR
LV Write Access read/write
LV Creation host, time dev-pfortune-img1, 2015-09-08 07:16:27 +0900
LV Status available
# open 1
LV Size 8.51 GiB
Current LE 2178
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
論理ボリュームを追加
$lvextend -l +100%FREE /dev/vg_vmtestimg1/lv_root
Size of logical volume vg_vmtestimg1/lv_root changed from 8.51 GiB (2178 extents) to 18.50 GiB (4737 extents).
Logical volume lv_root successfully resized
$resize2fs /dev/vg_vmtestimg1/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_vmtestimg1/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/vg_vmtestimg1/lv_root to 4850688 (4k) blocks.
The filesystem on /dev/vg_vmtestimg1/lv_root is now 4850688 blocks long.
centos7の場合は resize2fsではなく、xfs_growfsで拡張を行う
$xfs_growfs /dev/vg_vmtestimg1/lv_root
容量をdfコマンドで確認
$df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg_vmtestimg1-lv_root
18968780 1115892 16889024 7% /
tmpfs 961100 0 961100 0% /dev/shm
/dev/vda1 487652 73701 388351 16% /boot
追加されていれば無事終了です。