<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Leo Chang on Medium]]></title>
        <description><![CDATA[Stories by Leo Chang on Medium]]></description>
        <link>https://medium.com/@cchangleo?source=rss-a79a0ba0c580------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*Vlu2QeJ-aUSUiDscUxpIZQ.jpeg</url>
            <title>Stories by Leo Chang on Medium</title>
            <link>https://medium.com/@cchangleo?source=rss-a79a0ba0c580------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 15:25:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cchangleo/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How Nginx proxy event to Pubsub?]]></title>
            <link>https://medium.com/@cchangleo/how-nginx-proxy-event-to-pubsub-9d9fe2658bb2?source=rss-a79a0ba0c580------2</link>
            <guid isPermaLink="false">https://medium.com/p/9d9fe2658bb2</guid>
            <category><![CDATA[pub-sub]]></category>
            <category><![CDATA[nginx]]></category>
            <category><![CDATA[mixpanel]]></category>
            <category><![CDATA[lua]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Leo Chang]]></dc:creator>
            <pubDate>Thu, 26 Mar 2020 09:03:38 GMT</pubDate>
            <atom:updated>2020-03-26T09:03:38.521Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong>前情提要:</strong><br><em>由於公司APP產品使用Mixpanel 去接收user event 做分析, 但是不得不說，當資料進到Mixpanel 後要取出有點麻煩且有限制，於是接到需求要讓前端Mixpanel SDK 收集到的event 先丟上Google Pubsub. 再做後續處理，試過使用Python 但有效能問題，最後想說試試沒有碰過的Nginx 去做。</em></blockquote><p>架構圖如下:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/757/1*qsI2tRhtWmngxrtqdUP80Q.jpeg" /></figure><p>當要使用Nginx 去做到轉發event data 到 google pubsub 發現並不是這麼簡單，於是搜尋了下，需要使用到Lua 模組去實現這功能。</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F4NeNfuH50xo6k%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fstorm-cut-1947-4NeNfuH50xo6k&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F4NeNfuH50xo6k%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="332" frameborder="0" scrolling="no"><a href="https://medium.com/media/44616d2c5cae8674eb8efd4dd8e484d5/href">https://medium.com/media/44616d2c5cae8674eb8efd4dd8e484d5/href</a></iframe><p>費了好一番工夫去了解Nging with Lua, Lua流程可以參考這張圖</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1005/1*YbKY7vZCVulXu3FT-W-j2A.png" /></figure><p>做之前先列出幾個需要解決的問題</p><p><em>由於client 端並不會有google credential, 所以Nginx 需要去handle 憑證問題<br>2. Lua 需要處理event 格式問題，確認是可以發送給Pubsub 的格式<br>3.如何http轉發到cloud pubsub <br>4. Nginx環境變數問題</em></p><blockquote>這一步去做轉發到Pubsub， set_by_lua 會去讀取開頭init_by_lua 中放置到內存的環境變數</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12c027ad80a6a4e019161d0e3b5b5639/href">https://medium.com/media/12c027ad80a6a4e019161d0e3b5b5639/href</a></iframe><blockquote>使用content_by_lua_block去處理event格式</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6db63fb43020da531c7a1b601c9b8e6a/href">https://medium.com/media/6db63fb43020da531c7a1b601c9b8e6a/href</a></iframe><p>Nginx 本身並不支援環境變數，所以這邊使用lua 的共享內存模塊及Nginx的自定義變數去做到讓docker-compose 可以讀取環境變數給Nginx， 當然網路上也有其他種方，但基於效能考量，為了減少每次client端request時候都要讀取env，這邊就乾脆直接讀進內存以利重複使用。另外需在原本nginx.conf下加入需要被docker-compose assign 的環境變數，這樣我們的default.conf才會讀取的到</p><pre>env GCLOUD_PROJCT_NAME;<br>env GCLOUD_TOPIC_NAME;</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/689015c2e39f996f190005001626f4e3/href">https://medium.com/media/689015c2e39f996f190005001626f4e3/href</a></iframe><p>至於怎麼使用，只要像這樣用get呼叫，event就會透過Nginx publish 到pubsub了</p><pre>curl http://123.123.123:8000/track?data=&quot;你的base64 encode資料&quot;</pre><p>最後附上基於Docker環境的程式碼，有興趣的可以研究如何使用nginx proxy data to pubsub. 不得不說nginx with lua 是個很強大的工具，如果你是熟悉C語言的，相信一定能輕易駕馭…</p><p><a href="https://github.com/cchangleo/nginx-lua-pubsub.git">cchangleo/nginx-lua-pubsub</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FfkPxDJ2bpZX7q%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fgame-fights-brienne-fkPxDJ2bpZX7q&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FfkPxDJ2bpZX7q%2F200.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="245" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa5b63ab552658cd9f13d41dcbb93523/href">https://medium.com/media/fa5b63ab552658cd9f13d41dcbb93523/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9d9fe2658bb2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[使用Drone測試Docker CI/CD結合Gitlab]]></title>
            <link>https://medium.com/@cchangleo/%E4%BD%BF%E7%94%A8drone%E6%B8%AC%E8%A9%A6docker-ci-cd%E7%B5%90%E5%90%88gitlab-a3021fd277cd?source=rss-a79a0ba0c580------2</link>
            <guid isPermaLink="false">https://medium.com/p/a3021fd277cd</guid>
            <category><![CDATA[continuous-deployment]]></category>
            <category><![CDATA[continuous-integration]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[gitlab]]></category>
            <category><![CDATA[drone-service]]></category>
            <dc:creator><![CDATA[Leo Chang]]></dc:creator>
            <pubDate>Thu, 21 Nov 2019 08:47:46 GMT</pubDate>
            <atom:updated>2019-11-21T08:47:46.122Z</atom:updated>
            <content:encoded><![CDATA[<p>由於Jenkins在使用上需要一定的codeing程度，所以取而代之當然使用較為簡單且易上手的Dron. 且Drone 也支援gitlab YAML file. 網上已有許多文章介紹Drone，這邊就不多做介紹。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IGIwDMV-uM-JOTQvecb4gQ.png" /></figure><p>這篇文章主要是測試使用Drone 且從gitlab push 自己的docker image 到Docker-hub。Drone also support multi plugin. <a href="http://plugins.drone.io/">http://plugins.drone.io/</a></p><p><strong>Here is the article about Drone.</strong></p><ul><li><a href="https://medium.com/@stepanvrany/drone-io-first-impressions-43d9338cd7e5">Drone.io: first impressions</a></li><li><a href="https://blog.drone.io/drone-1/">Announcing Drone 1.0</a></li></ul><p>Drone is a modern continuous integration system built with a containers-first architecture. Pipelines are configured using a special <a href="https://docs.drone.io/config/">YAML file</a> that you check-in to your git repository. The syntax is designed to be easy to read and expressive so that anyone using the repository can understand the continuous delivery process.</p><p>Example code from Drone:</p><pre>---<br>kind: pipeline<br>name: default<br><br>steps:<br>- name: backend<br>  image: golang<br>  commands:<br>  - go build<br>  - go test<br><br>- name: frontend<br>  image: node<br>  commands:<br>  - npm install<br>  - npm test</pre><p>Drone executes build, test and deployment commands against your code inside isolated containers. The major benefit of containerized testing environments is the portability of your testing infrastructure. With containers, Drone can automatically download, install and create your testing environment without having to either manually set up and maintain infrastructure or sacrifice environmental fidelity.</p><h3>Test Case :</h3><p>I use EC2 instance to install Drone. And using Drone connect to the Gitlab to do CI/CD.</p><p>The docker-compose file will install Drone on the single machine. And it also could build on multi machine.</p><p>Docker-compose file:</p><pre>version: &#39;2&#39;<br><br>services:<br>  drone-server:<br>    image: drone/drone:1.0.0-rc.1<br>    volumes:<br>      - /var/lib/drone:/data<br>      - /var/run/docker.sock:/var/run/docker.sock<br>    restart: always<br>    environment:<br>      - DRONE_SERVER_HOST=${DRONE_SERVER_HOST}<br>      - DRONE_SERVER_PROTO=http<br>      - DRONE_TLS_AUTOCERT=false<br>      - DRONE_RUNNER_CAPACITY=2<br>      # GitLab Config<br>      - DRONE_GITLAB_URL=https://gitlab.com<br>      - DRONE_GITLAB_CLIENT_ID=${DRONE_GITLAB_CLIENT_ID}<br>      - DRONE_GITLAB_CLIENT_SECRET=${DRONE_GITLAB_CLIENT_SECRET}<br>      - DRONE_LOGS_PRETTY=true<br>      - DRONE_LOGS_COLOR=true<br>    ports:<br>      - 80:80<br>      - 443:443</pre><p>Also, need to create .env file for the variable in docker-compose file. The Gitlab Key should apply from your gitlab account.</p><pre>DRONE_SERVER_HOST=xxx.xxx.com<br>DRONE_SERVER_PROTO=http<br>DRONE_GITLAB_CLIENT_ID= your token<br>DRONE_GITLAB_CLIENT_SECRET= your token</pre><p>Then run the docker-compose file and connect to the host. And enter to the host your will see like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O6yK3Taqwjh30kVEgodrow.png" /></figure><p>Now you can choose which repository you want to activate to CI/CD.<br>Inside the repository you need to create .drome.yml for the CI/CD pipeline.<br>Here is the example for build docker image base on the frontend environment and push it to the docker-hub.</p><pre>kind: pipeline<br>name: default<br><br><br>steps:<br>- name: docker-build<br>  image: plugins/docker<br>  settings:<br>    username: abcd123<br>    password: 123456<br>    repo: cchangleo/dronerrr<br>    tags: latest</pre><p>You should change to your username and password.<br>And you also can put the username and password on Drone setting.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X5tIFSyoPjLWGjKN1f_CWw.png" /></figure><p>Then each time when you commit and push your code to the gitlab, it will auto start to CI/CD.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z4GJMiMChkk6QkoOODP9Cg.png" /></figure><p>Now, it will auto deploy you docker image to Docker-Hub.</p><p>Don’t forget, you also can using SSH comment in the Drone to deploy your environment on instance.</p><p>對於Drone 有興趣的朋友們可到ApplyBoy大大的網站上有更詳細的介紹:</p><p><a href="https://blog.wu-boy.com/2019/04/cicd-drone-1-0-feature/">CI/CD 大亂鬥: Drone 1.0 功能介紹</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FxUA7bcuTndaPQ6jtew%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FxUA7bcuTndaPQ6jtew%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FxUA7bcuTndaPQ6jtew%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="435" frameborder="0" scrolling="no"><a href="https://medium.com/media/e51877aaf33c118d27088d3d9685dbe9/href">https://medium.com/media/e51877aaf33c118d27088d3d9685dbe9/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a3021fd277cd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[使用ECS CLI 部署docker-compose到AWS ECS]]></title>
            <link>https://medium.com/@cchangleo/%E4%BD%BF%E7%94%A8ecs-cli-%E9%83%A8%E7%BD%B2docker-compose%E5%88%B0aws-ecs-3a3a13b2494e?source=rss-a79a0ba0c580------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a3a13b2494e</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[aws-ecs]]></category>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Leo Chang]]></dc:creator>
            <pubDate>Fri, 05 Jul 2019 08:49:04 GMT</pubDate>
            <atom:updated>2019-07-05T08:49:04.527Z</atom:updated>
            <content:encoded><![CDATA[<p><em>身為打雜工最近剛好有需求部署docker-compose 到AWS ECS上 ，折騰好久才終於弄懂了一些 ，就藉此機會把用ECS-CLI 操作的詳細步驟給記錄下來。</em></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FgHtvqib1yFvNbsjwCs%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FgHtvqib1yFvNbsjwCs%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FgHtvqib1yFvNbsjwCs%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="362" frameborder="0" scrolling="no"><a href="https://medium.com/media/44c4b41b9b7416a5a51fe527e96aabf9/href">https://medium.com/media/44c4b41b9b7416a5a51fe527e96aabf9/href</a></iframe><h3><strong>AWS ECS Environment</strong></h3><p>在部署上ECS之前，建議大家可先參考下面幾張ECS的流程架構圖，會有更好的理解。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HJO3QAkocLe9zCFZhW1RGg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/757/1*FXhdBYcTnKw1IZCJgzFgBg.png" /></figure><p>簡單來說，要部屬到ECS前，我們需Cluster、Task Definition，其中，最為重要的是Task Definition，也相對複雜，但我們只是需要測試，所以會用到的就是些基本設定。</p><p><strong>根據官方解釋： </strong><em>您必須先建立任務定義，才可以在 Amazon ECS 中執行 Docker 容器。您可以在任務定義中定義多個容器和資料磁碟區。</em></p><p>下面兩張圖為解釋Task Definition 到底在定義些什麼</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mTB2sVVFzRQE_Ux3khtpGA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5rWjgzkMpRsjTmMUcstCcA.png" /></figure><p>另外，由於目前的ECS不支援docker-compose 中的build images, Volume 語法，所以我們必須先build image 然後push 上docker-hub or AWS container registry.</p><p><strong><em>OK, 那我們開始吧!</em></strong></p><p>這次的任務是使用node js + traefik 部署上ECS，因此source code會來自我已提前push 上docker-hub的兩個image，有需要source code 的話可參考<a href="https://github.com/cchangleo/Frontend-Docker-Test.git"><em>https://github.com/cchangleo/Frontend_Sample.git</em></a></p><p>Traefik dockerfile跟docker-compose 的source code</p><p><a href="https://github.com/cchangleo/ecs-traefik-master.git"><em>https://github.com/cchangleo/ecs-traefik-master.git</em></a><br>cchangleo/traefik:latest <br>cchangleo/frontend-test:latest</p><p>新建一個目錄，然後建立docker-compose file，如前面所說docker-compose 裡面的volume, build並不會在ECS起作用，這邊我們忽略或刪除volume，然後使用docker-hub 上的image</p><pre>version: &#39;3&#39; </pre><pre>services:</pre><pre>reverse-proxy:</pre><pre>image: cchangleo/traefik:latest</pre><pre>restart: always</pre><pre>container_name: traefik</pre><pre>ports:</pre><pre><strong>-</strong> &quot;80:80&quot; #Http port</pre><pre><strong>-</strong> &quot;8080:8080&quot; #The Web UI</pre><pre>#- &quot;443:443&quot;</pre><pre>networks:</pre><pre><strong>-</strong> traefik</pre><pre>command: traefik -c /etc/traefik.toml</pre><pre>volumes:</pre><pre><strong>-</strong> /var/run/docker.sock:/var/run/docker.sock</pre><pre><strong>-</strong> ./traefik.toml:/traefik.toml</pre><pre><strong>-</strong> ./logs:/data/logs</pre><pre><strong>-</strong> ./acme.json:/acme.json</pre><pre>app:</pre><pre>image: cchangleo/frontend-test:latest</pre><pre>ports:</pre><pre><strong>-</strong> 3000:3000</pre><pre>labels:</pre><pre><strong>-</strong> &quot;traefik.docker.network=traefik&quot;</pre><pre><strong>-</strong> &quot;traefik.enable=true&quot;</pre><pre><strong>-</strong> &quot;traefik.basic.frontend.rule=Host:app.test.infocast&quot;</pre><pre><strong>-</strong> &quot;traefik.basic.port=3000&quot;</pre><pre><strong>-</strong> &quot;traefik.basic.protocol=http&quot;</pre><pre>networks:</pre><pre>traefik:</pre><pre>external: true</pre><p>再來需要準備ECS-CLI 會需要讀取ecs-params.yml 檔，此文件內的定義其實就是ECS console上的task definition</p><p>create ecs-params.yml， 這邊我定義了基本配置，如需定義其他設定可以參考這邊</p><p><a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cmd-ecs-cli-compose-ecsparams.html">Using Amazon ECS Parameters</a></p><pre>version: <strong>1</strong></pre><pre>task_definition:</pre><pre>services:</pre><pre>reverse-proxy:</pre><pre>cpu_shares: <strong>100</strong></pre><pre>mem_limit: <strong>162144000</strong></pre><pre>app:</pre><pre>cpu_shares: <strong>100</strong></pre><pre>mem_limit: <strong>262144000</strong></pre><p>保存好上面文件後就可以開始Build了~~</p><p>使用ecs-cli 之前 記得先發摟官網上的安裝設定</p><p><a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ECS_CLI_installation.html">Installing the Amazon ECS CLI</a></p><p><strong>Let’s rolling</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*mN6mW1WbKa15D_qVNdazGw.jpeg" /></figure><p>首先，build 好ecs cluster，EC2 instance選擇了一台 t3.micro，當然你也可以擴展到你想要的數量，cluster name 設為&quot;docker-test” ，我也提前設定好了iam role，所以keypair 可以直接使用不用再指定要在哪一個區域</p><pre>ecs-cli up --keypair MyKeyPair --capability-iam --size 1 --instance-type t3.micro --cluster docker-test --force</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pdWIHWWbhFjjJw1R9iWcLg.png" /></figure><p>看到完成後，再來就是要啟動docker-compose (記得在你的docker-compose file 目錄下啟動)</p><pre>ecs-cli compose up --create-log-groups --cluster-config docker-test</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b5S2ZJt5_PGQ8j-ISMJuKw.png" /></figure><p>完成後再輸入ecs-cli ps 就會看到已經跑起來了</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W1hcq4Hru1D4GoAaJGQzRg.png" /></figure><p>這時候可以到ECS WEB Console 上去確認是否真得有跑起來。確實兩個docker container 都有跑起，這時候只要再去ec2 的inbond rule打開需要的8080 &amp; 3000 port 就能看到traefik 跟node js的畫面了。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lvXjyzPN4Rf_XLQmx4TFuw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bHDeUpQDhHvind4EDIzVdw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v46bTxTFchVHiV3vbkErpg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TBWfIGI3GpM36_wJ16-l7w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XExjpiqqbba7hC0FeXJcwA.png" /></figure><p>最後~不要忘記需要清除我們的ECS環境，避免被收費， 一樣使用ecs-cli 指令操作。 這次先deregister task definition 再去刪除container 跟cluster</p><pre>aws ecs deregister-task-definition --task-definition ecs-traefik-master:6</pre><p>ecs-traefik-master:6 代表我們上傳的task definition版本號</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u_ecHhkJdSuEccbJ2RM5xA.png" /></figure><p>Shut down docker-compose</p><pre>ecs-cli compose down --cluster docker-test --cluster-config docker-test </pre><p>最後刪除EC2， 記得再次到ec2確認</p><pre>ecs-cli down</pre><p>完美的結束從deploy 到 刪除ECS 環境</p><h3><strong>總結</strong></h3><p>如果是初次使用的話記得要先設定好ecs-cli, iam role 跟keypair，這樣會方便許多，另外比較容易遇到的問題還是到底task definition 該如何定義，這也是我目前一直在嘗試了解的。如果你是偏好使用WEB 介面去deploy 也是可以的。 最後最後推薦如果想要更detail 了解ECS，可以去上此課程<a href="https://www.udemy.com/aws-ecs-deep-insight/">https://www.udemy.com/aws-ecs-deep-insight/</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F26AHC0kdj8IeLkmBy%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fend-fin-the-26AHC0kdj8IeLkmBy&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F26AHC0kdj8IeLkmBy%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="386" frameborder="0" scrolling="no"><a href="https://medium.com/media/11b12a3247736800307133ddca9152e2/href">https://medium.com/media/11b12a3247736800307133ddca9152e2/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a3a13b2494e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Airflow with Docker 容器部署 — part2]]></title>
            <link>https://medium.com/@cchangleo/airflow-with-docker-%E5%AE%B9%E5%99%A8%E9%83%A8%E7%BD%B2-part2-8ddb83dc2d4a?source=rss-a79a0ba0c580------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ddb83dc2d4a</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[data-engineering]]></category>
            <category><![CDATA[data-pipeline]]></category>
            <category><![CDATA[airflow]]></category>
            <category><![CDATA[etl]]></category>
            <dc:creator><![CDATA[Leo Chang]]></dc:creator>
            <pubDate>Tue, 26 Mar 2019 07:24:12 GMT</pubDate>
            <atom:updated>2019-04-02T08:07:55.694Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bF-mCJh4TCh9MvY1M97DRQ.jpeg" /></figure><h3>Airflow with Docker 容器部署 — part 2</h3><p>在前一篇文章從頭到尾的安裝了Airflow, 這次基於未來考量的擴展性跟管控性，決定也試試使用Docker 安裝Airflow。網路上已有現成的Docker包，在這邊我們只做些微的調整。(安裝環境依然是在Ubuntu上)</p><p>This is the setting I already changed and update to the git that you can just download to use it.</p><blockquote>git clone <a href="https://github.com/cchangleo/docker-airflow.git">https://github.com/cchangleo/docker-airflow.git</a></blockquote><p>Here are few steps we will follow:</p><ol><li>Installing Docker</li><li>Install Docker-Compose</li><li>Getting Docker-Airflow from git</li><li>Dockerfile setting</li><li>docker-compose setting</li><li>Start all service</li><li>Testing</li></ol><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FgOkawaguYNiSI%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fagree-nod-robert-redford-gOkawaguYNiSI&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FgOkawaguYNiSI%2F200.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="181" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0bb12d2b27a7da695d6d9a9ae5ff1e6/href">https://medium.com/media/d0bb12d2b27a7da695d6d9a9ae5ff1e6/href</a></iframe><p>If you don’t know what is Docker before, just like me, here are few websites can let you know more about Docker.</p><ul><li><a href="https://github.com/twtrubiks/docker-tutorial">GitHub - twtrubiks/docker-tutorial: Docker 基本教學 - 從無到有 Docker-Beginners-Guide 教你用 Docker 建立 Django + PostgreSQL 📝</a></li><li><a href="https://yeasy.gitbooks.io/docker_practice/content/">前言 · Docker -- 从入门到实践</a></li></ul><p>OK, let’s install Docker</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FxT5LMyWnCCvfKyD3PO%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fseason-3-the-simpsons-3x18-xT5LMyWnCCvfKyD3PO&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FxT5LMyWnCCvfKyD3PO%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="331" frameborder="0" scrolling="no"><a href="https://medium.com/media/eecc58ac99ccf84b4751d65a16f82569/href">https://medium.com/media/eecc58ac99ccf84b4751d65a16f82569/href</a></iframe><p><strong>安裝Docker CE</strong></p><pre>sudo apt-get update<br><br>sudo apt-get install docker-ce</pre><pre><em>#將當前用户加入docker組</em><br>sudo gpasswd -a ${USER} docker  <br>sudo systemcl enable docker <br>sudo systemcl restart docker  <br>docker ps</pre><p><strong>安裝Docker-Compose</strong></p><pre>sudo curl -L &quot;https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)&quot; -o /usr/local/bin/docker-compose</pre><pre><em>#給予執行權限</em></pre><pre>sudo chmod +x /usr/local/bin/docker-compose</pre><pre>sudo pip install docker-compose  </pre><pre><em>#檢查版本</em><br>docker-compose version</pre><p>So far so good, now we need to pull the image from the Docker repository.</p><pre>docker pull puckel/docker-airflow</pre><pre>#or get from git</pre><pre>git clone https://github.com/puckel/docker-airflow.git</pre><p>由於希望Docker airflow 的架構可以像這樣擴展，在這邊需要先修改下原本的dockerfile 與剛剛我們clone下來的docker-compose-CeleryExecutor.yml</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/689/0*LkVLDdgd3nb1lE6P.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/921/1*3p92WDmnf94QINq4ZX_bjw.png" /></figure><h4>Docker部署Airflow CeleryExecutor模式</h4><pre>cd docker-airflow</pre><p><strong>修改Dockerfile</strong><br><em>主要修改了redis安裝版本與幾個會用到的pip包，還有airflow 需要的資源</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3495fd53fafeebc9a77804dcd80d15f3/href">https://medium.com/media/3495fd53fafeebc9a77804dcd80d15f3/href</a></iframe><p><strong>修改 docker-compose-CeleryExecutor.yml</strong></p><p>這邊主要把airflow run job時會要用到的目錄都先掛載上去，可以在worker/scheduler 那邊添加，如果DB資料需要留存 也建議把command拿掉 才不會當刪除Docker時, docke裡db資料也不見。另外在這邊將使用postgres + redis + celery +flower。</p><p>默认的compose是抓原本版本的鏡像包，這邊記得都改成latest: 這樣才會抓取我們剛剛修改過後的Docker file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*qwsrQ4NHNy6C46rdzERyHQ.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea218486de2d32d34a6dbf7c70dea5e1/href">https://medium.com/media/ea218486de2d32d34a6dbf7c70dea5e1/href</a></iframe><p><strong>Airflow config 設定</strong></p><p>由於我們是要佈署Celery 在airflow, 這邊也需要修改airflow.config</p><pre>cd config<br>vi airflow.cfg</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/848/1*4yQwYJ4ka9s1a6PfaLWCKQ.png" /><figcaption>executor 改為CeleryExecutor</figcaption></figure><p>Now looks great, we can run dockerfile first, after need to run compose file.</p><pre>docker build --rm -t puckel/docker-airflow .</pre><pre>docker-compose -f docker-compose-CeleryExecutor.yml up -d</pre><p>Test it, now you should see like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FGMV3URO8mHNJVb2qF5p5w.png" /></figure><p>Here are some scripts let you control your docker easily. And if you need to delete the docker and change some setting, you need to stop docker compose then clean docker compose then clean the docker images.</p><pre># 啟動<br>docker-compose -f docker-compose-CeleryExecutor.yml up -d</pre><pre># 停止<br>docker-compose -f ./docker-compose-CeleryExecutor.yml stop</pre><pre># Celery and worker 擴展<br>docker-compose -f docker-compose-CeleryExecutor.yml scale worker=3 docker-compose -f docker-compose-CeleryExecutor.yml scale scheduler=3</pre><p>使用airflow 命令行的範例</p><pre>docker-compose -f docker-compose-CeleryExecutor.yml run --rm webserver airflow list_dags</pre><p>Now we can change the connection setting on airflow browser and check if celery and flower are working. (default setting for flower is localhost:5555)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KbTBRPXn21XUJKsocetrBw.png" /><figcaption>localhost:8080</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fwOQX74_t3PzCp2p9BdjcQ.png" /><figcaption>localhost:5555</figcaption></figure><p>Finally 大功告成，之後我們只需要把要跑得dag放在設定下的目錄就可以跑了~~ 在這邊我用的DAG範例來源如下:</p><p><a href="https://leemeng.tw/a-story-about-airflow-and-data-engineering-using-how-to-use-python-to-catch-up-with-latest-comics-as-an-example.html">一段 Airflow 與資料工程的故事：談如何用 Python 追漫畫連載</a></p><p>(已修改上述文中的爬蟲方式及某些設定)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RCUUKwnbdd9_chuZr5pNkQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D6VXm8EyEJ97yCIcYeNWcw.png" /><figcaption>成功跑在兩台workers</figcaption></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3o7TKnvDNYADdLYZIQ%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fart-design-illustration-3o7TKnvDNYADdLYZIQ&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3o7TKnvDNYADdLYZIQ%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="435" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd583fbc8f0f94f3c7e1f7389973fe7f/href">https://medium.com/media/dd583fbc8f0f94f3c7e1f7389973fe7f/href</a></iframe><p>Refreence:<br><a href="https://github.com/puckel/docker-airflow">https://github.com/puckel/docker-airflow</a><br><a href="https://medium.com/bluecore-engineering/were-all-using-airflow-wrong-and-how-to-fix-it-a56f14cb0753">https://medium.com/bluecore-engineering/were-all-using-airflow-wrong-and-how-to-fix-it-a56f14cb0753</a><br><a href="http://site.clairvoyantsoft.com/setting-apache-airflow-cluster/">http://site.clairvoyantsoft.com/setting-apache-airflow-cluster/</a><br><a href="https://github.com/apachecn/airflow-doc-zh/blob/master/zh/26.md">https://github.com/apachecn/airflow-doc-zh/blob/master/zh/26.md</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ddb83dc2d4a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Airflow 新手建置測試-Part 1]]></title>
            <link>https://medium.com/@cchangleo/airflow-%E6%96%B0%E6%89%8B%E5%BB%BA%E7%BD%AE%E6%B8%AC%E8%A9%A6-part-1-9e0fe4d12e7a?source=rss-a79a0ba0c580------2</link>
            <guid isPermaLink="false">https://medium.com/p/9e0fe4d12e7a</guid>
            <category><![CDATA[airflow]]></category>
            <category><![CDATA[big-data]]></category>
            <category><![CDATA[etl]]></category>
            <category><![CDATA[datepipe]]></category>
            <category><![CDATA[ubuntu]]></category>
            <dc:creator><![CDATA[Leo Chang]]></dc:creator>
            <pubDate>Tue, 26 Feb 2019 09:30:35 GMT</pubDate>
            <atom:updated>2019-03-21T09:20:13.849Z</atom:updated>
            <content:encoded><![CDATA[<h3>Airflow 建置測試-Part 1</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5NM9hHD8J1FhCz84uy_8dw.jpeg" /></figure><p><em>由於一直以來公司所用的ETL工具皆是像SSIS, Trinity 這樣類型的, 對於開源的Airflow 並沒有接觸過。在換了份新工作後，剛好有機會去試像Airflow 這類的 ETL工具。</em></p><p><em>在屢次安裝Airflow 失敗後，終於在Amazon Ubuntu 上安裝成功 Airflow 。為了不忘記這痛苦的過程 ，就藉此寫下較詳細的安裝步驟給同樣像我這樣的小白參考參考。</em> <em>(簡稱Airflow 踩坑記)</em></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F2vlmXyWEEDm7korr7N%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fok-k-you-got-it-2vlmXyWEEDm7korr7N&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F2vlmXyWEEDm7korr7N%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="235" frameborder="0" scrolling="no"><a href="https://medium.com/media/ecd9f3899d6f74f6b824ad48eb8f5bd4/href">https://medium.com/media/ecd9f3899d6f74f6b824ad48eb8f5bd4/href</a></iframe><p>安裝環境 : Amazon EC2 — Ubuntu Server 18.04 LTS (HVM)</p><h3>Preparation List</h3><ul><li>Python 3.6</li><li>pip</li><li>Mysql-server</li><li>Mysql-client</li><li>libmysqlclient-dev</li><li>python-dev</li><li>airflow-apache</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FYLHwkqayc1j7a%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FYLHwkqayc1j7a%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FYLHwkqayc1j7a%2F200.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="260" frameborder="0" scrolling="no"><a href="https://medium.com/media/657a47571fda328576c2459a3715a7af/href">https://medium.com/media/657a47571fda328576c2459a3715a7af/href</a></iframe><h4>Installing Database Backend (Mysql)</h4><p>sudo apt-get install mysql-server</p><p>apt-get isntall mysql-client</p><p>sudo apt-get install libmysqlclient-dev</p><p><strong>Create airflow database</strong></p><p>On Airflow default setting is initdb, but official recommend using Mysql/Postgresql DB</p><p>mysql -u root</p><p>mysql&gt; create database airflow <strong>default</strong> charset utf8 collate utf8_general_ci;</p><p>mysql&gt; create user airflow@&#39;localhost&#39; identified by &#39;airflow&#39;;</p><p>mysql&gt; grant all on airflow.* to airflow@&#39;localhost&#39;;</p><p>mysql&gt; flush privileges;</p><p><strong>Install python environment</strong></p><p>sudo apt install python3-pip</p><p>sudo pip3 install --upgrade pip</p><h3>Airflow Installation</h3><p>pip install MySQL<strong>-</strong>python</p><h4>Set Up Airflow Default Home</h4><p>mkdir -p ~/airflow</p><p>export AIRFLOW_HOME=~/airflow</p><p>export SLUGIFY_USES_TEXT_UNIDECODE=yes</p><h4>Installing Airflow</h4><p>pip install &quot;airflow[postgres, mysql, celery, rabbitmq, slack, crypto]&quot;</p><h4>Check <a href="https://airflow.incubator.apache.org/installation.html#extra-packages">here </a>or more details on the list of the sub-packages and what they enable.</h4><p>After successful installation, wee need to start Airflow’s database. Airflow requires a database to be initiated before you can run tasks.</p><p>The default db setting is SQLite, but in here we will change to use Mysql database. If in your airflow folder cannot find airflow.cfg file, you need to start airflow initdb first.</p><p>airflow initdb</p><p>The command will create airflow.cfg file under the airflow folder.</p><p><em>Note: Make sure to have specified explicit_defaults_for_timestamp=1 in your my.cnf under [mysqld] or like this</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*snJUWLXAk9L6-iu9StieWQ.png" /></figure><p><strong>Edit airglow.cfg file to change to Mysql database</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z58AH830PsgPm7eYIKXOag.png" /></figure><p><strong>Initiating Airflow Database</strong></p><p>airflow initdb</p><p>Now, your airflow database is changing to Mysql.</p><p><strong>Starting Airflow web server</strong></p><p><em>Before start the webserver, make sure your Default Home is correct. Enter this script on the airflow folder.</em></p><p><em>export AIRFLOW_HOME=~/airflow</em></p><p>airflow webserver -p 8080</p><p><strong>Starting Airflow Scheduler</strong></p><p>airflow scheduler</p><p>Now open the website: <em>localhost:8080</em> you will see the airflow web</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k_TvmQO4wIaP1GfRN0ig9Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ptAfzDodd3GFPLS05da2xA.png" /></figure><p>Finally, we successful installed Airflow, and there are some function you might want to change in the airflow.cfg file. In the next topic, I will cover how to build Airflow Docker in 10 min and what else we have to change in airflow.cfg file.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e0fe4d12e7a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>