AWS 環境部署:web server移轉至AWS EC2

莉森羊
莉森羊
Aug 22, 2017 · 4 min read

Web導入流程敘述

  1. 事先將web開發慣用的docker image 以docker pull 方式落在本地端後,打包成ami。
  2. 開發者將web已編譯好的程式壓縮檔放在s3,依照先前ami以userdata方式將web程式抓下至本地端,並輸入必須參數後,自動運行Container
  3. 開發者後續能夠使用ansible 遠端控制web server更新

實作時間

2016.08

Tool

docker, AWS S3, AWS EC2

userdata

1. 建置空環境的userdata,包成AWS AMI

  • 預期環境中有docker 及 meteorhacks/meteord:base 的docker image

設定檔位置: s3://<bucket_name>/frontend/userdata/WEB_AMI.sh

#!/bin/bash# import maintain keyaws s3 cp s3://<bucket_name>/common/SW_KeyPair_Maintain.pub /tmp/cat /tmp/KeyPair_Maintain.pub >> /home/ec2-user/.ssh/authorized_keys# cloudwatch log agent installcurl https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py -Ochmod +x ./awslogs-agent-setup.py./awslogs-agent-setup.py -n -r ap-northeast-1 -c s3://<bucket_name>/common/cloudwatch/Cloudwatchlog_Web.conf# install dockerdestination=/home/ec2-useryum update -yyum install -y dockerservice docker startusermod -a -G docker ec2-userchkconfig docker on# pull imagedocker pull meteorhacks/meteord:base

環境檢查測試

# 測試docker環境與映像檔是否成功安裝docker images

2. 用AMI後所用的userdata

  • 將web已編譯好的程式壓縮檔(web.tar.gz)映射至特定資料夾,以及附上其他需要的參數

設定檔位置: s3://<bucket_name>/frontend/userdata/WEB_DEPLOY.sh

#!/bin/bashdestination=/home/ec2-user# copy s3 app to ec2aws s3 cp s3://<bucket_name>/frontend/web.tar.gz $destination# depoly appdocker run -d \--restart=on-failure:10 \-v /home/ec2-user:/bundle \-e ROOT_URL=http://xyz.company.com \-e PORT=80 \-e MONGO_URL=mongodb://mongo.xyz.company-aws.com:27017/web \-e METEOR_SETTINGS='{"elasticsearch":{"host":"elasticsearch.xyz.company-aws.com:9200"}}' \-p 8080:80 \meteorhacks/meteord:base

環境檢查測試

# 測試docker容器是否有成功啟動docker psdocker logs <container name># 測試是否成功deploycurl 0.0.0.0:8080curl localhost:8080

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade