###### tags: `Meetup`、`Co-writing` # CNTUG meetup #23 [TOC] ## Session 1. Vishwakarma - A self-hosting Kubernetes cluster on AWS - Kyle Bai > Vishwakarma: Terraform modules for deloying EKS and Self-hosting Kubernetes ### Infrastructure as Code (IaC) 一般建 VM 大多都是使用類似 vagranet 這種 CLI Tool 或是用 Web Console 去部署,但是這樣 infra team 很難去維運。 透過 code 去定義資訊跟資源,用IaC 可以直接使用改 code 就可以自動部署在多個雲環境,會比手動使用 web console 更加有效率。 **The problems IaC tools can solve** 1. Create/Change/Destroy 多個 infrastructure 資源,如 compute, storage, networking 或是如 database, kubernetes cluster 等。 2. Update/Deploy infrastructure: 可以簡單地對 infra 去做升級及部署。 3. **Versioning infrastructure: 可以對 infra 架構做版本控管(因為是以code去表示)。** 可對 Infrastructure 做版本控管 可先 dry run 確認,之後再真正 deploy Infra (dry-run 可以預顯指令改變後的結果) 災難復原,可以快速 deploy different region or cloud provider。實際案例:17 play 由於 GCP 炸掉,導致服務掛掉,透過 IaC 能夠直接使用 code 部署在 Amazon 上面,不需要花太多時間去重建跟部署。 cloud-init: VM開啟時初始化(執行一些 pre-script/post-script),一般 private/public cloud 都會預先把它裝在 VM 裡。 ### Terraform Use the Hashicorp Configuration Language (HCL) to descrube the infrastructure resource. * 專注於管理Infra, 可以管理不同的公有雲, 不適合做應用程式的部署。 Concepts: * Provider: 哪個公有雲 * Provisioner: EC2, ELB, etc. * Modules: 可以把 HCL 寫成一個 Modules 拿來 reuse ,類似 Library。 * Plan Phase: 寫完 HCL 時,可以先使用 Plan Phase 看看結果。 * HCL: 可以定義描述Infrastructure resource * Apply Phase: 確定沒問題後執行 HCL。 **Comparation:** Ues CLI or Console deploy VM on multi-cloud: * 不同 Cloud 可能需要不同的 CLI 或是不同 Web Console Terraform CLI: * 不需要管不同的 Web Console, 只需要透過 Terraform 就可以部署 VM 在 multi-cloud。 terraform.tfvars 可以定義一些參數,例如: region, instance size or VPC ...etc Terraform 執行後會產生 terraform.tfstate ,之後的變動都會跟這個檔案做比對,類似 git diff 的功能 **Kubernetes + Terraform** * 較少人使用,大多用 `yaml`, `Helm` 部署。 * 可以使用 Terraform 部署 Kubernetes resource, 如: pod, deployment, service。 ### Vishwakarma Vishwakarma can be used to ceate a Kubernetes cluster in AWS by leveraging HashiCorp Terraform and CoreOS. * OS: coreOS, immutable, read-only **Challenges on build Kubernetes on AWS** 1. Maintain all AWS resource (EC2, instance, VPC) 2. Kubernetes node(worker) scalability. 3. Kubernetes components upgrade. 4. Logging and monitoring. 5. Configuration management. **Why deploy K8s by ourselves?** * Different instance type ASG(Auto Scaling Group). * Network environment customize. * Apply company policies: 例如網路不能對外,或是相關權限控管。 * Want more specific features: 可能需要自己客製化一些 Kubernetes 本身沒有的功能。 ... ### ElastKube ElastKube is highly configurable Terraform module with building blocks. The motivation to build ElastiKube is that we need: * Kubernetes master HA * Kubernetes worker HA * ... --- ## Session 2. Shift-Left Testing IaC With PaC - 蔡宗城 (Smalltown) IaC project: * Terraform * Pulumi * AWS CDK ... ### More Security and Compliance Testing shift left testing ### Policy as Code (PaC) what is policy? * Compliance Policy: infrastructure 或 code 是否有符合 PCI, GDPR 規範。 * Security Policies: code或是傳輸是否有作加密,或是是否有其他安全疑慮。 * Operational Excellence: Cluster是否有做HA, Fail over等服務相關的機制。 some policy as code tools: * ModSecurity: * Sentinel: only for Hashicorp * Inspec: * Open Policy Agent: ### Open Policy Agent OPA Feature: * Declarative Policy Language (Rego) * Library, Sidecar,Host-Level Daemon。 OPA Integrations: * Admission Control * API Auth * #### How does OPA work? Scenario 1. A salary system: You and your supervisor can view your salary ### Case 1: Network Misconfiguration * [本田汽車ElasticSearch案例](https://www.ithome.com.tw/news/134938) ##### Test Terraform with OPA OPA test 可以確保 Terraform Code 是否是安全的以及這樣配置是否適當,例如防火牆禁止配置如 `0.0.0.0/0` 這樣完全公開的設定。 * configuration check * Code Check * PR can run OPA test in CI ### Case 2. Audit K8s Cluster * Require specific lables on all resources **How OPA Audit K8s** 將 OPA agent 部署在 K8s (OPA 會變成 AdmissinoController),只要有部署資源,或是對 K8s 有任何操作, OPA 就會做 AdmissionView。 案例:某個使用者部署 OOM (Out of Memory) 的 pod ,導致同個 node 上的 pod 都被影響。因此可以透過 OPA 強制限定一定要撰寫 resources limit(quota)。 --- ## Session 3. 從大鯨魚 (Docker) 到地鼠三兄弟 (Podman: Pod manager) 的第一次旅程- Phil Huang 黃秉鈞 - 小飛機大帥哥 <- 誰寫ㄉ? 大部分從 Docker 轉移到 Podman 理由: 1. Security 2. Daemon 設計問題(使用量太大後,Daemon 資源會吃到 100%,會卡住沒辦法對 Docker 操作) > Podman 是 daemonless 改名稱: ~~Docker~~ Registry => Container Registry ~~Docker~~ Images => Container Images ~~Docker~~ Container => OCI Container ~~Docker~~file => Containerfile 實作範例: https://github.com/pichuang/debug-container > Podman 不可跟 Docker 併用 bud = build-using-dockerfile ### What is the missing commands in podman? * docker {network node plugin secret service stack swarm volume} * docker container {update rename} * Docker has daemon, but podman doesn't, so there's no command is "podman -d" * [podman-compose](https://github.com/containers/podman-compose): 實作沒有完整,估計明年中可以使用 * 常用的做法: podman + systemd * 要以Pod的觀點來維運,而非單一container (類似 k8s pod 概念) * podman 打的是 [root-less container](https://rootlesscontaine.rs/) * Podman , ### Reference 建議閱讀 * https://igene.tw/podman-intro ## Experience sharing & Job sharing