SRE 㧠Microservices ãæ¨é²ãã¦ãã @b4b4r07 ã§ãã
ã¡ã«ã«ãªã§ã¯å ¨ç¤¾ (US/UK/JP) çã« Microservices ã«èµãåãçµå¶æéãæã¡åºããã¦ãããMicroservices Platform Team ã§ã¯ Microservices ã¨ãã¦åãåºãã«ãµãããããµã¼ãã¹ã®åç·¨ã®ãµãã¼ãããæ°è¦ãµã¼ãã¹ã® Microservices åã®ãµãã¼ããããã³ãã®ã¹ã¿ã³ãã¼ããªã¤ã³ãã©åºç¤ã®éçºãªã©ããã¦ãã¾ãã
æ¬è¨äºã§ã¯ãã®ä¸ã§éçºãã Developer Productivity ã®åä¸ã«ã¤ãªããå°ããªãã¼ã«ããã¡ã«ã«ãªã§ã® Terraform ã®æ´»ç¨äºä¾ã«äº¤ãã¦ãç´¹ä»ãã¾ãã
ã¡ã«ã«ãªã§ã® Terraform æ´»ç¨
åé ã«æããã¨ãããå°ããã¤ãããããªãµã¼ãã¹ãç«ã¡ä¸ããå§ãã¦ãã¾ããããã®ã¤ã³ãã©ã¨ãã¦ä¸»ã« GCP (GKE) ã使ããã¦ãã¾ããéç¨ãã GKE ã¯ã©ã¹ã¿ã¯1ã¤ã§ãããå種 Microservices ãå©ç¨ãã GCP ããã¸ã§ã¯ãã¯ãµã¼ãã¹ã®æ°ã ãä½ãããã«ãã¦ãã¾ãããã㯠Microservices ãæ²ããç¬ç«æ§ã®æå³ãå ¼ãã¦ããµã¼ãã¹åä½ã§ GCP ã®ããã¸ã§ã¯ããåããããã«å種ãµã¼ãã¹ã«å¿ è¦ãª GCP ãªã½ã¼ã¹ (Cloud SQL ãªã©) ãä½ã£ã¦ãããããã«ãã¦ãã¾ãã
ãããã®çµ±ä¸çãªä½æãå¿ è¦ãªãªã½ã¼ã¹ã»ãã (GCP ããã¸ã§ã¯ããPagerDuty ãªã©) ã®ä½æããä»ã¯ Terraform ã使ã£ã¦å®ç¾ãã¦ãã¾ãã
CI çµç±ã§ plan
ãã
ãããã®ä½æãå¤æ´ãé©ç¨ããããã«ãMicroservices Platform Team ã§ã¯ç¶ç¶ç㪠Terraform ã³ãã³ãã®å®è¡ç°å¢ã¨ã㦠CI ãå©ç¨ãã¦ãã¾ããåãµã¼ãã¹ã®éçºè
ã«ãã£ã¦æãããããã«ãªã¯ã¨ã¹ãã§ã¯ãå¤æ´ããã Microservices ã®ã¿ã«å¯¾ãã¦ãã©ã³ãã®æ®µé㧠fmt
㨠plan
ãå®è¡ããmaster ã«ãã¼ã¸ãããã¿ã¤ãã³ã°ã§ apply
ãå®è¡ãã¾ãã
ãã®ã¨ãã¬ãã¥ã¼ã®ã¿ã¤ãã³ã°ã§æ£ãã plan
ãéã£ãããã©ããªå®è¡è¨ç»ãçµæã¨ãã¦åºãã®ãã®ç¢ºèªã®ããã« CI ã®ãã¼ã¸ã«é·ç§»ããå¿
è¦ãããã®ã§ãããã¬ãã¥ã¼ãã¹ããã«ãªã¯ã¨ã¹ããå¢ãããã³ã«æ¯å CI ãã¼ã¸ã«ã¦ç¢ºèªãã¦ããæéã«ãªã£ã¦ãã¦ãã¾ãã *1ã
ããã§ãplan
çµæãªã©ã¯ GitHub ã®ã³ã¡ã³ãã¨ãã¦ç¢ºèªã§ããã»ããå§åçã«ä¾¿å©ã ãããã¨ãããã¨ã§è¯ã解決æ¹æ³ã¯ãªããæ¢ãã¦ã¿ã¾ãããããããããããªéè¦ãªã®ãå
¨ç¶äºä¾ãã¿ã¤ããã (ã§ãå社ãã£ã¦ããã¯ã…)ãããããã¦ãCI ã®çµæã GitHub ã«ã³ã¡ã³ããã¦ãããã¨ããè¨äºãè¦ã¤ããã¨ãã¦ãããã©ããã£ã¦ããããã¯ã©ããªãã¼ã«ã使ã£ã¦ GitHub ã Slack ãªã©ã«éç¥ããã¦ããã®ãããããããèªåãã¡ã§è§£æ±ºããå¿
è¦ãã§ã¦ãã¾ããã
tfnotify ã®ç´¹ä»
ããã§ã¡ã«ã«ãªã§ã¯ãTerraform ã³ãã³ãã®å®è¡çµæããã¼ã¹ããä»»æã®ãã³ãã¬ã¼ãã«æå½¢ã㦠GitHub ã Slack ãªã©ã«ã³ã¡ã³ãããããã¼ã«ãæ¸ãã¾ãã (å è¡äºä¾ã®ç´¹ä»ããªãèªåãã¡ãå°ã£ããã¨ããããæ¬ãã¼ã«ã¯ OSS ã¨ãã¦å ¬éãããã¨ã«ãã¾ãã)ã
https://github.com/mercari/tfnotify
詳ãã使ãæ¹ãªã©ã¯ README ã«è¼ãã¦ãã¾ãããæ¬ç¯ã§ã¯ã¡ã«ã«ãªã§ã®å©ç¨ã·ã¼ã³ã¨åããã¦ãç´¹ä»ãã¾ãã
使ãæ¹
tfnotify 㯠Go ã§æ¸ããããã¤ããªã«ãªã£ã¦ãããTerraform ã®å®è¡çµæããã¤ãã§ç¹ãã§å®è¡ããããã¨ãæå¾ ãã¾ããå é¨çã« io.TeeReader ã使ã£ã¦ããããããããã¨ã§é常éã CI ã³ã³ã½ã¼ã«ã®æ¨æºåºåã«æµãã¤ã¤ãtfnotify ã§å®è¡çµæãåãåããã¨ãã§ãã¾ãã
以ä¸ã®ããã«å®è¡ãããã¨ãå¯è½ã§ããããããã¦ãã¾ã㨠Terraform ãåºãæ¨æºåºåããã¹ã¦ãã¡ã¤ã«ã«åãã¦ããã®ã§ãCI ã®ã³ã³ã½ã¼ã«ã«ã¯ä½ãæµãã¾ããã
$ terraform plan > plan.log $ cat plan.log | tfnotify plan
tee
ã³ãã³ãã使ã㨠CI ã³ã³ã½ã¼ã«ã«æµãã¤ã¤ããã¡ã¤ã«ã«ãåºåã§ãã¾ãããä¸æãã¡ã¤ã«ãä½ãã®ãä¸è¦ã§ã¯ããã®ã§ããã㯠tfnofity å´ã§å®è£
ã¨ãã¦çµã¿è¾¼ãã§ãã¾ã (tee
ã§ãã¡ã¤ã«ã¸æåããããåºåã® ANSI Color ãåãé¤ãããããå¿
è¦ãããã¾ã)ã
ãã£ã¦ä»¥ä¸ã®1è¡ã§æ¸ã¿ã¾ãã
$ terraform plan | tfnotify plan
ãã㯠CI ãã¼ã¸ã§ç¢ºèªãããã¨ãä»®ã«ãã£ãã¨ãã«ãCI ã³ã³ã½ã¼ã«ã«ä½ãåºåããã¦ããªãã¨ããã¯ããã§å°ãããã ãªã¨ããã¨ããããããã®ãããªå®è£ ã«ãªãã¾ããã
tfnotify ããã£ã¦ãããã¨ã¯ä»¥ä¸ã®3ç¹ã§ãã
- Terraform ã®å®è¡çµæããã¼ã¹
- Go ã®ãã³ãã¬ã¼ãã«è½ã¨ãè¾¼ã
- ä»»æã®éç¥å ã«ãã¹ããã
éç¥å ã«æå®ã§ããã®ã¯ãã¾ã®ã¨ãã GitHub 㨠Slack ã§ããå®è¡ã§ãã CI 㯠Circle CI 㨠Travis CI ã§ãã
GitHub ã¸ã®éç¥
tfnotify ã§ã¯ YAML ã§ç°¡åãªè¨å®ãæ¸ããã¨ãã§ãã¾ããTerraform ã³ãã³ããå®è¡ãããªãã¸ããªã«å
¥ãã¦ããã°ããã§ãã$GITHUB_TOKEN
ãªã©ã®ãã¼ã¯ã³ã¯ç´æ¥ãã¡ã¤ã«ã«æ¸ãå¿
è¦ã¯ããã¾ããããããããã CI ã® Environment Variables ããè¨å®ãã¦ããå¿
è¦ãããã¾ãã
--- ci: circleci notifier: github: token: $GITHUB_TOKEN repository: owner: "mercari" name: "tfnotify" terraform: plan: template: | {{ .Title }} {{ .Message }} {{if .Result}} <pre><code> {{ .Result }} </pre></code> {{end}} <details><summary>Details (Click me)</summary> <pre><code> {{ .Body }} </pre></code></details>
ãã³ãã¬ã¼ãã§ã¯ Terraform ã®å®è¡çµæãä»»æã® HTML ã«æå½¢ãããã¨ãã§ãã¾ãããã®ä¾ã§ã¯ plan
çµæã以ä¸ã®ãããªã¹ã¿ã¤ã«ã«ãã¦æ稿ãããã¨ãã§ãã¾ãã
åãã¬ã¼ã¹ãã«ãã¯ä»¥ä¸ã®æå³ãæã¡ã¾ãã
ãã¬ã¼ã¹ãã«ã | æå³ |
---|---|
{{ .Title }} |
tfnotify plan ã®å ´åãããã©ã«ãã 㨠## Plan result |
{{ .Message }} |
--message ãªãã·ã§ã³ã§ã³ãã³ãã©ã¤ã³ããè¨å®ã§ããæåå |
{{ .Result }} |
Plan: 1 to add ã No changes ãªã©ã®å®è¡çµæã®æ½åºé¨å |
{{ .Body }} |
æ¨æºå ¥åã§åãåãå®è¡çµæã®å ¨ä½ |
å®è¡çµæã®å
¨æã¯é·ããªããã¡ãªã®ã§ <details>
ã使ã£ã¦ç³ãã§ãã¾ã£ã¦ãã¾ãããããããã¨ã§éç¥ãä½åããã¦ãã³ã¡ã³ãæ¬ãè¦ã¥ãããªããªãããã«ãªã£ã¦ãã¾ãã
ã¾ããåãã¿ã¤ãã« {{ .Title }}
ã¨åãã¡ãã»ã¼ã¸ {{ .Message }}
ãæã¤ã³ã¡ã³ããæ稿ãããå ´åãå¤ããã®ã¯ããªã¼ãããã¦ææ°ã®ã³ã¡ã³ãã ãã«ãªãããã«ãªã£ã¦ãã¾ããããã¯ä½åº¦ã³ããããã¦ããã·ã¥ãã¦ãåãéç¥çµæã§ã³ã¡ã³ãæ¬ãåããããã¨ãé²ãã§ãã¾ãã
Slack ã¸ã®éç¥
åæ§ã« slack:
ã¨ã㦠Slack ç¨ã® notifier ãè¨å®ããã¨ãéç¥å
ãåãæ¿ãããã¨ãã§ãã¾ãã
-- notifier: slack: token: $SLACK_TOKEN channel: C8L3AM8B0 bot: tfnotify terraform: plan: template: | {{ .Message }} {{if .Result}} ``` <span class="synSpecial">{{</span> .Result <span class="synSpecial">}}</span> ``` {{end}} ``` <span class="synSpecial">{{</span> .Body <span class="synSpecial">}}</span> ```
--message
flag ã§åãåãæååããªãã¨ç©ºã«ãªãã¾ãã
ã¡ã«ã«ãªå ã§ã®ä½¿ããæ¹
ä»ã®æ®µéã§ã¯ Microservices ã«é¢ããä¸å¤®é権çãªãªãã¸ããªã§ãã¹ã¦ã® Terraform ã³ã¼ãã管çãã¦ãã¾ã (ãã ããMicroservices ãã¨ã«ãã£ã¬ã¯ããªãåãã¦ãããstate ã Microservices ãã¨ã«ç®¡çãã¦ãã¾ããplan
ã apply
ãå¤æ´ããã Microservices ã®ã¿ã«å®è¡ããã¾ããä¸å¤®é権çãã¤åæ£ç®¡çããã¦ãã¾ã)ãããã«ä¸ãã£ã¦ãããã«ãªã¯ã¨ã¹ã㯠Microservices Platform Team ã«ããã¬ãã¥ã¼ãè¡ãããã®ã§ãããå¤ããã©ã³ãã«å¯¾ã㦠terraform plan
ããã¦ãæå¾
ããçµæãå¾ãããªãã®ã§ã¹ãããããã¦ãã¾ããåæ§ã« tfnotify ã«ããéç¥ãã¹ããããã¾ã (以ä¸ã®ã¹ã¯ãªããã¯ã¤ã¡ã¼ã¸ã§ã)ã
set -e basedir="$1" if is_there_no_change "$basedir"; then echo "[INFO] No changes in $basedir" exit 0 fi if is_branch_behind_master; then echo "[INFO] Skipped because $current_branch is behind of master" exit 0 fi echo "[INFO] Running 'terraform plan' for changed dirs" for dir in $(changed_dirs "$basedir") do ./script/terraform-plan "$dir" | tfnotify plan --message "$dir" done
ã¾ãããããã Git ã®ãå ç¥è¿ããç¶æ ãé²ãããã« GitHub ã§ãã©ã³ãã«å¯¾ãã¦ä»¥ä¸ã®è¨å®ãæå¹ã«ãã¦ãã¾ãã
ãããããã¨ã§æ£ãã terraform plan
ã®çµæãåãåããã¨ãã§ãã¾ãã
ä»å¾
ç¾ç¶ãéç¥å 㯠GitHub 㨠Slackãåä½ç°å¢ã¯ Circle CI 㨠Travis CI ã ãã§ããããªã¼ãã³ã½ã¼ã¹ã«ããã®ã§ä»å¾ã¯ç¤¾å ã¦ã¼ã¹ã«ã¨ã©ã¾ããå¹ åºããã¤ç©æ¥µçã«ã¡ã³ããã³ã¹ãã¦ããããè¯ããªã¨æã£ã¦ãã¾ãã
è¿½è¨ (2018-09-11)
HashiCorp Meetup #3 DevOpsãæ¯ããä»è©±é¡ã®HashiCorpãã¼ã«ç¾¤ã«ã¤ãã¦ã«ã¦çºè¡¨ãã¾ããã
*1:å æã®ä¾ã ã¨1æ¥ã«5ã¤ä»¥ä¸ã®ãã«ãªã¯ã¨ã¹ããã¬ãã¥ã¼ã»ãã¼ã¸ããã¦ãã¾ã