Tina
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
###### tags: `LSA2` [toc] # 在雲端做部屬自動化( Infrastructure as Code ) <!-- https://cloud.netapp.com/blog/cloud-automation-why-where-and-how-cvo-blg https://blog.cti.app/archives/29816 --> ## 自動化 - 以人力以外的資源,例如機械、水力、電力等能源,代替人類操縱、控制及監視設備或程式,完成重複性的工作。 - 優點:節省人力及時間、降低人為錯誤機率、減少人員暴露在危險的環境中,並提升產品品質... - 例子:自駕車、大樓的空調管控、銀行轉帳自動化等。 ## 在雲端做自動化由來 - 在 IT 基礎設施全面搬上雲端的趨勢下,在雲端上手工創建的方式已難以繼續下去,出現了以下缺點 - 管理困難 - 當要更新特定資源的規格等操作時,手動運維的方式就導致沒有統一管理這批資源的入口,仍需要分別到各資源控制檯上操作。隨著資源數越來越多,資源管理就愈難以維護。 - 部署效率低 :::info - 部屬:雲端基礎架構的配置以及所需軟體的安裝。 - 雲端基礎架構:跟典型的資料中心一樣,只不過被虛擬化並被作為服務提供消費者。例如:伺服器、儲存空間、網路... ::: - 涉及到大量不同資源時,會發現需要在不同的資源控制檯之間來回切換,尤其是當資源之間還有相依性,還要時刻關注創建進度,才能再去創建下一個依賴它的資源。 :::info - 相依性 - 部屬 ( CloudDeploy )的順序 1. 網路與區塊儲存空間的準備 2. 從伺服器的映像啟動 3. 作業系統的安裝 5. 應用程式的部屬 ::: - 可複製性差 - 無法直接複製,無法做到一鍵部署、執行。 - 一致性差 - 非常容易出現配置錯誤,很難保證兩套環境中各個資源配置是完全相同的。 - 難以 DevOps - 每次開發、測試或部署軟體應用程式時都可能需要手動部署基礎設施,無法對基礎設施進行版本控制,也無法對其變動進行評斷(如評斷變動後的優劣、請變動員工負責...) :::info - DevOps - 一種重視「軟體開發人員(Development)」(包含軟體品質、測試人員)和「IT 維運技術人員(Operations)」(包含維護、系統架設人員)之間溝通合作的文化、運動或慣例,一套開發的方法論。 - 使得構建、測試、發布軟體能夠更加地快捷、頻繁和可靠。 - The DevOps Cycle ![](https://i.imgur.com/go05B8H.png =65%x) 1. plan:根據使用者需求、之前缺點做軟體設計、做期限規劃。 2. code:開發程式碼、版本控制 3. build:整合、編譯程式碼、討論錯誤、debug 5. test:可以用 IaC 部屬到模擬環境 ( staging environment )、自動或手動測試 ( 安全性、用戶接受測試 User Acceptance Test ... ) 6. release:程式碼儲存庫,可靠性高的版本,隨時可以自動或由人決定是否部屬到生產環境。 7. deploy:可以用 IaC 部屬到生產環境( production environment )。 8. operate:維運,使用者能回饋、auto-scaling。 10. monitor:性能表現、歷史 errors 紀錄,納入 plan 的考量。 - 「開發」「測試」「維運」三者的結合 ![](https://i.imgur.com/bG1WGsK.png =40%x) >FIXME: pipline 釐清 > CI/CD 用列點「簡單」講解 - 核心流程 - CI ( Continuous Integration )/ CD ( Continuous Delivery) / CD ( Continuous Deployment ) pipeline - ![](https://i.imgur.com/8OWwgMo.png =150%x) [圖片來源](https://blogs.vmware.com/management/2020/03/vi-admin-to-devops.html) - CI ( Continuous Integration ) 持續整合:幫助開發人員更加頻繁地(有時甚至每天)將代碼更改合並到共享分支或“主幹”中。一旦開發人員對應用所做的更改被合併,系統就會通過自動構建應用並運行不同級別的自動化測試。 - CD ( Continuous Delivery ) 持續交付 - CD ( Continuous Deployment ) 持續部屬 ::: ## 甚麼是雲端自動化 - 利用自動化軟體,管理雲端資源,使人對IT設備的干涉減少,增加效率,減少錯誤,也可以降低企業的人力成本。 - 常見的雲端自動化應用 - 雲端部屬自動化 ( Terraform、Ansible)、資料備份 ( Google雲端硬碟 )、偵測 Zombie Cloud Infrastructure、自動測試 ( GitHub Actions ) ### Infrastructure as Code ( IaC ) #### 簡介 - 使用高階程式語言 ( HashiCorp、YAML ... ) 自動化部屬及管理雲端基礎架構,替代原本以手動部屬及管理,讓組織能以更快、更安全、成本更低的方式部屬雲端應用程式。 #### 優點 - 更快、更有效率的開發 - 因 IaC 將部屬雲端基礎設備的開發、測試、生產寫成程式碼,可以自動化部屬。 - 一致性,防止配置漂移 ( configuration drift ) :::info 配置漂移 ( configuration drift ): - 機器上的實際配置與原始程式碼管理或心理預設的配置不一致。或者在不同的電腦或設備上,若軟體的版本不同、或有甚麼被更新,並且沒有紀錄讓別人知道。 - 要確保測試環境跟上線環境相同,否則將軟體上線時可能會有非預期結果。 - 不良溝通、緊急的程式碼更改導致不能完整記錄所更改的程式碼。 ::: - IaC 可以每次都部屬相同的環境,防止安全性不足、及防止開發過程需要嚴格遵守規格的應用程式和服務產生問題。 - 模組可重複利用 - 每個小模組 ( module ) 可以互相整合,在未來可以重複利用。 - 重要人才流失的應對措施 - 在沒有 IaC 的情況下,一旦有人離職,會交由其他人手動部屬。若有 IaC ,仍可保留其部屬的環境。 - 降低成本及增加投資報酬率 ( ROI ) - 僱用的開發者在部屬時可複製之前的程式碼,讓花較少時間在重複性的部屬,專注於創新、關鍵的軟體開發 #### 可變的基礎架構 ( mutable infrastructure ) vs 不可變的基礎架構 ( immutable infrastructure ) ![](https://i.imgur.com/plKN086.png) #### 步驟型 ( Imperative approach ) vs 宣告型 ( Declarative approach ) - 為兩種編寫 IaC 的方法 ![](https://i.imgur.com/SwRTKVb.png) :::info #### 協調工具 vs 配置管理工具 - 協調工具 ( provisioning tool ) :配置、組織與管理基礎架構元件,即決定要什麼樣的機器。例子:Terraform。 - 配置管理工具 ( configuration management tool ):安裝、更新、管理基礎架構元件內所執行的軟體,決定要安裝什麼套件、設定什麼環境變數…等之類的。例子:Ansible (但它也能管理部分基礎架構)。 ::: ## Terraform ![](https://i.imgur.com/OaZ55Nt.png =10%x) - 特點 - 開源 - evolves quickly, with new benefits and improvements added consistently. - 支援多個雲供應商,包括公有雲及私有雲。例如:AWS、Azure... - 不可變的基礎架構 - 宣告式 IaC - 由 Go 語言所開發 - 介紹 - 語法介紹 ( HCL 語言 ) ![](https://i.imgur.com/M46yjGd.png) - provider - developed by HashiCorp, third-party vendors, and our Terraform community. - ![](https://i.imgur.com/PAUNnUw.png) - resource - ![](https://i.imgur.com/YwE2ndx.png) - variable - input - 使用者可以客製化參數 - ![](https://i.imgur.com/grbvxgU.png) - output - 想要看到模組的某個內容,在terraform apply後顯示 - ![](https://i.imgur.com/ESIgAjT.png) - data vs import - 不建新的資源,使用已經在雲端裡的資源 - data - 引入後沒有納入 terraform 管理,不能更改資源內容 - 指定資源的 name 或 id - ![](https://i.imgur.com/F8rFjHU.png) - terraform.tfstate 的 mode 為 data - ![](https://i.imgur.com/rDvzZqD.png) - 引用資源 - ![](https://i.imgur.com/QDBb9Y8.png) - import - 引入後納入 terraform 管理,可以更改資源內容 - 在 terminal 執行 terraform import ![](https://i.imgur.com/upGL5EA.png) ![](https://i.imgur.com/VZK9vGZ.png) - terraform.tfstate 的 mode 為 managed - 在 .tf 檔引用:![](https://i.imgur.com/clHjv7M.png) - 檔案 - ![](https://i.imgur.com/z3QOSQk.png =50%x) - .tf 檔 - 管理雲端資源的腳本 - 在同一資料夾下的 .tf 檔,全都會被執行 - terraform.tfstate 檔 - JSON 格式。 - 儲存有納入 terraform 管理的雲端資源狀態,`terraform apply` 後,會將此檔更新。 - 此檔預設存在本機,若為團體工作,要將此檔存在遠,讓大家知道雲端狀態。 - 建議將此檔做版本控制,若雲端資源部屬不小心被改壞,可以回到上一個版本的雲端狀態。 ![](https://i.imgur.com/qUg45th.png =20%x) ![](https://i.imgur.com/AwGMlBc.png) - 當執行 .tf 檔時,terraform 會根據 terraform.tfstate 檔各資源的 id、name 等等可以辨識出指定資源的代號,去雲端裡讀取該特定資源的狀態,並更新此檔, .tf 檔會用 resource type + name 去跟 terraform.tfstate 檔對照,可看出該資料夾管理的 .tf 檔有沒有變更或者資源在雲端中被手動變更,並且以 .tf 檔配置的狀態更新雲端資源狀態,可以防止配置飄移。 - 基本指令 - `terraform init` - initializes your working directory - Backend Initialization - "terraform.tfstate" - Child Module Installation - 會搜索 module ,然後通過 source 參數安裝所需模塊 - 安裝用到的雲平台外掛程式( plugin ) - `terraform plan` - 查看此資料夾下,將要做更新的雲端資源( .tf 檔)跟原本管理的資源( terraform.tfstate 檔)做比較,顯示將會變更的內容。 - `terraform apply` - 執行指定資料夾下的所有 .tf 檔 - `terraform show` - 用於展示當前 state 中所有被管理的資源及其所有屬性值。 - `terraform destroy` - 刪除此資料夾下,由 terraform 管理的資源 - 安裝 Terraform - 官網:https://www.terraform.io/downloads - 安裝 gnupg、software-properties-common 和 curl 套件. ``` sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl ``` - Add the HashiCorp GPG key. ``` curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - ``` - Add the official HashiCorp Linux repository. ``` sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" ``` - Update to add the repository, and install the Terraform CLI. ``` sudo apt-get update && sudo apt-get install terraform ``` - Terraform registry:有多個雲端供應商可納入 terraform 管理,Documentation 有介紹各個雲端供應商管理的 resource 的名稱。 https://registry.terraform.io/providers/terraform-provider-openstack/openstack/latest/docs ![](https://i.imgur.com/d0xaCfc.png) ## 實作 - 實作目標: - 用 terraform 在 openstack 上建 3 台 ubuntu server 的 VM,其中兩台可以被外網存取( ping 和 ssh ),最後一台只能被前兩台 VM 存取。意義為模擬實務上有兩台對外網站可以做 load balance,以防其中一個伺服器突然掛點,對內的 database 不能由外網存取。 - 架構 ![](https://i.imgur.com/kQCxcPG.png) - 步驟 1. 為了得到所需的環境變數: a. 到 https://10.20.20.1 登錄 openstack,帳號為 admin,密碼執行`sudo snap get microstack config.credentials.keystone-password`取得 ![](https://i.imgur.com/9tqYW0y.png) b. 下載 OpenStack RC File ![](https://i.imgur.com/QtMIem7.png) - 得到 admin-openrc.sh 環境變數檔(在 ~/Downloads 裡) ![](https://i.imgur.com/AWTY1Lq.png =20%x) ![](https://i.imgur.com/N3kZkJd.png) 2. 下載 cloud image 並上傳到儀表板 - 下載 https://cloud-images.ubuntu.com/focal/current/ 裡面的 focal-server-cloudimg-amd64.img - 這是專門給雲端環境用的系統映像檔,裡面會包含一個完整的 Linux 發行版,但這個 image 會在開機時自動尋找 cloud-init 的相關資訊,並利用這些資訊來對這個系統進行初始化,而初始化完成後,使用者就可以進去操作這個系統了。 :::info - cloud-init - userdata 用途:客製化 - add users - apt upgrade should be run on first boot - a different apt mirror should be used - additional apt sources should be added - certain SSH keys should be imported - and many more… - metadata - 雲端提供的 vm 的屬性資訊,在創建雲端 vm 的時候做預設配置,目前包括hostname、鏡像名稱、網路類型、ip位址等18個屬性值。 - 學長姐共筆: https://hackmd.io/@ncnu-opensource/book/https%3A%2F%2Fhackmd.io%2FkO6-flQpSsmR5jk-dytgkA%3Fview#Cloud-Config-Data 、 - 官網: https://cloudinit.readthedocs.io/en/latest/topics/modules.html# ::: - 上傳到儀表板 1. compute -> image -> create image ![](https://i.imgur.com/TpeGxqE.png) 2. 畫線處為需填寫的地方 ![](https://i.imgur.com/1neAHpZ.png) 3. 本機生成金鑰 `ssh-keygen -t rsa` `cd ~/.ssh/` `id_rsa`:私鑰 `id_rsa.pub`: 公鑰 4. 下載程式碼 - github: https://github.com/Ting-Jung-Lin/terraform-openstack.git 5. 執行指令 1. `. 路徑/admin-openrc.sh` 或 `source 路徑/admin-openrc.sh` 2. `cd 路徑/.tf檔` 3. `terraform init` ![](https://i.imgur.com/JNEX4cD.png) 4. `terraform plan` ![](https://i.imgur.com/KZWmPwc.png) 5. `terraform apply` ![](https://i.imgur.com/ieGoMPv.png) ![](https://i.imgur.com/CQofbPm.png) 6. 到 openstack 上啟動虛擬機試登錄 ![](https://i.imgur.com/KZ6BrmQ.png) - 結果 - Project -> Network -> Network Topology - ![](https://i.imgur.com/2Ztbtib.png) - ping - 外網 ping wordpress - ![](https://i.imgur.com/yDmbYOa.png) - 外網 ping database - ![](https://i.imgur.com/AXPbY4b.png) - wordpress ping database - ![](https://i.imgur.com/EXp4cVU.png) - ssh - 外網 ssh wordpress - ![](https://i.imgur.com/HhcT5z1.png) - 外網 ssh database - ![](https://i.imgur.com/D5UTAUh.png) - wordpress ssh database - ![](https://i.imgur.com/te5ZWaf.png)

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully