阿里雲CLI是基於阿里雲開放API建立的管理工具。您可以通過該工具調用阿里雲開放API來管理阿里雲產品。
前提條件
在使用阿里雲CLI之前,您需要配置調用阿里雲資源所需的憑證資訊、地區、語言等。更多資訊,請參見簡介。
安裝配置阿里雲CLI和kubectl
Cloud Shell預設安裝配置了阿里雲CLI和帳號資訊,無需任何額外配置。如果您不使用Cloud Shell,您需要安裝和配置如下組件。
安裝阿里雲CLI。
在Linux上安裝阿里雲CLI,具體操作,請參見在Linux上安裝阿里雲CLI。
在macOS上安裝阿里雲CLI。
您也可以通過包管理器工具安裝CLI。請參考Homebrew,安裝包管理工具後,執行以下命令安裝CLI。
brew install aliyun-cli
在Windows上安裝阿里雲CLI,具體操作,請參見在Windows上安裝阿里雲CLI。
配置阿里雲CLI。執行以下命令,建立環境變數,用於存放身份認證資訊。
aliyun configure系統輸出類似以下結果。
Configuring profile 'default' in 'AK' authenticate mode... Access Key Id []: ************ Access Key Secret []: ************ Default Region Id []: cn-beijing Default Output Format [json]: json (Only support json) Default Language [zh|en] en: Saving profile[default] ...Done. Configure Done!!! ..............888888888888888888888 ........=8888888888888888888D=.............. ...........88888888888888888888888 ..........D8888888888888888888888I........... .........,8888888888888ZI: ...........................=Z88D8888888888D.......... .........+88888888 ..........................................88888888D.......... .........+88888888 .......Welcome to use Alibaba Cloud.......O8888888D.......... .........+88888888 ............. ************* ..............O8888888D.......... .........+88888888 .... Command Line Interface(Reloaded) ....O8888888D.......... .........+88888888...........................................88888888D.......... ..........D888888888888DO+. ..........................?ND888888888888D.......... ...........O8888888888888888888888...........D8888888888888888888888=........... ............ .:D8888888888888888888.........78888888888888888888O ..............
安裝和設定kubectl用戶端,詳情請參見Install and Set Up kubectl。
建立ACK Serverless叢集
建立一個工作目錄,並且在工作目錄下建立
create.json檔案。create.json檔案請求樣本如下:{ "cluster_type": "ManagedKubernetes", "profile": "Serverless", "name": "test-serverless-k8s", "region_id": "cn-hangzhou", "zoneid": "cn-hangzhou-h", "nat_gateway": true, "private_zone": false, "tags": [ {"key": "env", "value": "test"} ] }該樣本檔案描述了ACK Serverless叢集配置資訊。更多資訊,請參考建立ACK Serverless叢集。
執行以下命令建立ACK Serverless叢集。
aliyun cs POST /clusters --header "Content-Type=application/json" --body "$(cat create.json)"{ "cluster_id": "************************", "instanceId": "************************", "request_id": "**********-****-****-****-************", "task_id": "*-************" }建立成功後,執行以下命令查看叢集執行個體。
aliyun cs GET /clusters/<YOUR-CLUSTER-ID>系統輸出類似以下結果。
{ "cluster_id": "************************", "cluster_spec": "ack.standard", "cluster_type": "ManagedKubernetes", "created": "2024-05-06T14:48:40+08:00", "current_version": "1.28.3-aliyun.1", "deletion_protection": false, "external_loadbalancer_id": "lb-*********", "init_version": "1.28.3-aliyun.1", "name": "test-serverless-k8s", "network_mode": "vpc", "profile": "Serverless", "region_id": "cn-hangzhou", "resource_group_id": "rg-*********", "security_group_id": "sg-*********", "service_domain_name": "", "size": 0, "state": "running", "tags": [ { "key": "env", "value": "test" }, { "key": "ack.aliyun.com", "value": "cc98dd6edd4ff4c*****************" } ], "updated": "2024-05-06T14:52:44+08:00", "vpc_id": "vpc-*********", "vswitch_id": "vsw-*********", "zone_id": "cn-hangzhou-*" }執行以下命令擷取當前的叢集配置資訊。
KUBECONFIG=<YOUR-LOCAL-KUBECONFIG-PATH> aliyun cs GET /k8s/$cluster_id/user_config | jq -r '.config' > $KUBECONFIG kubectl get ns系統輸出類似以下結果。
NAME STATUS AGE default Active 7m43s kube-node-lease Active 7m45s kube-public Active 7m45s kube-system Active 7m45s
驗證ACK Serverless叢集
執行以下命令,部署Nginx應用。
kubectl run nginx --image=registry-vpc.cn-shenzhen.aliyuncs.com/acs-sample/nginx:latest系統輸出類似以下結果。
deployment.apps/nginx created執行以下命令,查詢Nginx應用狀態。
kubectl get deploy nginx系統輸出類似以下結果。
NAME READY UP-TO-DATE AVAILABLE AGE nginx 1/1 1 1 58s
刪除資源
如果您需要刪除Nginx應用,請執行以下命令。
kubectl delete deploy nginx系統輸出類似以下結果。
deployment.extensions "nginx" deleted您可以執行以下命令刪除包含ACK Serverless叢集和相關的VPC等資源。
aliyun cs DELETE /clusters/************