iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 11
1
Kubernetes

15 分鐘學習系列 - 第一次學 Kubernetes 就上手系列 第 11

Day 11 - Azure Kubernetes Service (AKS) 水平自動擴充 Pod

上一篇介紹如何擴充 AKS 節點與手動擴充 Pod, 今天的筆記將介紹如何設定自動擴充 Pod.
開始設定自動擴充之前, 要先了解目前 node 的配置狀況.

檢查與配置 Resource limit

  1. 使用命令 kubectl get nodes 取得 Nodes 資訊 (Node 名稱)
PS C:\k8s\dotnet-docker\samples\aspnetapp> kubectl get nodes
NAME                       STATUS    ROLES     AGE       VERSION
aks-nodepool1-25432928-0   Ready     agent     4d        v1.9.9
  1. 使用命令 kubectl describe nodes aks-nodepool1-25432928-0 確認 Node 資源配置的狀態, 以我們的範例尚未配置 Resource Quata, 因此我們需要先設定好 Resource 限制
Name:               aks-nodepool1-25432928-0
<略...>
Capacity:
 cpu:     2
 memory:  7137152Ki
 pods:    110
Allocatable:
 cpu:     1940m
 memory:  5567360Ki
 pods:    110
<略...>
  Namespace                  Name                                     CPU Requests  CPU Limits  Memory Requests  Memory Limits
  ---------                  ----                                     ------------  ----------  ---------------  -------------
  default                    aspnetapp-deployment-655457c855-55f52    0 (0%)        0 (0%)      0 (0%)           0 (0%)
  default                    aspnetapp-deployment-655457c855-7554f    0 (0%)        0 (0%)      0 (0%)           0 (0%)
  default                    aspnetapp-deployment-655457c855-rtk2m    0 (0%)        0 (0%)      0 (0%)           0 (0%)
<略...>
  1. 使用命令 code .\aspnetapp-deployment.yml 叫用 VS Code 修改 deployment manifest yaml file
PS C:\k8s\dotnet-docker\samples\aspnetapp> code .\aspnetapp-deployment.yml

a. 修改 Resources 配置, 其中 CPU 配置 Container 只使用 1 個 CPU 單元, Request 限制使用 0.5 個 CPU 單元

apiVersion: apps/v1
kind: Deployment
metadata:
  name: aspnetapp-deployment
  labels:
    app: aspnetapp
spec:
  replicas: 1
  template:
    metadata:
      name: aspnetapp
      labels:
        app: aspnetapp
    spec:
      containers:
      - name: aspnetapp
        image: 15maksacr.azurecr.io/aspnetapp:v1
        imagePullPolicy: IfNotPresent
        resources:
          requests:
            cpu: "0.5"
          limits:
            cpu: "1"
      restartPolicy: Always
  selector:
    matchLabels:
      app: aspnetapp

---

apiVersion: v1
kind: Service
metadata:
  name: aspnetapp-service
spec:
  selector:
    app: aspnetapp
  ports:
    - port: 80
  type: LoadBalancer

b. 使用命令 kubectl apply -f .\aspnetapp-deployment.yml 重新套用部署與服務

設定水平自動擴充 (hpa)

這裡遇到一個小問題, 因為 AKS 預設的版本比 Docker 還要舊, 因此出現無法偵測 CPU Metric 問題. 建議可以先參考下一篇 Day 12 - 升級 Azure Kubernetes Service (AKS) 叢集 將 AKS 版本升級到 1.10.8
為了能讓自動擴充展現, 筆者特別將 Target 調整到很小, 方便進行壓力測試

  1. 使用命令 kubectl autoscale deployment aspnetapp-deployment --cpu-percent=1 --min=1 --max=10 將自動擴充設定為 CPU-percent 設定為** 1%**, 並且將 minimum replica 數設為 1, maximum replica 數設為 10.
PS C:\k8s\dotnet-docker\samples\aspnetapp> kubectl autoscale deployment aspnetapp-de
ployment --cpu-percent=1 --min=1 --max=10
deployment.apps "aspnetapp-deployment" autoscaled
  1. 開啟另一個命令視窗, 使用 PowerShell 命令 While ($true) {curl http://52.163.87.155 > $null} 進行壓測, 其中 Public IP 可以用命令 kubectl get svc 取得

  2. 使用命令 kubectl get hpa 觀察水平自動擴充狀態, 請注意 Target 中有兩個數字, 是 Current/Target CPU 使用率

PS C:\WINDOWS\system32> kubectl get hpa
NAME                   REFERENCE                         TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
aspnetapp-deployment   Deployment/aspnetapp-deployment   0%/1%     1         10        1          43m
  1. 使用命令 kubectl get deployment 觀察 deployment 數量有無增加
PS C:\WINDOWS\system32> kubectl get deployment
NAME                   DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
aspnetapp-deployment   2         2         2            1           1h
  1. 使用命令 kubectl get pod 觀察 pod 數量有無增加, 並且注意狀態, 如果 > 1 表示水平自動擴充 (hpa) 已經成功設置且生效了
PS C:\WINDOWS\system32> kubectl get pod
NAME                                    READY     STATUS              RESTARTS   AGE
aspnetapp-deployment-7b94579445-nw45n   1/1       Running             0          4m
aspnetapp-deployment-7b94579445-wqbkp   1/1       Running             0          14s

小叮嚀: 若您練習後, 可以使用以下的命令 kubectl delete -f .\aspnetapp-deployment.yml 先將部署與服務刪除.

PS C:\k8s\dotnet-docker\samples\aspnetapp> kubectl delete -f .\aspnetapp-deployment.yml
deployment.apps "aspnetapp-deployment" deleted
service "aspnetapp-service" deleted

並且使用命令 kubectl delete hpa aspnetapp-deployment 將水平自動擴充配置刪除.

PS C:\k8s\dotnet-docker\samples\aspnetapp> kubectl delete hpa aspnetapp-deployment
horizontalpodautoscaler.autoscaling "aspnetapp-deployment" deleted

參考資料:


上一篇
Day 10 - 擴充 Azure Kubernetes Service (AKS) 叢集節點與手動擴充 Pods
下一篇
Day 12 - 升級 Azure Kubernetes Service (AKS) 叢集
系列文
15 分鐘學習系列 - 第一次學 Kubernetes 就上手30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言