hoge-hogeoのひきこもごも

インフラエンジニアだけど形を持ったインフラを触ったことがない人の徒然

beanstalk触ってる話

概要

さわってみた系の話

検証

.ebextensitonを使ってみる

プロジェクトのソースコードの最上位に.ebextensionsを掘れ?

掘って、ファイル置いてデプロイしたら変わるのだろうか。

f:id:hoge-hogeo:20180928172355p:plain

healthcheckurl.configを置いてみる

f:id:hoge-hogeo:20180928172413p:plain

hello/.ebextensions配下に上記ファイルを置いてzip化する

マネコンからアップロードして、beanstalkにデプロイさせる

エラーが出る

/healthというパスはないからね

つらい

とりあえず1台に適当な静的ファイルを置いてみる

$ sudo sh -c "echo ok >> /var/www/html/hello/public/health"

f:id:hoge-hogeo:20180928172626p:plain

直った。(実は直ってなかった)

とにかく、この使い方でいいということが分かった。

2台目にも手動でhealthファイルを配置した、がエラーが解けない。

f:id:hoge-hogeo:20180928174843p:plain

アプリケーションのバージョンが合わないと。これはもしかしてデプロイ諦めた結果?

もたついてる間にローリングデプロイがAbortされたらしい。

During an aborted deployment, some instances may have deployed the new application version. To ensure all instances are running the same version, re-deploy the appropriate application version.

f:id:hoge-hogeo:20180928172646p:plain

ちなみに、.ebextensions/healthcheckurl.configの内容も切り戻されていたので、現象としては

  • .ebextensions/healthcheckurl.configに/healthがヘルスチェックパスだと指定してアプリをデプロイ
  • 1台目に最新アプリをデプロイしたが、/healthのパスがなかったため、ヘルスチェックに失敗
  • もたもたとhealthファイルを作ったが、操作はAbortされた  →.ebextensions等の変更内容も切り戻された。(ヘルスチェックパスが/healthから/に戻っていた)
  • 最新のアプリが1台目にデプロイ済みのため、その1台目の状態が「低下」になった
  • 少し↑で直ったと思ったのは、デプロイをAbortされて古いままになっていた2台目の状態が「Ok」になっているだけだった

healthの静的ファイルもアプリ内に配置して、再デプロイしてみる。

バージョン情報が分かりにくかったので文字列にしてみる。

f:id:hoge-hogeo:20180928172903p:plain

なんとかなった。実行バージョンも更新された。

f:id:hoge-hogeo:20180928172920p:plain

td-agentを入れてみる

インストール

$ cat hello/.ebextensions/requirement.config
commands:
  command block:
    command: |
      curl -L "https://toolbelt.treasuredata.com/sh/install-amazon1-td-agent3.sh" > td-agent_install.sh
      sh td-agent_install.sh
      td-agent-gem install fluent-plugin-forest

デプロイされたEC2で確認

$ rpm -qa|grep td-a
td-agent-3.2.0-0.el2018.x86_64


$ td-agent-gem list|grep forest
fluent-plugin-forest (0.3.3)

td-agent.confを配布する

$ cat hello/.ebextensions/td-agent_conf.config
files:
    "/etc/td-agent/td-agent.conf" :
        mode: "000644"
        owner: root
        group: root
        content: |
            <source>
              @type tail
              format ltsv
              path /var/www/html/hello/storage/logs/monolog-%Y-%m-%d
              tag local.laravel.log
              pos_file /var/log/td-agent/laravel.log.pos
            </source>
            <filter local.**>
              @type record_transformer
              <record>
                host ${hostname}
              </record>
            </filter>
            <match local.**>
              @type forest
              subtype s3
              <template>
                aws_key_id アイディー
                aws_sec_key キー
                s3_bucket at.test.bucket
                s3_region ap-northeast-1
                path laravel/dt=%Y-%m-%d-%H/
                buffer_path /var/log/td-agent/buffer/laravel.buf
                time_slice_format %Y-%m-%d
                time_slice_wait 10s
                flush_interval 10s
                format ltsv
              </template>
            </match>

ebコマンドを用いたデプロイ

https://docs.aws.amazon.com/ja_jp/elasticbeanstalk/latest/dg/eb-cli3-configuration.html#eb-cli3-artifact

ざっくりイメージ

  • jenkinsでビルド
  • S3アップロード
  • ebコマンドでローリングデプロイ
ebコマンド使ってみる

ebコマンドインストール

本家にあるので省略

使ってみる

eb init

[centos@ip-172-31-10-238 ~]$ eb init

Select a default region
1) us-east-1 : US East (N. Virginia)
2) us-west-1 : US West (N. California)
3) us-west-2 : US West (Oregon)
4) eu-west-1 : EU (Ireland)
5) eu-central-1 : EU (Frankfurt)
6) ap-south-1 : Asia Pacific (Mumbai)
7) ap-southeast-1 : Asia Pacific (Singapore)
8) ap-southeast-2 : Asia Pacific (Sydney)
9) ap-northeast-1 : Asia Pacific (Tokyo)
10) ap-northeast-2 : Asia Pacific (Seoul)
11) sa-east-1 : South America (Sao Paulo)
12) cn-north-1 : China (Beijing)
13) cn-northwest-1 : China (Ningxia)
14) us-east-2 : US East (Ohio)
15) ca-central-1 : Canada (Central)
16) eu-west-2 : EU (London)
17) eu-west-3 : EU (Paris)
(default is 3): 9

Select an application to use
1) BGDeploy
2) sample-laravel
3) [ Create new Application ]
(default is 3): 2

Select a platform.
1) Node.js
2) PHP
3) Python
4) Ruby
5) Tomcat
6) IIS
7) Docker
8) Multi-container Docker
9) GlassFish
10) Go
11) Java
12) Packer
(default is 1): 2

Select a platform version.
1) PHP 7.1
2) PHP 7.0
3) PHP 5.6
4) PHP 5.5
5) PHP 5.4
6) PHP 5.3
(default is 1): 1
Cannot setup CodeCommit because there is no Source Control setup, continuing wit                                                           h initialization
Do you want to set up SSH for your instances?
(Y/n): Y

Select a keypair.
1) hoge-hogeo
2) [ Create new KeyPair ]
(default is 1): 1

eb health  →マネコンでみているようなリストが出力された f:id:hoge-hogeo:20180928173243p:plain

memo

aws elasticbeanstalk describe-application-versions

マネコンの「アプリケーションバージョン」で見れるやつ

$ aws --profile eb-cli --region ap-northeast-1 elasticbeanstalk describe-application-versions
{
    "ApplicationVersions": [
        {
            "ApplicationName": "sample-laravel",
            "Status": "UNPROCESSED",
            "VersionLabel": "modify_healthcheck_path",
            "Description": "",
            "ApplicationVersionArn": "arn:aws:elasticbeanstalk:ap-northeast-1:57142996XXXX:applicationversion/sample-laravel/modify_healthcheck_path",
            "DateCreated": "2018-07-19T03:16:14.455Z",
            "DateUpdated": "2018-07-19T03:16:14.455Z",
            "SourceBundle": {
                "S3Bucket": "elasticbeanstalk-ap-northeast-1-57142996XXXX",
                "S3Key": "2018200EjT-hello.zip"
            }
        },

コマンドラインでデプロイしたい

アプリケーションのバージョンを作成
$ aws --profile eb-cli --region ap-northeast-1 elasticbeanstalk create-application-version \
--application-name sample-laravel --version-label 201807201700 \
--source-bundle S3Bucket="__bucket_name__",S3Key="hello.zip" --auto-create-application
{
    "ApplicationVersion": {
        "ApplicationName": "sample-laravel",
        "Status": "UNPROCESSED",
        "VersionLabel": "201807201700",
        "ApplicationVersionArn": "arn:aws:elasticbeanstalk:ap-northeast-1:571429965935:applicationversion/sample-laravel/201807201700",
        "DateCreated": "2018-07-20T08:19:43.496Z",
        "DateUpdated": "2018-07-20T08:19:43.496Z",
        "SourceBundle": {
            "S3Bucket": "__bucket_name__",
            "S3Key": "hello.zip"
        }
    }
}

無事登録された。

f:id:hoge-hogeo:20180928173404p:plain

環境へのデプロイ

201807251600をデプロイする

$ aws --profile eb-cli --region ap-northeast-1 elasticbeanstalk update-environment --environment-name Laravel-env --version-label 201807251600
{
    "ApplicationName": "laravel",
    "EnvironmentName": "Laravel-env",
    "VersionLabel": "201807251600",
    "Status": "Updating",
    "EnvironmentArn": "arn:aws:elasticbeanstalk:ap-northeast-1:571429965935:environment/laravel/Laravel-env",
    "PlatformArn": "arn:aws:elasticbeanstalk:ap-northeast-1::platform/PHP 7.1 running on 64bit Amazon Linux/2.7.1",
    "EndpointURL": "awseb-AWSEB-6T78TTLA31X-XXXXXXXXX.ap-northeast-1.elb.amazonaws.com",
    "SolutionStackName": "64bit Amazon Linux 2018.03 v2.7.1 running PHP 7.1",
    "EnvironmentId": "e-axab792pcj",
    "CNAME": "Laravel-env.XXXXXXXXXX.ap-northeast-1.elasticbeanstalk.com",
    "AbortableOperationInProgress": true,
    "Tier": {
        "Version": "1.0",
        "Type": "Standard",
        "Name": "WebServer"
    },
    "Health": "Grey",
    "DateUpdated": "2018-07-25T06:53:38.215Z",
    "DateCreated": "2018-07-25T02:01:56.666Z"
}

f:id:hoge-hogeo:20180928173934p:plain

アプリケーションバージョンについて

いくらバージョンラベルを切っても、ソースの「アプリ.zip」のパスが一緒だと最新のものが参照されるらしい。(当然といえば当然か)

下記の画像だと、20180725hhmmのバージョンラベルが3つあるが、ソースのパスはS3上で同じパスなので、

最古のバージョンラベルに紐づくソースは最新のバージョンラベルのものと同じになる。

f:id:hoge-hogeo:20180928174026p:plain

requirement.txtってどこで実行されてるの?

command:にtouch hogeとか入れてみる

$cat /var/www/html/hello/requirement.txt
commands:
  command block:
    command: |
      touch hoge
      "curl -L \"https://toolbelt.treasuredata.com/sh/install-amazon1-td-agent3.sh\" > td-agent_install.sh"
      sh td-agent_install.sh


ここがrequirement.txtが読み込まれ、実行されるpwdらしい

$ sudo find / -type f -name hoge
/opt/elasticbeanstalk/eb_infra/hoge