# K8s Yaml ## Default * apiVersion: 確定要用哪個版本的 k8s api 來執行後續的設定 > Ref. https://segmentfault.com/a/1190000017134399 ``` cmd kubectl api-versions //確定目前支援的版本 ``` * kind: 要建立什麼種類的 resource * metadata: 定義一些 resource 的基本設定 * name: resource 建立後的名稱 * labels: 掛在 resource 上的標籤,後續 resource 互相溝通時會使用 labels 來辨別 resource * spec: resource 內部的實際執行內容 ## Pod ### Basic * spec.containers: pod 裡面要建立的 container,可以建立多個 container * spec.containers.name: container 名稱 * spec.containers.image: 要使用什麼 image 來建立 container * spec.containers.args: 建立 container 後會在 container 內執行的 cmd ### Resources 資源限制 限制 container 所能使用的資源上限 ``` yaml apiVersion: apps/v1 kind: Pod metadata: name: nginx labels: app: nginx rel: stable1 spec: containers: - name: nginx image: nginx:alpine resources: limits: memory: "128Mi" # 128 MB cpu: "200m" # 200 milli cpu (.2 cpu or 20% of the cpu) ``` ### Probe 探針 probe 有 2 種,分為 liveness probe 跟 readness probe,用來確定 container 目前的狀態 * liveness probe: 定義 container 的健康狀態,代表是否該重啟 container 了 * readiness probe: 定義 container 是否可以接受 request,代表可以開始接受外部 request 了 Probe Types: 1. ExecAction : 在 container 內執行一個動作 2. TCPSocketAction: 透過 tcp 的方式檢查 container 的 ip 跟 port 3. HttpGetAction: 透過 Http Get 去訪問 container Probe Result: * Success * Failure * Unknown Config: * initialDelaySeconds: container 建立後 **N** 秒才會進行檢查 * timeoutSeconds: 每次請求 **N** 秒過後就算 timeout,預設值為 1 * periodSeconds: 每 **N** 秒檢查一次,預設值為 10 * failureThreshold: 可以失敗 **N** 次,第 **N+1** 次才算 health check 失敗,預設值為 3 #### Template: ##### Liveness Probe Template * HttpGetAction: 透過 http get 去檢查返回的 http status 是否為 200 ``` yaml apiVersion: apps/v1 kind: Pod metadata: name: nginx labels: app: nginx rel: stable1 spec: containers: - name: nginx image: nginx:alpine livenessProbe: httpGet: # probe types path: /index.html port: 80 initialDelaySeconds: 15 # container 建立後 15 秒才會進行檢查 timeoutSeconds: 2 # 每次請求 2 秒過後就算 timeout,預設值為 1 periodSeconds: 5 # 每 5 秒檢查一次,預設值為 10 failureThreshold: 1 # 可以失敗 1 次,第2次才算 health check 失敗,預設值為 3 ``` > httpGet Type,如果後續進入 container 內刪除指定的頁面 index.html,在下次的檢查時間會直接被踢出來,並且原 container 會被刪除並重新建立 ![](https://i.imgur.com/P0hWWwP.png) ![](https://i.imgur.com/0FFujqC.png) * ExecAction: 在 container 內部執行動作自我檢查 ``` yaml apiVersion: apps/v1 kind: Pod metadata: name: nginx labels: app: nginx rel: stable1 spec: containers: - name: nginx image: nginx:alpine args: # args for container - /bin/sh - -c - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 60; livenessProbe: exec: # probe types command: - cat - /tmp/healthy initialDelaySeconds: 15 # container 建立後 15 秒才會進行檢查 periodSeconds: 5 # 每 5 秒檢查一次 ``` > Exec Type,當他時間到並自動把 container 內 health check 的檔案刪除後,在下次的檢查時間會直接被踢出來,並且原 container 會被刪除並重新建立 ![](https://i.imgur.com/l7oS4Z8.png) ##### Readiness Probe Template ``` yaml apiVersion: apps/v1 kind: Pod metadata: name: nginx labels: app: nginx rel: stable1 spec: containers: - name: nginx image: nginx:alpine readinessProbe: httpGet: # probe types path: /index.html port: 80 initialDelaySeconds: 15 # container 建立後 15 秒才會進行檢查 periodSeconds: 5 # 每 5 秒檢查一次 ``` ## Deployment ### Basic Config: * spec.replicas: 指定 pod 數量 **N**,如果有 pod 死了會自動補到 **N**,pod 需保持 **N**,deployment 上版時會多 1 個 pod,等到上板結束便會刪除 * spec.minReadySeconds: 指定 pod 在啟用之後,最少需要存活 **N** 秒 * spec.selector: 尋找 pod template by labels,用來確定要用哪一組 template 去建立 pod 及 container * spec.selector.matchLabels: 指定要 pod template 的 label,符合的就會被拿來使用並建立 ### Template ``` yaml apiVersion: apps/v1 kind: Deployment metadata: # deployment 的設定 name: frontend labels: app: v1 spec: replicas: 3 # 指定 pod 數量為 3 個 minReadySeconds: 10 # pod 在啟用之後,最少需要存活 10 秒 selector: matchLabels: tier: frontend # 會去找 label > key: tier, value: frontend 的 labal,聽說是全部符合這個名字的都會,但我不確定怎麼跑 template: metadata: labels: tier: frontend # 代表這組 template 的 label 是 key: tier, value: frontend,因此上面這個 deployment 在執行時會把這組 template 拿來建立 pod 及 container spec: containers: - name: ap image: test/app:v1 resources: limits: memory: "128Mi" # 128 MB cpu: "200m" # 200 milli cpu (.2 cpu or 20% of the cpu) livenessProbe: httpGet: path: / port: 80 initialDelaySeconds: 15 periodSeconds: 5 ``` ###### tags: `K8s`