阿里云CLI 是基于阿里云开放 API 建立的管理工具。借助此工具,您可以通过调用阿里云开放 API 来管理阿里云产品。
Cloud Shell 默认安装配置了阿里云CLI和账号信息,无需任何额外配置。如果您不使用Cloud Shell,需要满足以下前提条件:
安装阿里云CLI。
brew install aliyun-cli
, 或参考 https://help.aliyun.com/document_detail/121544.html。配置阿里云CLI。创建环境变量,用于存放身份认证信息。
$ aliyun configure
Configuring profile 'default' in 'AK' authenticate mode...
Access Key Id []: ************
Access Key Secret []: ************
Default Region Id []: cn-hangzhou
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 ..............
说明 更多内容请参见 https://help.aliyun.com/document_detail/110341.html。
1、创建一个工作目录,并且在下面创建文件create.json
。
该文件描述了如下的K8s Serverless集群配置。详细信息请参考[创建集群实例] (https://help.aliyun.com/document_detail/93084.html)。
{
"cluster_type": "Ask",
"name": "test-serverless-k8s",
"region_id": "cn-beijing",
"zoneid": "cn-beijing-h",
"nat_gateway": true,
"private_zone": true,
"tags": [
{"key": "env", "value": "test"}
]
}
2、创建Serverless K8s集群。
$ aliyun cs POST /clusters --header "Content-Type=application/json" --body "$(cat create.json)"
{
"cluster_id": "************************",
"instanceId": "************************",
"request_id": "**********-****-****-****-************",
"task_id": "*-************"
}
3、查看集群实例。
$ aliyun cs GET /clusters/<YOUR-CLUSTER-ID>
{
"-": "PayByTraffic",
"cluster_healthy": "",
"cluster_id": "************************",
"cluster_spec": "",
"cluster_type": "Ask",
"created": "2020-07-23T10:02:18+08:00",
"current_version": "v1.16.6-aliyun.1",
"data_disk_category": "cloud",
"data_disk_size": 0,
"deletion_protection": false,
"docker_version": "",
"enabled_migration": false,
"external_loadbalancer_id": "lb-*********",
"gw_bridge": "",
"init_version": "v1.16.6-aliyun.1",
"instance_type": "",
"name": "test-serverless-k8s",
"need_update_agent": false,
"network_mode": "vpc",
"node_status": "",
"private_zone": false,
"profile": "ask.v2",
"region_id": "cn-beijing",
"resource_group_id": "rg-*********",
"security_group_id": "sg-*********",
"size": 0,
"state": "running",
"subnet_cidr": "172.16.0.0/16",
"swarm_mode": false,
"tags": [
{
"key": "env",
"value": "test"
}
],
"updated": "2020-07-23T10:05:11+08:00",
"vpc_id": "vpc-*********",
"vswitch_cidr": "",
"vswitch_id": "vsw-*********",
"worker_ram_role_name": ""
}
4、获取当前的集群配置。 说明 安装和设置 kubectl 客户端,详情请参见 https://kubernetes.io/docs/tasks/tools/install-kubectl/。
KUBECONFIG=<YOUR-LOCAL-KUBECONFIG-PATH>
$ aliyun cs GET /k8s/$cluster/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
5、测试集群,创建和删除应用。
$ kubectl run nginx --image=registry-vpc.cn-shenzhen.aliyuncs.com/acs-sample/nginx:latest
deployment.apps/nginx created
$ kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 58s
$ kubectl delete deploy nginx
deployment.extensions "nginx" deleted
6、删除资源,包含K8s集群和相关的VPC等资源。
$ aliyun cs DELETE /clusters/************