# 6. Cách cấu hình autoscale Kubernetes Cluster trên HI GIO Portal

## <mark style="color:green;">**Tổng quan**</mark> <a href="#overview" id="overview"></a>

*Hướng dẫn từng bước về cách cấu hình tự động mở rộng cluster Kubernetes HI GIO.*

* *Cài đặt tanzu-cli*
* *Tạo cluster-autoscaler deployment từ tanzu package sử dụng tanzu-cli*
* *Kích hoạt tự động mở rộng cho cluster của bạn*
* *kiểm tra tự động mở rộng cluster*
* *Xóa cluster-autoscaler deployment và dọn dẹp tài nguyên thử nghiệm.*

## <mark style="color:green;">Quy trình</mark> <a href="#id-2.-guidance" id="id-2.-guidance"></a>

* Bastion Ubuntu có thể kết nối với Kubernetes cluster của bạn.
* Có quyền truy cập vào Kubernetes cluster của bạn.

{% stepper %}
{% step %}
**Bước 1:** Cài đặt tanzu-cli

```
#Install tanzu-cli to ubuntu
sudo apt update
sudo apt install -y ca-certificates curl gpg
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://storage.googleapis.com/tanzu-cli-installer-packages/keys/TANZU-PACKAGING-GPG-RSA-KEY.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/tanzu-archive-keyring.gpg
echo "deb [signed-by=/etc/apt/keyrings/tanzu-archive-keyring.gpg] https://storage.googleapis.com/tanzu-cli-installer-packages/apt tanzu-cli-jessie main" | sudo tee /etc/apt/sources.list.d/tanzu.list
sudo apt update
sudo apt install -y tanzu-cli
#Verify tanzu-cli installation
tanzu version
```

<div align="left"><figure><img src="/files/6z1teipsR67Pk2WLDSpd" alt=""><figcaption></figcaption></figure></div>

{% hint style="warning" %}
Để cài đặt tanzu-cli trên môi trường khác, tham khảo tài liệu dưới đây:

[![](https://techdocs.broadcom.com/favicon.ico)Installing and Using VMware Tanzu CLI v1.5.x](https://techdocs.broadcom.com/us/en/vmware-tanzu/cli/tanzu-cli/1-5/cli/index.html)
{% endhint %}

{% hint style="info" %}
(Tùy chọn) Nếu bạn muốn định cấu hình hoàn thành tanzu, vui lòng chạy lệnh bên dưới và làm theo hướng dẫn đầu ra

`tanzu completion --help`
{% endhint %}

<div align="left"><figure><img src="/files/XwbeyHafmSFbm6RWUbls" alt=""><figcaption></figcaption></figure></div>
{% endstep %}

{% step %}
**Bước 2:** Tạo deployment cluster-autoscaler từ gói tanzu bằng tanzu-cli

* Chuyển sang context Kubernetes của bạn

```
kubectl config use-context <your context name>
```

<div align="left"><figure><img src="/files/jAfnjjBXXRT5HZlo8Ja8" alt=""><figcaption></figcaption></figure></div>

* Liệt kê các phiên bản cluster-autoscaler có sẵn trong gói tanzu và ghi lại tên phiên bản

```
tanzu package available list cluster-autoscaler.tanzu.vmware.com
```

<figure><img src="/files/e5GqoCBF7nEpBXvgp0fH" alt=""><figcaption></figcaption></figure>

* Tạo secret kubeconfig có tên `cluster-autoscaler-mgmt-config-secret` trong namespace `kube-system`

```
kubectl create secret generic cluster-autoscaler-mgmt-config-secret \
--from-file=value=<path to your kubeconfig file> \
-n kube-system
```

<div align="left"><figure><img src="/files/iPEFtdtlgFZTp4Hf1yLa" alt=""><figcaption></figcaption></figure></div>

{% hint style="warning" %}
Không thay đổi tên secret (cluster-autoscaler-mgmt-config-secret) và namespace (kube-system).
{% endhint %}

* Tạo file `cluster-autoscaler-values.yaml`&#x20;

```
arguments:
  ignoreDaemonsetsUtilization: true
  maxNodeProvisionTime: 15m
  maxNodesTotal: 0 #Leave this value as 0. We will define the max and min number of nodes later.
  metricsPort: 8085
  scaleDownDelayAfterAdd: 10m
  scaleDownDelayAfterDelete: 10s
  scaleDownDelayAfterFailure: 3m
  scaleDownUnneededTime: 10m
clusterConfig:
  clusterName: "demo-autoscale-tkg" #adjust here
  clusterNamespace: "demo-autoscale-tkg-ns" #adjust here
paused: false
```

{% hint style="warning" %}
Giá trị bắt buộc:

* `clusterName`: Tên cluster của bạn

* `clusterNamespace`: Tên cluster namespace của bạn
  {% endhint %}

* Cài đặt cluster-autoscaler

```
tanzu package install cluster-autoscaler \
--package cluster-autoscaler.tanzu.vmware.com \
--version <version available> \ #adjust the version listed above to match your kubernetes version
--values-file 'cluster-autoscaler-values.yaml' \
--namespace tkg-system #please do not change, this is default namespace for tanzu package
```

<div align="left"><figure><img src="/files/KLoEHacPdImN9r9cYwTt" alt=""><figcaption></figcaption></figure></div>

{% hint style="warning" %}
Cluster-autoscaler sẽ được triển khai vào namespace `kube-system`.

Chạy lệnh dưới đây để xác minh việc triển khai cluster-autoscaler:

`kubectl get deployments.apps -n kube-system cluster-autoscaler`
{% endhint %}

<div align="left"><figure><img src="/files/zeFeDxyCR5wBL64FlsLz" alt=""><figcaption></figcaption></figure></div>

* Cấu hình số lượng node tối thiểu và tối đa trong cluster
  * Lấy tên và namespace của `machinedeployments`

```
kubectl get machinedeployments.cluster.x-k8s.io -A
```

<figure><img src="/files/wdkQS4c31hj7bL4HJeX2" alt=""><figcaption></figcaption></figure>

* Cấu hình `cluster-api-autoscaler-node-group-min-size` and `cluster-api-autoscaler-node-group-max-size`

```
kubectl annotate machinedeployment <machinedeployment name> cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size=<number min> -n <machinedeployment namespace>
kubectl annotate machinedeployment <machinedeployment name> cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size=<number max> -n <machinedeployment namespace>
```

* Bật cluster autoscale cho cluster của bạn

{% hint style="warning" %}
Vì bước này cần có sự cho phép của nhà cung cấp để thực hiện nên vui lòng thông báo cho cloud provider để thực hiện bước này.
{% endhint %}
{% endstep %}

{% step %}
**Bước 3:** Kiểm tra tự động mở rộng cluster

* Lấy số lượng node hiện tại

`kubectl get nodes`

{% hint style="warning" %}
Hiện tại chỉ có một worker node.
{% endhint %}

* Tạo file `test-autoscale.yaml`&#x20;

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
      topologySpreadConstraints: #Spreads pods across different nodes (ensures no node has more pods than others)
      - maxSkew: 1 
        topologyKey: kubernetes.io/hostname
        whenUnsatisfiable: DoNotSchedule
        labelSelector:
          matchLabels:
            app: nginx
```

* Áp dụng file `test-autoscale.yaml` để triển khai 2 replicas nginx pod trong namespace default (lệnh này sẽ kích hoạt tạo một worker node mới)

```
kubectl apply -f test-autoscale.yaml
```

<div align="left"><figure><img src="/files/Da3DLvvKlaC1WqLM3TFu" alt=""><figcaption></figcaption></figure></div>

* Kiểm tra deployment nginx

```
kubectl get pods
```

<div align="left"><figure><img src="/files/zjXJr3dhdHEsjoNuNjyD" alt=""><figcaption></figcaption></figure></div>

```
kubectl describe pod nginx-589656b9b5-mcm5j | grep -A 10 Events
```

<figure><img src="/files/wmB9QRdwGEWii6l6W6cF" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
Bạn sẽ thấy một nginx pod mới với trạng thái Pending và các sự kiện hiển thị `FailedScheduling` và `TriggeredScaleUp`:
{% endhint %}

```
Warning  FailedScheduling  2m53s  default-scheduler   0/2 nodes are available: 1 node(s) didn't match pod topology spread constraints, 1 node(s) had untolerated taint {node-role.kubernetes.io/control-plane: }. preemption: 0/2 nodes are available: 1 No preemption victims found for incoming pod, 1 Preemption is not helpful for scheduling.
Normal   TriggeredScaleUp  2m43s  cluster-autoscaler  pod triggered scale-up: [{MachineDeployment/demo-autoscale-tkg-ns/demo-autoscale-tkg-worker-node-pool-1 1->2 (max: 5)}]
```

* Chờ một chút và bạn sẽ thấy một worker node mới đã được cấp phát và nginx pod mới có trạng thái Running.

  <figure><img src="/files/48zQ3JOi1NtCM4L1Xzlh" alt=""><figcaption></figcaption></figure>
* Dọn dẹp tài nguyên thử nghiệm

```
kubectl delete -f test-autoscale.yaml
```

<div align="left"><figure><img src="/files/EWXJ5JwNviQGm20NFRGb" alt=""><figcaption></figcaption></figure></div>

{% hint style="warning" %}
Sau khi xóa deployment nginx thử nghiệm, cluster sẽ chờ vài phút để xóa node không cần thiết (tham khảo giá trị `scaleDownUnneededTime` trong file `cluster-autoscaler-values.yaml`).
{% endhint %}

* Xóa deployment cluster-autoscaler (Tùy chọn)

{% hint style="info" %}
Nếu bạn không muốn cluster của mình tự động mở rộng nữa, có thể xóa deployment cluster-autoscaler bằng tanzu-cli:

```
tanzu package installed delete cluster-autoscaler -n tkg-system -y
```

{% endhint %}

<figure><img src="/files/x8YmlIGaYZadtgagoI0i" alt=""><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.higiocloud.vn/hi-gio-user-guide-vn/hi-gio-kubernetes/6.-cach-cau-hinh-autoscale-kubernetes-cluster-tren-hi-gio-portal.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
