Slide 1

Slide 1 text

帶你從無到有打造 Kubernetes 的環境 Sammy, Scott, Can

Slide 2

Slide 2 text

Sammy Lin 17Live Engineering Director Scott 17Live Site Reliability Engineer Can Yu mutix Software Engineer 講者及助教介紹

Slide 3

Slide 3 text

[[警語]] 這場是 K8s 101 (請高手不要酸) 今天的 Workshop 不會深入介紹 K8s 底層,只有手把手的操作

Slide 4

Slide 4 text

Agenda 1. 認識 kubernetes (K8s) -- 5 mins 2. 建立你第一個 K8s cluster - 10 min 3. 運行一個靜態的網頁容器在 K8s 上 -- 20 min 4. 運行一個 Blog 在 K8s 上 (Mongo + flask) - 25 min 5. 透過 K8s 管理 YAML 工具 - 20 min a. helm b. kustomize c. cdk8s 6. 打造 CI/CD 的環境 - 10 min

Slide 5

Slide 5 text

認識 kubernetes (K8s)

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

今天大部份內容參考於這兩本書

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

現在...

Slide 10

Slide 10 text

現在...

Slide 11

Slide 11 text

天下武功,無堅不破,唯快不破

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Download link:https://www.cncf.io/phippy/

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

(坑) 我忘了怎麼做了 我以為沒有要了 我以為只是PoC 之前工程師留下來的

Slide 16

Slide 16 text

Immutable Infrastructure

Slide 17

Slide 17 text

前輩的寶典

Slide 18

Slide 18 text

Declarative Configuration

Slide 19

Slide 19 text

refer to: https://www.digitalocean.com/community/tutorials/imperative-vs-declarative-kubernetes-management-a-digitalocean-comic

Slide 20

Slide 20 text

refer to: https://www.digitalocean.com/community/tutorials/imperative-vs-declarative-kubernetes-management-a-digitalocean-comic

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Self-Healing Systems

Slide 23

Slide 23 text

自建(the hard way) or KaaS?

Slide 24

Slide 24 text

● 目前團隊規模? ● 專業人員是否足夠? ● 遇到現有技術上貧頸? ● 想要走在時代的的尖端,就是要超英趕美 評估

Slide 25

Slide 25 text

k8s distribution ref: https://landscape.cncf.io/category=certified-kubernetes-distribution&format=card-mode&grouping=category&sort=stars

Slide 26

Slide 26 text

k8s 服務供應商 ref: https://landscape.cncf.io/category=certified-kubernetes-hosted&format=card-mode

Slide 27

Slide 27 text

k8s 安裝工具 ref: https://landscape.cncf.io/category=certified-kubernetes-installer&format=card-mode&grouping=category&sort=stars

Slide 28

Slide 28 text

部署 K8s clusters in Clouds Sammy

Slide 29

Slide 29 text

DEMO https://parg.co/b0yH

Slide 30

Slide 30 text

DEMO - AKS $ az login $ az group create --name k8s-summit --location eastasia $ az aks create \ --name aks-2020 \ --resource-group k8s-summit \ --node-count 2 # 等待建置完成 ~ # 設定 kubeconfig $ az aks get-credentials --resource-group k8s-summit --name aks-2020 $ kubectl get nodes NAME STATUS ROLES AGE VERSION aks-nodepool1-30396539-vmss000000 Ready agent 21m v1.17.9 aks-nodepool1-30396539-vmss000001 Ready agent 20m v1.17.9

Slide 31

Slide 31 text

DEMO - GKE # 取得操作權限: 登入帳號, 設定隨意專案, 預設區域 26 (asia-east1-b) $ gcloud init $ gcloud container clusters create gke-2020 --num-nodes=2 # 等待建置完成 ~ # 設定 kubeconfig $ gcloud container clusters get-credentials gke-2020 $ kubectl get nodes NAME STATUS ROLES AGE VERSION gke-gke-2020-default-pool-ed94fc94-l7cs Ready 6m59s v1.15.12-gke.2 gke-gke-2020-default-pool-ed94fc94-rfkz Ready 6m59s v1.15.12-gke.2

Slide 32

Slide 32 text

DEMO - EKS $ export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE $ export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY $ export AWS_DEFAULT_REGION=us-west-2 $ eksctl create cluster \ --name eks-2020 \ --managed \ --node-type t3.medium # 等待建置完成, 自動更新 kubeconfig $ kubectl get nodes NAME STATUS ROLES AGE VERSION ip-192-168-31-192.us-west-2.compute.internal Ready 3m33s v1.17.9-eks-4c6976 ip-192-168-89-55.us-west-2.compute.internal Ready 3m52s v1.17.9-eks-4c6976

Slide 33

Slide 33 text

Cluster 之間的切換 $ kubectl config get-contexts CURRENT NAME CLUSTER AUTHINFO NAMESPACE aks-2020 aks-2020 aks-2020 * gke-2020 gke-2020 gke-2020 eks-2020 eks-2020 eks-2020 $ kubectl config use-context eks-2020 CURRENT NAME CLUSTER AUTHINFO NAMESPACE aks-2020 aks-2020 aks-2020 gke-2020 gke-2020 gke-2020 * eks-2020 eks-2020 eks-2020

Slide 34

Slide 34 text

Scott 運行一個靜態的網頁容器在 K8s 上

Slide 35

Slide 35 text

一個靜態網頁 ● 需要一台主機/開台VM ● 需要一個 Http Server ● 需要安裝好多東西...

Slide 36

Slide 36 text

過去的你

Slide 37

Slide 37 text

現在的你

Slide 38

Slide 38 text

DEMO $ git clone [email protected]:DevOpsTW/k8s_workshop.git

Slide 39

Slide 39 text

沒有 Deploy 不上 K8S 的 Container 只有包不好的Docker

Slide 40

Slide 40 text

你需要一份 Dockerfile 1. 用Official NGINX 的Image 2. 告訴Docker會用到80 Port 3. 把index.html copy到預設的Nginx路徑 4. Build Docker Image $ docker build . -t 5. 把Image推到Docker Repo $ docker push 搞定!

Slide 41

Slide 41 text

接著你要丟到 K8s 上,你需要的是... 1. 一個打包好的Docker Image 2. 一份 delpoyment.yaml 定義你的Pods要長 什麼樣子 3. 一份 service.yaml 來把服務開放出來

Slide 42

Slide 42 text

Deployment 在做什麼? 1. 控制Server的數量 2. 控制軟體的版本 3. 定義Container a. 環境變數 b. 開放的Port c. 掛載的Volume d. 使用的Image e. ...等等等

Slide 43

Slide 43 text

斯斯有三種,Service也有三種 ClusterIP(預設) 指定一個IP Address並且將所有traffic轉送到對應 的Pod身上,使用的是 K8S Cluster 內部的 IP Address,無法對外開放服務 ref: https://medium.com/google-cloud/kubernetes-nodeport-vs-loadbalancer-vs-ingress-when-should-i-use-what-922f010849e0

Slide 44

Slide 44 text

斯斯有三種,Service也有三種 NodePort 使用機器的IP對外開放服務,需要指定某一個對 外服務的Port ref: https://medium.com/google-cloud/kubernetes-nodeport-vs-loadbalancer-vs-ingress-when-should-i-use-what-922f010849e0

Slide 45

Slide 45 text

斯斯有三種,Service也有三種 LoadBalancer 使用外部IP建立對Pod的連線,沒有複雜的功能, 只是不斷的轉發送進來的 Traffic,各家平台會有 各自的實作方式,像是 GCP會建立對應的Network Load Balancer Config基本上跟ClusterIP相同,只是把Type改成 LoadBalancer ref: https://medium.com/google-cloud/kubernetes-nodeport-vs-loadbalancer-vs-ingress-when-should-i-use-what-922f010849e0

Slide 46

Slide 46 text

Ingress 又是什麼? 跟 Service 最大的差異是你可以做到 Layer 7的流 量控制,或著 Routing 等等 根據部署平台或著選用的 API Gateway 不同會有 些許差異,像是GCP會建立 Http Load Balancer, 你也可以選擇自己用 Nginx-Ingress 或著其他開 源軟體實作 ref: https://medium.com/google-cloud/kubernetes-nodeport-vs-loadbalancer-vs-ingress-wh en-should-i-use-what-922f010849e0

Slide 47

Slide 47 text

練習時間 1. Apply 新的 Image $ kubectl apply -f 01_plain/deploy/ 2. 用Port-Forwarding 連到 K8s的 pod (kubectl port-forward) $ kubectl port-forward svc/plain-app-service 10080:80 3. 或著用 kubectl 取得剛剛LoadBalancer的Address $ k get svc/plain-app 記得要用 http 開,我們沒有設定HTTPS(因為太麻煩了...

Slide 48

Slide 48 text

Scott 運行一個 Blog 在 K8s 上 (Mongo + flask)

Slide 49

Slide 49 text

Mongo StatefulSet: 保留狀態的 Deployment ● 確保每一個Pod的獨立性以及順序 ○ 啟動: 0 -> 1 -> 2 … ○ 關閉: … 2 -> 1 -> 0 ● 掛載的儲存區在每一次 Pod狀態變化 時都能夠確保是一致的 ○ ex: Pod 1 掛掉 -> 掛載在 Pod 1 上的 Data Volume 1 會重新被掛載在新啟 動的 Pod 1 上

Slide 50

Slide 50 text

Persistent Volume Claim, Persistent Volumes Persistent Volumes Claim (request) ● 告訴 K8s 我需要一個永久儲存區 Persistent Volumes ● 實體儲存區 PVC 會建立 PV 來使用 PV可以是Node上的硬碟或著其他儲存裝置 (NFS, AWS EBS, GCEPersistentDisk, AzureDisk…)

Slide 51

Slide 51 text

練習時間 1. 建立Mongo DB $ kubectl apply -f 02_mongo

Slide 52

Slide 52 text

Python + MongoDB $ kubectl apply -f 03_python_mgo/deploy

Slide 53

Slide 53 text

用 Config Map 改變應用程式的設定 Config Map可以被掛載在Pod的Volume之中 所以你可以拿來放 ● app的設定 ● script(mongo的case有用到) ● 任何其他想要經常修改,但不需要改動到 程式碼的東西 需要重新啟動Pod才會生效,或著在程式內設計每次讀取新的Config $ kubectl rollout restart deployment/flask-app

Slide 54

Slide 54 text

Secrets ConfigMap的好朋友,但是會加密存在K8S中 可以用來放 ● 使用者帳密檔案,例如 basic http auth 的 .htpasswd ● TLS用的 Certificate 跟 Private Key ● 其他不想被看到的秘密(但是只要能掛載 到 Pod 中還是會被發現)

Slide 55

Slide 55 text

監控Pod狀態 + 水平擴充Pod數量 先裝 K8S Metris-Server $ kubectl apply -f https://parg.co/b0vy 建立HPA $ kubectl autoscale deployment flask-app --cpu-percent=50 --min=1 --max=2 啟動壓測工具 Locust $ kubectl run -it --rm load-generator --image=doody/load_test --generator=run-pod/v1

Slide 56

Slide 56 text

監控Pod狀態 + Horizontal Pod Autoscaling 轉接壓測介面 $ kubectl port-forward load-generator 8089:8089 打開瀏覽器 http://localhost:8089 看HPA狀態 $ kubectl get hpa 設定HPA最大數量 $ kubectl patch hpa flask-app --patch '{"spec":{"maxReplicas":10}}'

Slide 57

Slide 57 text

k8s 管理 yaml 工具 Sammy

Slide 58

Slide 58 text

The Kubernetes command-line tool (kubectl) kubectl

Slide 59

Slide 59 text

kubectl - Kubernetes Object Management # Imperative commands $ kubectl create deployment nginx --image nginx. # Imperative object configuration $ kubectl create -f nginx.yaml # Declarative object configuration $ kubectl diff -f configs/ $ kubectl apply -f configs/ $ kubectl diff -R -f configs/ $ kubectl apply -R -f configs/

Slide 60

Slide 60 text

yaml Engineer 是你 ? DB ├── deployment.yaml ├── configmap.yaml ├── service.yaml └── ingress.yaml Web-A ├── deployment.yaml ├── configmap.yaml ├── service.yaml └── ingress.yaml Transaction Server ├── deployment.yaml ├── configmap.yaml ├── service.yaml └── ingress.yaml Auth Server ├── deployment.yaml ├── configmap.yaml ├── service.yaml └── ingress.yaml Graphql Server ├── deployment.yaml ├── configmap.yaml ├── service.yaml └── ingress.yaml Web B ├── deployment.yaml ├── configmap.yaml ├── service.yaml └── ingress.yaml API Server C ├── deployment.yaml ├── configmap.yaml ├── service.yaml └── ingress.yaml Live Stream ├── deployment.yaml ├── configmap.yaml ├── service.yaml └── ingress.yaml

Slide 61

Slide 61 text

https://www.facebook.com/DevOpsTaiwan/photos/pcb.3420564474687091/3420559708020901/

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

工具的選擇 ● Helm ○ Templating ● Kustomize ○ Composing and Customizing ● cdk8s ○ Code

Slide 64

Slide 64 text

Helm wordpress/ Chart.yaml # A YAML file containing information about the chart LICENSE # OPTIONAL: A plain text file containing the license for the chart README.md # OPTIONAL: A human-readable README file values.yaml # The default configuration values for this chart values.schema.json # OPTIONAL: A JSON Schema for imposing a structure on the values.yaml file charts/ # A directory containing any charts upon which this chart depends. crds/ # Custom Resource Definitions templates/ # A directory of templates that, when combined with values, deployment.yaml service.yaml configmap.yaml ingress.yaml # will generate valid Kubernetes manifest files. templates/NOTES.txt # OPTIONAL: A plain text file containing short usage notes

Slide 65

Slide 65 text

Kustomize ├── base │ ├── config-map.yaml │ ├── deployment.yaml │ ├── kustomization.yaml │ └── service.yaml └── overlays ├── production │ ├── app.cfg │ └── kustomization.yaml └── test ├── app.cfg └── kustomization.yaml

Slide 66

Slide 66 text

不想寫 YAML ???

Slide 67

Slide 67 text

cdk8s

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

很多 Kubernetes application management tools https://docs.google.com/spreadsheets/d/1FCgqz1Ci7_VCz_wdh8vBitZ3giBtac_H8SBw4uxnrsE/edit#gid=0

Slide 70

Slide 70 text

練習時間 - 透過三個工具來部署Application 1. Helm cd demo_app/04_configuration_tools/web-helm $ helm install test . -f ./values-development.yaml 2. Kustomize $ cd demo_app/04_configuration_tools/web-kustomize $ kubectl apply -k overlays/development/ 3. CDK8S $ npm install typescript $ npm run build $ kubectl apply -f dist/flaskapp.k8s.yaml

Slide 71

Slide 71 text

打造 CI/CD 的環境 Sammy

Slide 72

Slide 72 text

註冊 DockerHub (https://hub.docker.com)

Slide 73

Slide 73 text

Create Repository

Slide 74

Slide 74 text

Create Repository in Github

Slide 75

Slide 75 text

Create Repository in Github

Slide 76

Slide 76 text

Add Secrts

Slide 77

Slide 77 text

● DOCKER_USERNAME ● DOCKER_PASSWORD ● DOCKER_REPOSITORY ○ sammylin/k8s_cicd AWS ● AWS_ACCESS_KEY_ID ● AWS_SECRET_ACCESS_KEY ● KUBE_CONFIG_DATA ○ mv $HOME/.kube/config $HOME/.kube/config.tmp ○ eksctl utils write-kubeconfig --cluster=eks-2020 ○ cat $HOME/.kube/config | base64 | pbcopy GKE ● PROJECT_ID ● GOOGLE_APPLICATION_CREDENTIALS ○ Enter https://console.cloud.google.com/iam-admin/serviceaccounts ○ Add Service Account & grant “Kubernetes Engine Admin” role ○ cat gke_sa.json | base64 | pbcopy ● GKE_CLUSTER_NAME ● ZONE_NAME Add Secrts

Slide 78

Slide 78 text

Puch Code into Repository $ cd ~ $ mkdir 2020_workshop $ cd 2020_workshop $ git clone [email protected]:DevOpsTW/k8s_workshop.git $ cp -r k8s_workshop/demo_app/05_cicd . $ mv 05_cicd/ k8s_cicd ## Edit .github/workflow/main.yaml $ vi .github/workflows/main.yml

Slide 79

Slide 79 text

Puch Code into Repository $ cd k8s_cicd $ git init $ git commit -m "first commit" $ git branch -M master $ git remote add origin [email protected]:/k8s_cicd.git $ git push -u origin master

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

感謝您參加今天的活動!歡迎填寫此份問卷取得價 值25美元的AWS Credit Code,請務必正確填 寫資訊,以確保後續順利提供 Credit code,謝謝! Thank you for joining the event today! Please take a few minutes to finish the survey and make sure you check the box to receive US$25 AWS credits. Thank you for your time.

Slide 82

Slide 82 text

其他資源 ● Example code: https://github.com/devOpsTW/k8s_workshop/ ● DevOps Taiwan telegram: https://t.me/devopstw ● AWS CDK telegram: https://t.me/AWSCDK ● Amazon EKS用戶群: https://t.me/AmazonEKS Thank You